@ronin/compiler 0.14.2 → 0.14.3-leo-ron-1099-1-experimental-322
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +60 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -26,6 +26,10 @@ var CURRENT_TIME_EXPRESSION = {
|
|
26
26
|
var composeIncludedTableAlias = (fieldSlug) => {
|
27
27
|
return `including_${fieldSlug}`;
|
28
28
|
};
|
29
|
+
var MOUNTING_PATH_SUFFIX = /(.*?)(\{(\d+)\})?$/;
|
30
|
+
var composeMountingPath = (input) => {
|
31
|
+
return input.replace(MOUNTING_PATH_SUFFIX, (_, p, __, n) => `${p}{${n ? +n + 1 : 1}}`);
|
32
|
+
};
|
29
33
|
var MODEL_ENTITY_ERROR_CODES = {
|
30
34
|
field: "FIELD_NOT_FOUND",
|
31
35
|
index: "INDEX_NOT_FOUND",
|
@@ -299,7 +303,7 @@ var handleFor = (model, instructions) => {
|
|
299
303
|
};
|
300
304
|
|
301
305
|
// src/instructions/including.ts
|
302
|
-
var handleIncluding = (models, model, statementParams, single, instruction) => {
|
306
|
+
var handleIncluding = (models, model, statementParams, single, instruction, options = {}) => {
|
303
307
|
let statement = "";
|
304
308
|
let tableSubQuery;
|
305
309
|
for (const ephemeralFieldSlug in instruction) {
|
@@ -311,8 +315,9 @@ var handleIncluding = (models, model, statementParams, single, instruction) => {
|
|
311
315
|
const relatedModel = getModelBySlug(models, queryModel);
|
312
316
|
let joinType = "LEFT";
|
313
317
|
let relatedTableSelector = `"${relatedModel.table}"`;
|
314
|
-
const tableAlias = composeIncludedTableAlias(ephemeralFieldSlug);
|
315
318
|
const subSingle = queryModel !== relatedModel.pluralSlug;
|
319
|
+
const subMountingPath = ephemeralFieldSlug === "ronin_root" ? options.mountingPath ? composeMountingPath(options.mountingPath) : void 0 : `${options?.mountingPath ? `${options?.mountingPath}.` : ""}${subSingle ? ephemeralFieldSlug : `${ephemeralFieldSlug}[0]`}`;
|
320
|
+
const tableAlias = composeIncludedTableAlias(subMountingPath || ephemeralFieldSlug);
|
316
321
|
if (!modifiableQueryInstructions?.with) {
|
317
322
|
joinType = "CROSS";
|
318
323
|
if (subSingle) {
|
@@ -332,7 +337,7 @@ var handleIncluding = (models, model, statementParams, single, instruction) => {
|
|
332
337
|
);
|
333
338
|
relatedTableSelector = `(${subSelect.main.statement})`;
|
334
339
|
}
|
335
|
-
statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;
|
340
|
+
statement += `${joinType} JOIN ${relatedTableSelector} as "${tableAlias}"`;
|
336
341
|
model.tableAlias = model.tableAlias || model.table;
|
337
342
|
if (joinType === "LEFT") {
|
338
343
|
const subStatement = composeConditions(
|
@@ -356,7 +361,8 @@ var handleIncluding = (models, model, statementParams, single, instruction) => {
|
|
356
361
|
{ ...relatedModel, tableAlias },
|
357
362
|
statementParams,
|
358
363
|
subSingle,
|
359
|
-
modifiableQueryInstructions.including
|
364
|
+
modifiableQueryInstructions.including,
|
365
|
+
{ mountingPath: subMountingPath }
|
360
366
|
);
|
361
367
|
statement += ` ${subIncluding.statement}`;
|
362
368
|
}
|
@@ -403,15 +409,13 @@ var handleOrderedBy = (model, instruction) => {
|
|
403
409
|
// src/instructions/selecting.ts
|
404
410
|
var handleSelecting = (models, model, statementParams, single, instructions, options = {}) => {
|
405
411
|
let isJoining = false;
|
406
|
-
const selectedFields = (
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
return field;
|
411
|
-
}) : model.fields).filter((field) => !(field.type === "link" && field.kind === "many")).map((field) => {
|
412
|
+
const selectedFields = filterSelectedFields(
|
413
|
+
model,
|
414
|
+
instructions.selecting
|
415
|
+
).filter((field) => !(field.type === "link" && field.kind === "many")).map((field) => {
|
412
416
|
const newField = { ...field, mountingPath: field.slug };
|
413
417
|
if (options.mountingPath) {
|
414
|
-
newField.mountingPath = `${options.mountingPath}.${field.slug}`;
|
418
|
+
newField.mountingPath = `${options.mountingPath.replace(/\{\d+\}/g, "")}.${field.slug}`;
|
415
419
|
}
|
416
420
|
return newField;
|
417
421
|
});
|
@@ -432,11 +436,11 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
|
|
432
436
|
const subQueryModel = getModelBySlug(models, queryModel);
|
433
437
|
isJoining = true;
|
434
438
|
if (queryInstructions?.selecting) options.expandColumns = true;
|
435
|
-
const tableAlias = composeIncludedTableAlias(key);
|
436
439
|
const subSingle = queryModel !== subQueryModel.pluralSlug;
|
437
440
|
if (!model.tableAlias)
|
438
441
|
model.tableAlias = single && !subSingle ? `sub_${model.table}` : model.table;
|
439
|
-
const subMountingPath = key === "ronin_root" ? options.mountingPath : `${options?.mountingPath ? `${options?.mountingPath}.` : ""}${subSingle ? key : `${key}[0]`}`;
|
442
|
+
const subMountingPath = key === "ronin_root" ? options.mountingPath ? composeMountingPath(options.mountingPath) : void 0 : `${options?.mountingPath ? `${options?.mountingPath}.` : ""}${subSingle ? key : `${key}[0]`}`;
|
443
|
+
const tableAlias = composeIncludedTableAlias(subMountingPath || key);
|
440
444
|
const { columns: nestedColumns, selectedFields: nestedSelectedFields } = handleSelecting(
|
441
445
|
models,
|
442
446
|
{ ...subQueryModel, tableAlias },
|
@@ -508,15 +512,12 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
|
|
508
512
|
if (symbol?.type === "query") {
|
509
513
|
const { queryModel: subQueryModelSlug, queryInstructions: subQueryInstructions } = splitQuery(symbol.value);
|
510
514
|
const subQueryModel = getModelBySlug(models, subQueryModelSlug);
|
511
|
-
if (subQueryInstructions?.selecting) {
|
512
|
-
const currentFields = new Set(subQueryInstructions.selecting);
|
513
|
-
currentFields.add("id");
|
514
|
-
subQueryInstructions.selecting = Array.from(currentFields);
|
515
|
-
}
|
516
515
|
const subQuerySelectedFields = subQueryInstructions?.selecting;
|
517
516
|
const subQueryIncludedFields = subQueryInstructions?.including;
|
518
517
|
const subQueryFields = [
|
519
|
-
...
|
518
|
+
...filterSelectedFields(subQueryModel, subQuerySelectedFields).map(
|
519
|
+
(field) => field.slug
|
520
|
+
),
|
520
521
|
...subQueryIncludedFields ? Object.keys(
|
521
522
|
flatten(subQueryIncludedFields || {})
|
522
523
|
) : []
|
@@ -762,6 +763,32 @@ var replaceJSON = (key, value) => {
|
|
762
763
|
if (key === QUERY_SYMBOLS.EXPRESSION) return value.replaceAll(`'`, `''`);
|
763
764
|
return value;
|
764
765
|
};
|
766
|
+
var matchSelectedFields = (fields, pattern) => {
|
767
|
+
let regexStr = pattern.replace(/\./g, "\\.");
|
768
|
+
regexStr = regexStr.replace(/\*\*/g, "<<DOUBLESTAR>>");
|
769
|
+
regexStr = regexStr.replace(/\*/g, "[^.]*");
|
770
|
+
regexStr = regexStr.replace(/<<DOUBLESTAR>>/g, ".*");
|
771
|
+
const regex2 = new RegExp(`^${regexStr}$`);
|
772
|
+
return fields.filter((field) => regex2.test(field.slug));
|
773
|
+
};
|
774
|
+
var filterSelectedFields = (model, instruction) => {
|
775
|
+
if (!instruction) return model.fields;
|
776
|
+
let selectedFields = [];
|
777
|
+
for (const pattern of instruction) {
|
778
|
+
const isNegative = pattern.startsWith("!");
|
779
|
+
const cleanPattern = isNegative ? pattern.slice(1) : pattern;
|
780
|
+
const matchedFields = matchSelectedFields(
|
781
|
+
isNegative ? selectedFields : model.fields,
|
782
|
+
cleanPattern
|
783
|
+
);
|
784
|
+
if (isNegative) {
|
785
|
+
selectedFields = selectedFields.filter((field) => !matchedFields.includes(field));
|
786
|
+
} else {
|
787
|
+
selectedFields.push(...matchedFields);
|
788
|
+
}
|
789
|
+
}
|
790
|
+
return selectedFields;
|
791
|
+
};
|
765
792
|
var prepareStatementValue = (statementParams, value) => {
|
766
793
|
if (value === null) return "NULL";
|
767
794
|
if (!statementParams) {
|
@@ -1261,7 +1288,8 @@ var addDefaultModelPresets = (list, model) => {
|
|
1261
1288
|
}
|
1262
1289
|
}
|
1263
1290
|
}
|
1264
|
-
}
|
1291
|
+
},
|
1292
|
+
selecting: ["**", "!source", "!target"]
|
1265
1293
|
}
|
1266
1294
|
}
|
1267
1295
|
}
|
@@ -2000,7 +2028,18 @@ var Transaction = class {
|
|
2000
2028
|
for (const arrayField of joinFields.values()) {
|
2001
2029
|
const currentValue = existingRecord[arrayField];
|
2002
2030
|
const newValue = record[arrayField];
|
2003
|
-
|
2031
|
+
for (const newRecord of newValue) {
|
2032
|
+
if ("id" in newRecord) {
|
2033
|
+
const existingIndex = currentValue.findIndex((value) => {
|
2034
|
+
return value.id === newRecord.id;
|
2035
|
+
});
|
2036
|
+
if (existingIndex > -1) {
|
2037
|
+
Object.assign(currentValue[existingIndex], newRecord);
|
2038
|
+
continue;
|
2039
|
+
}
|
2040
|
+
}
|
2041
|
+
currentValue.push(newRecord);
|
2042
|
+
}
|
2004
2043
|
}
|
2005
2044
|
}
|
2006
2045
|
return single ? records[0] : records;
|