Skip to content

Commit 1bae39d

Browse files
committed
tidy up resolve-relationships
1 parent c3c3f96 commit 1bae39d

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

packages/core/src/lib/core/resolve-relationships.ts

+24-37
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,20 @@ type RelWithoutForeignKeyAndName = Omit<Rel, 'field'> & {
4040
function sortRelationships (left: Rel, right: Rel): readonly [Rel, RelWithoutForeignKeyAndName] {
4141
if (left.field.mode === 'one' && right.field.mode === 'one') {
4242
if (left.field.foreignKey !== undefined && right.field.foreignKey !== undefined) {
43-
throw new Error(
44-
`You can only set db.foreignKey on one side of a one to one relationship, but foreignKey is set on both ${left.listKey}.${left.fieldPath} and ${right.listKey}.${right.fieldPath}`
45-
)
46-
}
47-
if (left.field.foreignKey || right.field.foreignKey) {
48-
// return the field that specifies the foreignKey first
49-
return left.field.foreignKey ? [left, right] : [right, left]
43+
throw new Error(`You can only set db.foreignKey on one side of a one to one relationship, but foreignKey is set on both ${left.listKey}.${left.fieldPath} and ${right.listKey}.${right.fieldPath}`)
5044
}
45+
46+
// return the field that specifies the foreignKey first
47+
if (left.field.foreignKey) return [left, right]
48+
if (right.field.foreignKey) return [right, left]
49+
5150
} else if (left.field.mode === 'one' || right.field.mode === 'one') {
5251
// many relationships will never have a foreign key, so return the one relationship first
5352
const rels = left.field.mode === 'one' ? ([left, right] as const) : ([right, left] as const)
5453
// we're only doing this for rels[1] because:
5554
// - rels[1] is the many side
5655
// - for the one side, TypeScript will already disallow relationName
57-
if (rels[1].field.relationName !== undefined) {
58-
throw new Error(
59-
`You can only set db.relationName on one side of a many to many relationship, but db.relationName is set on ${rels[1].listKey}.${rels[1].fieldPath} which is the many side of a many to one relationship with ${rels[0].listKey}.${rels[0].fieldPath}`
60-
)
61-
}
56+
if (rels[1].field.relationName !== undefined) throw new Error(`You can only set db.relationName on one side of a many to many relationship, but db.relationName is set on ${rels[1].listKey}.${rels[1].fieldPath} which is the many side of a many to one relationship with ${rels[0].listKey}.${rels[0].fieldPath}`)
6257
return rels
6358
}
6459
if (
@@ -67,22 +62,21 @@ function sortRelationships (left: Rel, right: Rel): readonly [Rel, RelWithoutFor
6762
(left.field.relationName !== undefined || right.field.relationName !== undefined)
6863
) {
6964
if (left.field.relationName !== undefined && right.field.relationName !== undefined) {
70-
throw new Error(
71-
`You can only set db.relationName on one side of a many to many relationship, but db.relationName is set on both ${left.listKey}.${left.fieldPath} and ${right.listKey}.${right.fieldPath}`
72-
)
65+
throw new Error(`You can only set db.relationName on one side of a many to many relationship, but db.relationName is set on both ${left.listKey}.${left.fieldPath} and ${right.listKey}.${right.fieldPath}`)
7366
}
7467
return left.field.relationName !== undefined ? [left, right] : [right, left]
7568
}
69+
7670
const order = left.listKey.localeCompare(right.listKey)
77-
if (order > 0) {
78-
// left comes after right, so swap them.
79-
return [right, left]
80-
} else if (order === 0) {
71+
72+
if (order > 0) return [right, left] // left comes after right, so swap them
73+
if (order === 0) {
8174
// self referential list, so check the paths.
8275
if (left.fieldPath.localeCompare(right.fieldPath) > 0) {
8376
return [right, left]
8477
}
8578
}
79+
8680
return [left, right]
8781
}
8882

@@ -97,9 +91,7 @@ export function resolveRelationships (
9791
lists: Record<string, { fields: Record<string, { dbField: DBField }>, isSingleton: boolean }>
9892
): ListsWithResolvedRelations {
9993
const alreadyResolvedTwoSidedRelationships = new Set<string>()
100-
const resolvedLists: Record<string, Record<string, ResolvedDBField>> = Object.fromEntries(
101-
Object.keys(lists).map(listKey => [listKey, {}])
102-
)
94+
const resolvedLists: Record<string, Record<string, ResolvedDBField>> = Object.fromEntries(Object.keys(lists).map(listKey => [listKey, {}]))
10395
for (const [listKey, fields] of Object.entries(lists)) {
10496
const resolvedList = resolvedLists[listKey]
10597
for (const [fieldPath, { dbField: field }] of Object.entries(fields.fields)) {
@@ -109,37 +101,27 @@ export function resolveRelationships (
109101
}
110102
const foreignUnresolvedList = lists[field.list]
111103
if (!foreignUnresolvedList) {
112-
throw new Error(
113-
`The relationship field at ${listKey}.${fieldPath} points to the list ${listKey} which does not exist`
114-
)
104+
throw new Error(`The relationship field at ${listKey}.${fieldPath} points to the list ${listKey} which does not exist`)
115105
}
116106
if (foreignUnresolvedList.isSingleton) {
117-
throw new Error(
118-
`The relationship field at ${listKey}.${fieldPath} points to a singleton list, ${listKey}, which is not allowed`
119-
)
107+
throw new Error(`The relationship field at ${listKey}.${fieldPath} points to a singleton list, ${listKey}, which is not allowed`)
120108
}
121109
if (field.field) {
122110
const localRef = `${listKey}.${fieldPath}`
123111
const foreignRef = `${field.list}.${field.field}`
124-
if (alreadyResolvedTwoSidedRelationships.has(localRef)) {
125-
continue
126-
}
112+
if (alreadyResolvedTwoSidedRelationships.has(localRef)) continue
127113

128114
alreadyResolvedTwoSidedRelationships.add(foreignRef)
129115
const foreignField = foreignUnresolvedList.fields[field.field]?.dbField
130116
if (!foreignField) throw new Error(`${localRef} points to ${foreignRef}, but ${foreignRef} doesn't exist`)
131117

132118
if (foreignField.kind !== 'relation') {
133-
throw new Error(
134-
`${localRef} points to ${foreignRef}, but ${foreignRef} is not a relationship field`
135-
)
119+
throw new Error(`${localRef} points to ${foreignRef}, but ${foreignRef} is not a relationship field`)
136120
}
137121

138122
const actualRef = foreignField.field ? `${foreignField.list}.${foreignField.field}` : foreignField.list
139123
if (actualRef !== localRef) {
140-
throw new Error(
141-
`${localRef} expects ${foreignRef} to be a two way relationship, but ${foreignRef} points to ${actualRef}`
142-
)
124+
throw new Error(`${localRef} expects ${foreignRef} to be a two way relationship, but ${foreignRef} points to ${actualRef}`)
143125
}
144126

145127
const [leftRel, rightRel] = sortRelationships(
@@ -175,6 +157,7 @@ export function resolveRelationships (
175157
}
176158
continue
177159
}
160+
178161
if (leftRel.field.mode === 'many' && rightRel.field.mode === 'many') {
179162
const relationName = leftRel.field.relationName ?? `${leftRel.listKey}_${leftRel.fieldPath}`
180163
resolvedLists[leftRel.listKey][leftRel.fieldPath] = {
@@ -195,6 +178,7 @@ export function resolveRelationships (
195178
}
196179
continue
197180
}
181+
198182
const relationName = `${leftRel.listKey}_${leftRel.fieldPath}`
199183
resolvedLists[leftRel.listKey][leftRel.fieldPath] = {
200184
kind: 'relation',
@@ -211,6 +195,7 @@ export function resolveRelationships (
211195
},
212196
relationName,
213197
}
198+
214199
resolvedLists[rightRel.listKey][rightRel.fieldPath] = {
215200
kind: 'relation',
216201
mode: 'many',
@@ -221,6 +206,7 @@ export function resolveRelationships (
221206
}
222207
continue
223208
}
209+
224210
const foreignFieldPath = `from_${listKey}_${fieldPath}`
225211
if (foreignUnresolvedList.fields[foreignFieldPath]) {
226212
throw new Error(`The relationship field at ${listKey}.${fieldPath} points to the list ${field.list}, Keystone needs to a create a relationship field at ${field.list}.${foreignFieldPath} to support the relationship at ${listKey}.${fieldPath} but ${field.list} already has a field named ${foreignFieldPath}`)
@@ -269,6 +255,7 @@ export function resolveRelationships (
269255
}
270256
}
271257
}
258+
272259
// the way we resolve the relationships means that the relationships will be in a
273260
// different order than the order the user specified in their config
274261
// doesn't really change the behaviour of anything but it means that the order of the fields in the prisma schema will be

0 commit comments

Comments
 (0)