@ronin/compiler 0.10.0 → 0.10.1-leo-ron-1083-experimental-209
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 +115 -33
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -102,6 +102,9 @@ var expand = (obj) => {
|
|
102
102
|
return res;
|
103
103
|
}, {});
|
104
104
|
};
|
105
|
+
var getProperty = (obj, path) => {
|
106
|
+
return path.split(".").reduce((acc, key) => acc?.[key], obj);
|
107
|
+
};
|
105
108
|
var splitQuery = (query) => {
|
106
109
|
const queryType = Object.keys(query)[0];
|
107
110
|
const queryModel = Object.keys(query[queryType])[0];
|
@@ -648,11 +651,15 @@ var formatModelEntity = (type, entities) => {
|
|
648
651
|
});
|
649
652
|
return entries ? Object.fromEntries(entries) : void 0;
|
650
653
|
};
|
651
|
-
var
|
654
|
+
var handleSystemModel = (models, dependencyStatements, action, systemModel, newModel) => {
|
652
655
|
const { system: _, ...systemModelClean } = systemModel;
|
653
656
|
const query = {
|
654
657
|
[action]: { model: action === "create" ? systemModelClean : systemModelClean.slug }
|
655
658
|
};
|
659
|
+
if (action === "alter" && newModel) {
|
660
|
+
const { system: _2, ...newModelClean } = newModel;
|
661
|
+
query.alter.to = newModelClean;
|
662
|
+
}
|
656
663
|
const statement = compileQueryInput(query, models, []);
|
657
664
|
dependencyStatements.push(...statement.dependencies);
|
658
665
|
};
|
@@ -712,12 +719,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
712
719
|
}
|
713
720
|
queryTypeDetails = { to: finalModel };
|
714
721
|
getSystemModels(models, modelWithPresets).map((systemModel) => {
|
715
|
-
return
|
716
|
-
models,
|
717
|
-
dependencyStatements,
|
718
|
-
"create",
|
719
|
-
systemModel
|
720
|
-
);
|
722
|
+
return handleSystemModel(models, dependencyStatements, "create", systemModel);
|
721
723
|
});
|
722
724
|
}
|
723
725
|
if (action === "alter" && model) {
|
@@ -744,12 +746,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
744
746
|
dependencyStatements.push({ statement: `DROP TABLE "${model.table}"`, params: [] });
|
745
747
|
queryTypeDetails = { with: { slug } };
|
746
748
|
models.filter(({ system }) => system?.model === model.slug).map((systemModel) => {
|
747
|
-
return
|
748
|
-
models,
|
749
|
-
dependencyStatements,
|
750
|
-
"drop",
|
751
|
-
systemModel
|
752
|
-
);
|
749
|
+
return handleSystemModel(models, dependencyStatements, "drop", systemModel);
|
753
750
|
});
|
754
751
|
}
|
755
752
|
const queryTypeAction = action === "create" ? "add" : action === "alter" ? "set" : "remove";
|
@@ -759,6 +756,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
759
756
|
}
|
760
757
|
};
|
761
758
|
}
|
759
|
+
const modelBeforeUpdate = structuredClone(model);
|
762
760
|
const existingModel = model;
|
763
761
|
const pluralType = PLURAL_MODEL_ENTITIES[entity];
|
764
762
|
const targetEntityIndex = existingModel[pluralType]?.findIndex(
|
@@ -773,6 +771,8 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
773
771
|
const existingEntity = existingModel[pluralType]?.[targetEntityIndex];
|
774
772
|
if (entity === "field") {
|
775
773
|
const statement = `ALTER TABLE "${existingModel.table}"`;
|
774
|
+
const existingField = existingEntity;
|
775
|
+
const existingLinkField = existingField?.type === "link" && existingField.kind === "many";
|
776
776
|
if (action === "create") {
|
777
777
|
const field2 = jsonValue;
|
778
778
|
field2.type = field2.type || "string";
|
@@ -785,20 +785,17 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
785
785
|
}
|
786
786
|
} else if (action === "alter") {
|
787
787
|
const newSlug = jsonValue?.slug;
|
788
|
-
if (newSlug) {
|
788
|
+
if (newSlug && !existingLinkField) {
|
789
789
|
dependencyStatements.push({
|
790
790
|
statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`,
|
791
791
|
params: []
|
792
792
|
});
|
793
793
|
}
|
794
|
-
} else if (action === "drop") {
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
params: []
|
800
|
-
});
|
801
|
-
}
|
794
|
+
} else if (action === "drop" && !existingLinkField) {
|
795
|
+
dependencyStatements.push({
|
796
|
+
statement: `${statement} DROP COLUMN "${slug}"`,
|
797
|
+
params: []
|
798
|
+
});
|
802
799
|
}
|
803
800
|
}
|
804
801
|
const statementAction = action.toUpperCase();
|
@@ -890,7 +887,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
890
887
|
const value = prepareStatementValue(statementParams, jsonValue);
|
891
888
|
json = `json_set(${field}, '$.${slug}', json_patch(json_extract(${field}, '$.${slug}'), ${value}))`;
|
892
889
|
const targetEntity = existingModel[pluralType];
|
893
|
-
targetEntity[targetEntityIndex]
|
890
|
+
Object.assign(targetEntity[targetEntityIndex], jsonValue);
|
894
891
|
break;
|
895
892
|
}
|
896
893
|
case "drop": {
|
@@ -903,15 +900,35 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
903
900
|
return system?.model === existingModel.slug;
|
904
901
|
});
|
905
902
|
const newSystemModels = getSystemModels(models, existingModel);
|
903
|
+
const matchSystemModels = (oldSystemModel, newSystemModel) => {
|
904
|
+
const conditions = [
|
905
|
+
oldSystemModel.system?.model === newSystemModel.system?.model
|
906
|
+
];
|
907
|
+
if (oldSystemModel.system?.associationSlug) {
|
908
|
+
const oldFieldIndex = modelBeforeUpdate?.fields.findIndex((item) => {
|
909
|
+
return item.slug === newSystemModel.system?.associationSlug;
|
910
|
+
});
|
911
|
+
const newFieldIndex = existingModel.fields.findIndex((item) => {
|
912
|
+
return item.slug === oldSystemModel.system?.associationSlug;
|
913
|
+
});
|
914
|
+
conditions.push(oldFieldIndex === newFieldIndex);
|
915
|
+
}
|
916
|
+
return conditions.every((condition) => condition === true);
|
917
|
+
};
|
906
918
|
for (const systemModel of currentSystemModels) {
|
907
|
-
const exists = newSystemModels.find((
|
908
|
-
if (exists)
|
909
|
-
|
919
|
+
const exists = newSystemModels.find(matchSystemModels.bind(null, systemModel));
|
920
|
+
if (exists) {
|
921
|
+
if (exists.slug !== systemModel.slug) {
|
922
|
+
handleSystemModel(models, dependencyStatements, "alter", systemModel, exists);
|
923
|
+
}
|
924
|
+
continue;
|
925
|
+
}
|
926
|
+
handleSystemModel(models, dependencyStatements, "drop", systemModel);
|
910
927
|
}
|
911
928
|
for (const systemModel of newSystemModels) {
|
912
|
-
const exists = currentSystemModels.find((
|
929
|
+
const exists = currentSystemModels.find(matchSystemModels.bind(null, systemModel));
|
913
930
|
if (exists) continue;
|
914
|
-
|
931
|
+
handleSystemModel(models, dependencyStatements, "create", systemModel);
|
915
932
|
}
|
916
933
|
return {
|
917
934
|
set: {
|
@@ -925,9 +942,24 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
925
942
|
};
|
926
943
|
};
|
927
944
|
|
928
|
-
// src/
|
945
|
+
// src/utils/pagination.ts
|
929
946
|
var CURSOR_SEPARATOR = ",";
|
930
947
|
var CURSOR_NULL_PLACEHOLDER = "RONIN_NULL";
|
948
|
+
var generatePaginationCursor = (model, orderedBy, record) => {
|
949
|
+
const { ascending = [], descending = [] } = orderedBy || {};
|
950
|
+
const keys = [...ascending, ...descending];
|
951
|
+
if (keys.length === 0) keys.push("ronin.createdAt");
|
952
|
+
const cursors = keys.map((fieldSlug) => {
|
953
|
+
const property = getProperty(record, fieldSlug);
|
954
|
+
if (property === null || property === void 0) return CURSOR_NULL_PLACEHOLDER;
|
955
|
+
const { field } = getFieldFromModel(model, fieldSlug, "orderedBy");
|
956
|
+
if (field.type === "date") return new Date(property).getTime();
|
957
|
+
return property;
|
958
|
+
});
|
959
|
+
return cursors.map((cursor) => encodeURIComponent(String(cursor))).join(CURSOR_SEPARATOR);
|
960
|
+
};
|
961
|
+
|
962
|
+
// src/instructions/before-after.ts
|
931
963
|
var handleBeforeOrAfter = (model, statementParams, instructions) => {
|
932
964
|
if (!(instructions.before || instructions.after)) {
|
933
965
|
throw new RoninError({
|
@@ -1198,6 +1230,11 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
|
|
1198
1230
|
if (symbol?.type === "query") {
|
1199
1231
|
let { queryModel: subQueryModelSlug, queryInstructions: subQueryInstructions } = splitQuery(symbol.value);
|
1200
1232
|
const subQueryModel = getModelBySlug(models, subQueryModelSlug);
|
1233
|
+
if (subQueryInstructions?.selecting) {
|
1234
|
+
const currentFields = new Set(subQueryInstructions.selecting);
|
1235
|
+
currentFields.add("id");
|
1236
|
+
subQueryInstructions.selecting = Array.from(currentFields);
|
1237
|
+
}
|
1201
1238
|
const subQuerySelectedFields = subQueryInstructions?.selecting;
|
1202
1239
|
const subQueryIncludedFields = subQueryInstructions?.including;
|
1203
1240
|
const subQueryFields = [
|
@@ -1220,7 +1257,19 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
|
|
1220
1257
|
...subQueryInstructions.including
|
1221
1258
|
};
|
1222
1259
|
}
|
1223
|
-
|
1260
|
+
let statement2 = "";
|
1261
|
+
if (subQuerySelectedFields) {
|
1262
|
+
const selectedFields = [
|
1263
|
+
...subQueryFields,
|
1264
|
+
...defaultFieldsToAdd.map(([key]) => key)
|
1265
|
+
];
|
1266
|
+
const columns = selectedFields.map((field) => {
|
1267
|
+
return getFieldFromModel(model, field, "to").fieldSelector;
|
1268
|
+
});
|
1269
|
+
statement2 = `(${columns.join(", ")}) `;
|
1270
|
+
}
|
1271
|
+
statement2 += compileQueryInput(symbol.value, models, statementParams).main.statement;
|
1272
|
+
return statement2;
|
1224
1273
|
}
|
1225
1274
|
Object.assign(toInstruction, defaultFields);
|
1226
1275
|
for (const fieldSlug in toInstruction) {
|
@@ -1484,11 +1533,44 @@ var Transaction = class {
|
|
1484
1533
|
return expand(formattedRecord);
|
1485
1534
|
}
|
1486
1535
|
prepareResults(results) {
|
1487
|
-
|
1536
|
+
const relevantResults = results.filter((_, index) => {
|
1537
|
+
return this.statements[index].returning;
|
1538
|
+
});
|
1539
|
+
return relevantResults.map((result, index) => {
|
1488
1540
|
const query = this.queries.at(-index);
|
1489
|
-
const { queryModel } = splitQuery(query);
|
1541
|
+
const { queryModel, queryInstructions } = splitQuery(query);
|
1490
1542
|
const model = getModelBySlug(this.models, queryModel);
|
1491
|
-
|
1543
|
+
const single = queryModel !== model.pluralSlug;
|
1544
|
+
if (single) {
|
1545
|
+
return { record: this.formatRecord(model, result[0]) };
|
1546
|
+
}
|
1547
|
+
const pageSize = queryInstructions?.limitedTo;
|
1548
|
+
const output = {
|
1549
|
+
records: result.map((resultItem) => {
|
1550
|
+
return this.formatRecord(model, resultItem);
|
1551
|
+
})
|
1552
|
+
};
|
1553
|
+
if (pageSize && output.records.length > 0) {
|
1554
|
+
if (queryInstructions?.before || queryInstructions?.after) {
|
1555
|
+
const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
|
1556
|
+
const firstRecord = output.records[0];
|
1557
|
+
output[direction] = generatePaginationCursor(
|
1558
|
+
model,
|
1559
|
+
queryInstructions.orderedBy,
|
1560
|
+
firstRecord
|
1561
|
+
);
|
1562
|
+
}
|
1563
|
+
if (output.records.length > pageSize) {
|
1564
|
+
const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
|
1565
|
+
const lastRecord = output.records.pop();
|
1566
|
+
output[direction] = generatePaginationCursor(
|
1567
|
+
model,
|
1568
|
+
queryInstructions.orderedBy,
|
1569
|
+
lastRecord
|
1570
|
+
);
|
1571
|
+
}
|
1572
|
+
}
|
1573
|
+
return output;
|
1492
1574
|
});
|
1493
1575
|
}
|
1494
1576
|
};
|