-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild.ts
More file actions
340 lines (300 loc) · 16 KB
/
Copy pathbuild.ts
File metadata and controls
340 lines (300 loc) · 16 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { isArray } from 'radash';
import { buildSuqlFilter, parseFilter } from '../../../adapters/surrealDB/filters/filters';
import { sanitizeNameSurrealDB } from '../../../adapters/surrealDB/helpers';
import { parseValueSurrealDB } from '../../../adapters/surrealDB/parsing/values';
import { getCurrentFields, getSchemaByThing, oFilter } from '../../../helpers';
import type { EnrichedBormRelation, EnrichedBormSchema, EnrichedBQLMutationBlock } from '../../../types';
import { Parent } from '../../../types/symbols';
import type { FlatBqlMutation } from '../bql/flatter';
export const buildSURQLMutation = async (flat: FlatBqlMutation, schema: EnrichedBormSchema) => {
const buildThings = (block: EnrichedBQLMutationBlock) => {
//console.log('currentThing:', block);
const { $filter, $thing, $bzId, $op, $id, $tempId } = block;
const currentSchema = getSchemaByThing(schema, $thing);
const { usedDataFields } = getCurrentFields(currentSchema, block);
const { idFields } = currentSchema;
const idValue = $id || block[idFields[0]];
const sanitizedThings = (isArray($thing) ? $thing : [$thing]).map(sanitizeNameSurrealDB);
const meta = oFilter(block, (k: string) => k.startsWith('$'));
const rest = oFilter(block, (k: string) => !k.startsWith('$'));
const restString = JSON.stringify(rest);
const metaString = Object.entries(meta)
.map(([key, value]) => (key === '$tempId' ? `'$tempId': '_:${value}'` : `'${key}': '${value}'`)) //todo: At some point migrate tempIds so they only use _: when not explicit.
.join(',');
const parent = block[Parent as any]; //todo
const dataFieldStrings = usedDataFields
.filter((df) => !idFields.includes(df))
.map((df) => {
const dataFieldSchema = currentSchema.dataFields?.find((f) => f.path === df || f.dbPath === df);
if (!dataFieldSchema) {
throw new Error(`Data field schema not found for ${df}`);
}
const value = block[df];
if (value === null) {
return `${df} = NONE`;
}
return `${df} = ${parseValueSurrealDB(value, dataFieldSchema.contentType)}`;
})
.filter(Boolean);
const VAR = `$⟨${$tempId || $bzId}⟩`;
const COND = (() => {
if (parent?.bzId) {
return `array::flatten($⟨${parent.bzId}⟩.⟨${parent.edgeField}⟩ || []).filter(|$v| $v != NONE).len`;
}
if (idValue) {
if (isArray(idValue)) {
return sanitizedThings.flatMap((table: string) => idValue.map((id: string) => `${table}:⟨${id}⟩`)).join(', ');
}
return sanitizedThings.map((table: string) => `${table}:⟨${idValue}⟩`).join(', ');
}
return true; // no parent, no id value, then we can run the update or deletion safely
})();
const TARGET = (() => {
//Non root
if (parent?.bzId) {
const parentRef = `array::flatten($⟨${parent.bzId}⟩.⟨${parent.edgeField}⟩ || []).filter(|$v| $v != NONE)`; //needed to fix an issue where deletions fail when finding none. If we want to thow an error on undefined this might be a good place
if (idValue) {
if (isArray(idValue)) {
return `${parentRef}[? $this.id() IN [${idValue.map((id) => `'${id}'`).join(', ')}] ]`;
}
return `${parentRef}[? $this.id() IN ['${idValue}'] ]`;
}
return parentRef;
}
if (idValue) {
if (isArray(idValue)) {
return sanitizedThings.flatMap((table: string) => idValue.map((id: string) => `${table}:⟨${id}⟩`)).join(', ');
}
return sanitizedThings.map((table: string) => `${table}:⟨${idValue}⟩`).join(', ');
}
return sanitizedThings.join(', ');
})();
const WHERE = $filter ? `WHERE ${buildSuqlFilter(parseFilter($filter, $thing, schema))}` : '';
const SET = dataFieldStrings.length > 0 ? `SET ${dataFieldStrings.join(', ')}` : '';
const OUTPUT = `VALUE (CREATE ONLY Delta SET input = ${restString}, meta = {${metaString}, "$sid": $parent.id, "$id": record::id($parent.id)}, after = $after, before = $before RETURN VALUE $parent.id )`;
const DELETE_OUTPUT = 'BEFORE';
if (['link', 'unlink', 'replace'].includes($op)) {
throw new Error("Edge ops don't belong to things");
}
if (block.$op === 'match') {
if ($tempId) {
return ''; //tempIds are already stored on their creation
}
return `LET ${VAR} = (SELECT VALUE id FROM ${TARGET} ${WHERE});`;
}
if (block.$op === 'create') {
if (isArray(idValue)) {
throw new Error('Cannot create multiple things at once');
}
const tableName = sanitizeNameSurrealDB($thing);
return `LET ${VAR} = (CREATE ONLY ${tableName}:⟨${idValue}⟩ ${SET} RETURN ${OUTPUT});`;
}
if (block.$op === 'update') {
return `LET ${VAR} = IF (${COND}) THEN (UPDATE ${TARGET} ${SET} ${WHERE} RETURN ${OUTPUT}) END;`;
}
if (block.$op === 'delete') {
return `LET ${VAR} = IF (${COND}) THEN (DELETE ${TARGET} ${WHERE} RETURN ${DELETE_OUTPUT}) END;`;
}
throw new Error(`Unsupported operation ${block.$op}`);
};
const buildEdges = (block: EnrichedBQLMutationBlock) => {
//console.log('currentEdge:', block);
const { $thing, $bzId, $op, $tempId } = block;
const currentSchema = getSchemaByThing(schema, $thing);
const { usedRoleFields } = getCurrentFields(currentSchema, block);
const VAR = `$⟨${$tempId || $bzId}⟩`;
const roleFields =
'roles' in currentSchema
? usedRoleFields.flatMap((rf) => {
const roleFieldSchema = currentSchema.roles[rf];
if (!roleFieldSchema) {
throw new Error(`Role field schema not found for ${rf}`);
}
const { cardinality } = roleFieldSchema;
const asArrayOfVars = isArray(block[rf])
? block[rf].map((node: string) => `$⟨${node}⟩`)
: [`$⟨${block[rf]}⟩`];
if (cardinality === 'ONE') {
if (asArrayOfVars.length > 1) {
//This is ok as long as only one is a match, but we can link to several in card ONE. This is practical if we don't know the $thing for instance, we can try multiple ones
const arrayString = `array::filter(array::flatten([${asArrayOfVars}]), |$v| !!$v)`;
switch ($op) {
case 'link':
case 'replace':
return `${rf} = ((array::len(${arrayString})==1) && ${arrayString}[0]) || ${arrayString}`; //todo: throw a custom error instead
case 'unlink':
return `${rf} = NONE`; //todo this is not necessarily correct if $id or $filter! Should be none only if the node has been found
default:
throw new Error(`Unsupported operation ${$op} for ONE cardinality`);
}
}
switch ($op) {
case 'link':
case 'replace':
return `${rf} = ((type::is::array(${asArrayOfVars[0]}) && array::len(${asArrayOfVars[0]})==1) && ${asArrayOfVars[0]}[0]) || ${asArrayOfVars[0]}`;
case 'unlink':
return `${rf} = NONE`; //todo this is not necessarily correct if $id or $filter! Should be none only if the node has been found
default:
throw new Error(`Unsupported operation ${$op} for ONE cardinality`);
}
}
if (cardinality === 'MANY') {
const nodesString = `array::flatten([${asArrayOfVars}])`;
switch ($op) {
case 'link':
return `${rf} += ${nodesString}`;
case 'unlink':
return `${rf} -= ${nodesString}`;
case 'replace':
return `${rf} = ${nodesString}`;
default:
throw new Error(`Unsupported operation ${$op} for MANY cardinality`);
}
}
throw new Error(`Unsupported cardinality ${cardinality}`);
})
: [];
const roleFieldsString = roleFields.length > 0 ? `${roleFields.join(', ')}` : '';
const SET = roleFieldsString ? `SET ${roleFieldsString}` : '';
return `IF ${VAR} THEN (UPDATE ${VAR} ${SET} RETURN VALUE id) END; ${VAR};`; //todo: confirm if the WHERE is actually needed here?
};
const buildArcs = (block: EnrichedBQLMutationBlock) => {
const { $thing, $op } = block;
const currentSchema = getSchemaByThing(schema, $thing) as EnrichedBormRelation;
const tableName = sanitizeNameSurrealDB($thing);
const { usedRoleFields } = getCurrentFields(currentSchema, block);
if (!['create', 'delete'].includes($op)) {
throw new Error('Arcs can only be created or deleted');
}
const [roleA, roleB] = usedRoleFields;
const thingsA = (isArray(block[roleA]) ? block[roleA] : [block[roleA]]) as string[];
const thingsB = (isArray(block[roleB]) ? block[roleB] : [block[roleB]]) as string[];
if ($op === 'create') {
if (usedRoleFields.length !== 2) {
throw new Error('Not supported: Arcs must have exactly 2 roles');
}
const rest = oFilter(block, (k: string) => !k.startsWith('$'));
const restString = JSON.stringify(rest);
const meta = oFilter(block, (k: string) => k.startsWith('$'));
const metaString = Object.entries(meta)
.map(([key, value]) => (key === '$tempId' ? `'$tempId': '_:${value}'` : `'${key}': '${value}'`))
.join(',');
const OUTPUT = `(CREATE ONLY Delta SET input = ${restString}, meta = {${metaString}, "$sid": $parent.id, "$id": record::id($parent.id)}, after = $after, before = $before RETURN VALUE $parent.id )`;
const roleOneSchema = currentSchema.roles[roleA];
const isMany1 = roleOneSchema.cardinality === 'MANY';
const roleTwoSchema = currentSchema.roles[roleB];
const isMany2 = roleTwoSchema.cardinality === 'MANY';
/*const thingsAString = thingsA.map((thingA) => `$⟨${thingA}⟩`).join(', ');
const thingsAArrayString = `array::flatten([${thingsAString}])`;
const thingsBString = thingsB.map((thingB) => `$⟨${thingB}⟩`).join(', ');
const thingsBArrayString = `array::flatten([${thingsBString}])`;
*/
/*
//this is the third version, where we only create one arc per arc defined in the flatter function. Todo: Check cardinality and throw error if it is not correct instead of the || to trigger it internally
const arc = `CREATE ONLY ${tableName} SET
${roleA} = ${isMany1 ? thingsAArrayString : `array::len(${thingsAArrayString}) == 1 && array::first(${thingsAArrayString}) || ${thingsAArrayString}`},
${roleB} = ${isMany2 ? thingsBArrayString : `array::len(${thingsBArrayString}) == 1 && array::first(${thingsBArrayString}) || ${thingsBArrayString}`}
`;
return arc;
*/
///before it was multiple arcs, running a loop over thingsA and thingsB in addition to the surrealDB loop inside the surql
const arcs = [
///This ignored cardinality and created N*M arcs. I keep it here as it could be an option in the mutation config in the future.
//
//
`
${!isMany1 ? `IF array::len(array::filter(array::flatten([$⟨${thingsA}⟩]), |$v| !!$v))>1 { THROW "[Validation] Cardinality constraint: ${roleA} is cardinality one and can link a single thing." + <string>$⟨${thingsA}⟩; };` : ''}
${!isMany2 ? `IF array::len(array::filter(array::flatten([$⟨${thingsB}⟩]), |$v| !!$v))>1 { THROW "[Validation] Cardinality constraint: ${roleB} is cardinality one and can link a single thing." + <string>$⟨${thingsB}⟩; };` : ''}
FOR $node1 IN array::flatten([$⟨${thingsA}⟩]) {
IF $node1 {
FOR $node2 IN array::flatten([$⟨${thingsB}⟩]) {
IF $node2 {
CREATE ONLY ${tableName} SET
${roleA} = ${isMany1 ? 'array::flatten([$node1])' : '$node1'},
${roleB} = ${isMany2 ? 'array::flatten([$node2])' : '$node2'}
RETURN ${OUTPUT};
}
}
}
}`,
];
///This is the throw error version that checks the cardinality, if it is ugly but it works, it ain't ugly
// `CREATE ONLY ${tableName} SET ${roleA} = ${isMany1 ? `fn::as_array($⟨${thingA}⟩)` : `array::len(fn::as_array($⟨${thingA}⟩)) == 1 && array::first(fn::as_array($⟨${thingA}⟩)) || $⟨${thingA}⟩`}, ${roleB} = ${isMany2 ? `fn::as_array($⟨${thingB}⟩)` : `array::len(fn::as_array($⟨${thingB}⟩)) == 1 && array::first(fn::as_array($⟨${thingB}⟩)) || $⟨${thingB}⟩`} RETURN ${OUTPUT};`,
//console.log('arcs', arcs);
return arcs;
}
if ($op === 'delete') {
return `DELETE FROM ${tableName} WHERE fn::as_array(${roleA}) CONTAINSANY $⟨${thingsA}⟩ AND fn::as_array(${roleB}) CONTAINSANY $⟨${thingsB}⟩ RETURN BEFORE`;
}
};
const buildReferences = (block: EnrichedBQLMutationBlock) => {
const { $thing, $bzId, $op, $tempId } = block;
const currentSchema = getSchemaByThing(schema, $thing);
const { usedRefFields } = getCurrentFields(currentSchema, block);
const VAR = `$⟨${$tempId || $bzId}⟩`;
const refFields = usedRefFields.flatMap((rf) => {
const refFieldSchema = currentSchema.refFields[rf];
if (!refFieldSchema) {
throw new Error(`ReferenceField schema not found for ${rf}`);
}
const { cardinality, contentType } = refFieldSchema;
if (contentType === 'REF') {
const asArrayOfVars = isArray(block[rf]) ? block[rf] : [`${block[rf]}`];
if (cardinality === 'ONE') {
if (asArrayOfVars.length > 1) {
//This is ok as long as only one is a match, but we can link to several in card ONE. This is practical if we don't know the $thing for instance, we can try multiple ones
const arrayString = `array::filter(array::flatten([${asArrayOfVars}]), |$v| !!$v)`;
switch ($op) {
case 'link':
case 'replace':
return `${rf} = ((array::len(${arrayString})==1) && ${arrayString}[0]) || ${arrayString}`; //todo: throw a custom error instead
case 'unlink':
return `${rf} = NONE`; //todo this is not necessarily correct if $id or $filter! Should be none only if the node has been found
default:
throw new Error(`Unsupported operation ${$op} for ONE cardinality`);
}
}
switch ($op) {
case 'link':
case 'replace':
return `${rf} = ((type::is::array(${asArrayOfVars[0]}) && array::len(${asArrayOfVars[0]})==1) && ${asArrayOfVars[0]}[0]) || ${asArrayOfVars[0]}`;
case 'unlink':
return `${rf} = NONE`; //todo this is not necessarily correct if $id or $filter! Should be none only if the node has been found
default:
throw new Error(`Unsupported operation ${$op} for ONE cardinality`);
}
}
if (cardinality === 'MANY') {
const nodesString = `array::flatten([${asArrayOfVars}])`;
switch ($op) {
case 'link':
return `${rf} += ${nodesString}`;
case 'unlink':
return `${rf} -= ${nodesString}`;
case 'replace':
return `${rf} = ${nodesString}`;
default:
throw new Error(`Unsupported operation ${$op} for MANY cardinality`);
}
}
throw new Error(`Unsupported cardinality ${cardinality}`);
}
if (contentType === 'FLEX') {
//todo: card one check len 1
//todo: add/remove etc
return `${rf} = ${cardinality === 'ONE' ? `array::flatten([${block[rf]}])[0]` : `array::flatten([${block[rf]}])`}`;
}
});
const refFieldsString = refFields.length > 0 ? `${refFields.join(', ')}` : '';
const SET = refFieldsString ? `SET ${refFieldsString}` : '';
return `IF ${VAR} THEN (UPDATE ${VAR} ${SET} RETURN VALUE id) END; ${VAR};`;
};
const result = [
...flat.things.map(buildThings),
...flat.edges.map(buildEdges),
...flat.arcs.flatMap(buildArcs),
...flat.references.map(buildReferences),
];
//console.log('builtMutation', result);
return result;
};