-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrelation-meta-builders.ts
More file actions
225 lines (197 loc) · 6.75 KB
/
relation-meta-builders.ts
File metadata and controls
225 lines (197 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { safeInflection } from './inflection-utils';
import {
buildForeignKeyConstraint,
} from './constraint-meta-builders';
import { buildFieldList, type BuildContext } from './table-meta-context';
import {
getRelation,
getResourceCodec,
getUniques,
sameAttributes,
} from './table-resource-utils';
import type {
BelongsToRelation,
HasRelation,
ManyToManyRelation,
MetaBuild,
PgAttribute,
PgCodec,
PgManyToManyRelationDetails,
PgRelation,
PgTableResource,
PgUnique,
} from './types';
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
export function buildBelongsToRelations(
codec: PgCodec,
attributes: Record<string, PgAttribute>,
uniques: PgUnique[],
relations: Record<string, PgRelation>,
context: BuildContext,
): BelongsToRelation[] {
const belongsTo: BelongsToRelation[] = [];
for (const [relationName, relation] of Object.entries(relations)) {
// PostGraphile only sets isReferencee when true (reverse FK);
// forward relations have isReferencee undefined, not false.
if (relation.isReferencee) continue;
const localAttributes = relation.localAttributes || [];
const isUnique = uniques.some((unique) =>
sameAttributes(unique.attributes, localAttributes),
);
const remoteCodec = relation.remoteResource?.codec;
belongsTo.push({
fieldName: relationName,
isUnique,
type: remoteCodec?.name || null,
keys: buildFieldList(localAttributes, codec, attributes, context),
references: { name: remoteCodec?.name || 'unknown' },
});
}
return belongsTo;
}
export function buildReverseRelations(
codec: PgCodec,
attributes: Record<string, PgAttribute>,
relations: Record<string, PgRelation>,
context: BuildContext,
): { hasOne: HasRelation[]; hasMany: HasRelation[] } {
const hasOne: HasRelation[] = [];
const hasMany: HasRelation[] = [];
for (const [relationName, relation] of Object.entries(relations)) {
if (relation.isReferencee !== true) continue;
const remoteUniques = getUniques(relation.remoteResource || {});
const remoteAttributes = relation.remoteAttributes || [];
const isUnique = remoteUniques.some((unique) =>
sameAttributes(unique.attributes, remoteAttributes),
);
const remoteCodec = relation.remoteResource?.codec;
const meta: HasRelation = {
fieldName: relationName,
isUnique,
type: remoteCodec?.name || null,
keys: buildFieldList(relation.localAttributes || [], codec, attributes, context),
referencedBy: { name: remoteCodec?.name || 'unknown' },
};
if (isUnique) {
hasOne.push(meta);
} else {
hasMany.push(meta);
}
}
return { hasOne, hasMany };
}
function isManyToManyDetails(value: unknown): value is PgManyToManyRelationDetails {
if (!isRecord(value)) return false;
return (
isRecord(value.leftTable) &&
isRecord(value.junctionTable) &&
isRecord(value.rightTable) &&
typeof value.leftRelationName === 'string' &&
typeof value.rightRelationName === 'string'
);
}
function parseManyToManyRelationships(value: unknown): PgManyToManyRelationDetails[] {
if (!Array.isArray(value)) return [];
return value.filter(isManyToManyDetails);
}
function getManyToManyRelationships(
build: MetaBuild,
tableResource: PgTableResource,
codec: PgCodec,
): PgManyToManyRelationDetails[] {
const relationshipsByResource = build.pgManyToManyRealtionshipsByResource;
if (!(relationshipsByResource instanceof Map)) return [];
const direct = parseManyToManyRelationships(relationshipsByResource.get(tableResource));
if (direct.length > 0) return direct;
for (const [leftTable, relationships] of relationshipsByResource.entries()) {
const details = parseManyToManyRelationships(relationships);
if (details.length === 0 || !isRecord(leftTable)) continue;
if ((leftTable as PgTableResource).codec === codec) {
return details;
}
}
return [];
}
function buildManyToManyRelation(
details: PgManyToManyRelationDetails,
context: BuildContext,
): ManyToManyRelation | null {
const leftCodec = getResourceCodec(details.leftTable);
const junctionCodec = getResourceCodec(details.junctionTable);
const rightCodec = getResourceCodec(details.rightTable);
if (!leftCodec || !junctionCodec || !rightCodec) return null;
const leftRelation = getRelation(details.leftTable, details.leftRelationName);
const junctionRightRelation = getRelation(details.junctionTable, details.rightRelationName);
if (!leftRelation || !junctionRightRelation) return null;
const leftJunctionAttributes = leftRelation.remoteAttributes || [];
const leftTableAttributes = leftRelation.localAttributes || [];
const rightJunctionAttributes = junctionRightRelation.localAttributes || [];
const rightTableAttributes = junctionRightRelation.remoteAttributes || [];
const relationFieldName = safeInflection(
() => context.build.inflection._manyToManyRelation?.(details),
details.rightRelationName || rightCodec.name || null,
);
const junctionLeftConstraint = buildForeignKeyConstraint(
details.leftRelationName || `${junctionCodec.name}_${leftCodec.name}_fkey`,
junctionCodec,
junctionCodec.attributes,
leftJunctionAttributes,
leftCodec,
leftCodec.attributes,
leftTableAttributes,
context,
);
const junctionRightConstraint = buildForeignKeyConstraint(
details.rightRelationName || `${junctionCodec.name}_${rightCodec.name}_fkey`,
junctionCodec,
junctionCodec.attributes,
rightJunctionAttributes,
rightCodec,
rightCodec.attributes,
rightTableAttributes,
context,
);
return {
fieldName: relationFieldName,
type: rightCodec.name || null,
junctionTable: { name: junctionCodec.name || 'unknown' },
junctionLeftConstraint,
junctionLeftKeyAttributes: buildFieldList(
leftJunctionAttributes,
junctionCodec,
junctionCodec.attributes,
context,
),
junctionRightConstraint,
junctionRightKeyAttributes: buildFieldList(
rightJunctionAttributes,
junctionCodec,
junctionCodec.attributes,
context,
),
leftKeyAttributes: buildFieldList(
leftTableAttributes,
leftCodec,
leftCodec.attributes,
context,
),
rightKeyAttributes: buildFieldList(
rightTableAttributes,
rightCodec,
rightCodec.attributes,
context,
),
rightTable: { name: rightCodec.name || 'unknown' },
};
}
export function buildManyToManyRelations(
resource: PgTableResource,
codec: PgCodec,
context: BuildContext,
): ManyToManyRelation[] {
return getManyToManyRelationships(context.build, resource, codec)
.map((details) => buildManyToManyRelation(details, context))
.filter((relation): relation is ManyToManyRelation => relation !== null);
}