@ronin/compiler 0.10.0 → 0.10.1-leo-ron-1083-experimental-208

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/dist/index.js +73 -31
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -648,11 +648,15 @@ var formatModelEntity = (type, entities) => {
648
648
  });
649
649
  return entries ? Object.fromEntries(entries) : void 0;
650
650
  };
651
- var composeSystemModelStatement = (models, dependencyStatements, action, systemModel) => {
651
+ var handleSystemModel = (models, dependencyStatements, action, systemModel, newModel) => {
652
652
  const { system: _, ...systemModelClean } = systemModel;
653
653
  const query = {
654
654
  [action]: { model: action === "create" ? systemModelClean : systemModelClean.slug }
655
655
  };
656
+ if (action === "alter" && newModel) {
657
+ const { system: _2, ...newModelClean } = newModel;
658
+ query.alter.to = newModelClean;
659
+ }
656
660
  const statement = compileQueryInput(query, models, []);
657
661
  dependencyStatements.push(...statement.dependencies);
658
662
  };
@@ -712,12 +716,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
712
716
  }
713
717
  queryTypeDetails = { to: finalModel };
714
718
  getSystemModels(models, modelWithPresets).map((systemModel) => {
715
- return composeSystemModelStatement(
716
- models,
717
- dependencyStatements,
718
- "create",
719
- systemModel
720
- );
719
+ return handleSystemModel(models, dependencyStatements, "create", systemModel);
721
720
  });
722
721
  }
723
722
  if (action === "alter" && model) {
@@ -744,12 +743,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
744
743
  dependencyStatements.push({ statement: `DROP TABLE "${model.table}"`, params: [] });
745
744
  queryTypeDetails = { with: { slug } };
746
745
  models.filter(({ system }) => system?.model === model.slug).map((systemModel) => {
747
- return composeSystemModelStatement(
748
- models,
749
- dependencyStatements,
750
- "drop",
751
- systemModel
752
- );
746
+ return handleSystemModel(models, dependencyStatements, "drop", systemModel);
753
747
  });
754
748
  }
755
749
  const queryTypeAction = action === "create" ? "add" : action === "alter" ? "set" : "remove";
@@ -759,6 +753,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
759
753
  }
760
754
  };
761
755
  }
756
+ const modelBeforeUpdate = structuredClone(model);
762
757
  const existingModel = model;
763
758
  const pluralType = PLURAL_MODEL_ENTITIES[entity];
764
759
  const targetEntityIndex = existingModel[pluralType]?.findIndex(
@@ -773,6 +768,8 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
773
768
  const existingEntity = existingModel[pluralType]?.[targetEntityIndex];
774
769
  if (entity === "field") {
775
770
  const statement = `ALTER TABLE "${existingModel.table}"`;
771
+ const existingField = existingEntity;
772
+ const existingLinkField = existingField?.type === "link" && existingField.kind === "many";
776
773
  if (action === "create") {
777
774
  const field2 = jsonValue;
778
775
  field2.type = field2.type || "string";
@@ -785,20 +782,17 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
785
782
  }
786
783
  } else if (action === "alter") {
787
784
  const newSlug = jsonValue?.slug;
788
- if (newSlug) {
785
+ if (newSlug && !existingLinkField) {
789
786
  dependencyStatements.push({
790
787
  statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`,
791
788
  params: []
792
789
  });
793
790
  }
794
- } else if (action === "drop") {
795
- const existingField = existingEntity;
796
- if (!(existingField.type === "link" && existingField.kind === "many")) {
797
- dependencyStatements.push({
798
- statement: `${statement} DROP COLUMN "${slug}"`,
799
- params: []
800
- });
801
- }
791
+ } else if (action === "drop" && !existingLinkField) {
792
+ dependencyStatements.push({
793
+ statement: `${statement} DROP COLUMN "${slug}"`,
794
+ params: []
795
+ });
802
796
  }
803
797
  }
804
798
  const statementAction = action.toUpperCase();
@@ -890,7 +884,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
890
884
  const value = prepareStatementValue(statementParams, jsonValue);
891
885
  json = `json_set(${field}, '$.${slug}', json_patch(json_extract(${field}, '$.${slug}'), ${value}))`;
892
886
  const targetEntity = existingModel[pluralType];
893
- targetEntity[targetEntityIndex] = jsonValue;
887
+ Object.assign(targetEntity[targetEntityIndex], jsonValue);
894
888
  break;
895
889
  }
896
890
  case "drop": {
@@ -903,15 +897,35 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
903
897
  return system?.model === existingModel.slug;
904
898
  });
905
899
  const newSystemModels = getSystemModels(models, existingModel);
900
+ const matchSystemModels = (oldSystemModel, newSystemModel) => {
901
+ const conditions = [
902
+ oldSystemModel.system?.model === newSystemModel.system?.model
903
+ ];
904
+ if (oldSystemModel.system?.associationSlug) {
905
+ const oldFieldIndex = modelBeforeUpdate?.fields.findIndex((item) => {
906
+ return item.slug === newSystemModel.system?.associationSlug;
907
+ });
908
+ const newFieldIndex = existingModel.fields.findIndex((item) => {
909
+ return item.slug === oldSystemModel.system?.associationSlug;
910
+ });
911
+ conditions.push(oldFieldIndex === newFieldIndex);
912
+ }
913
+ return conditions.every((condition) => condition === true);
914
+ };
906
915
  for (const systemModel of currentSystemModels) {
907
- const exists = newSystemModels.find((model2) => model2.slug === systemModel.slug);
908
- if (exists) continue;
909
- composeSystemModelStatement(models, dependencyStatements, "drop", systemModel);
916
+ const exists = newSystemModels.find(matchSystemModels.bind(null, systemModel));
917
+ if (exists) {
918
+ if (exists.slug !== systemModel.slug) {
919
+ handleSystemModel(models, dependencyStatements, "alter", systemModel, exists);
920
+ }
921
+ continue;
922
+ }
923
+ handleSystemModel(models, dependencyStatements, "drop", systemModel);
910
924
  }
911
925
  for (const systemModel of newSystemModels) {
912
- const exists = currentSystemModels.find((model2) => model2.slug === systemModel.slug);
926
+ const exists = currentSystemModels.find(matchSystemModels.bind(null, systemModel));
913
927
  if (exists) continue;
914
- composeSystemModelStatement(models, dependencyStatements, "create", systemModel);
928
+ handleSystemModel(models, dependencyStatements, "create", systemModel);
915
929
  }
916
930
  return {
917
931
  set: {
@@ -1198,6 +1212,11 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
1198
1212
  if (symbol?.type === "query") {
1199
1213
  let { queryModel: subQueryModelSlug, queryInstructions: subQueryInstructions } = splitQuery(symbol.value);
1200
1214
  const subQueryModel = getModelBySlug(models, subQueryModelSlug);
1215
+ if (subQueryInstructions?.selecting) {
1216
+ const currentFields = new Set(subQueryInstructions.selecting);
1217
+ currentFields.add("id");
1218
+ subQueryInstructions.selecting = Array.from(currentFields);
1219
+ }
1201
1220
  const subQuerySelectedFields = subQueryInstructions?.selecting;
1202
1221
  const subQueryIncludedFields = subQueryInstructions?.including;
1203
1222
  const subQueryFields = [
@@ -1220,7 +1239,19 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
1220
1239
  ...subQueryInstructions.including
1221
1240
  };
1222
1241
  }
1223
- return compileQueryInput(symbol.value, models, statementParams).main.statement;
1242
+ let statement2 = "";
1243
+ if (subQuerySelectedFields) {
1244
+ const selectedFields = [
1245
+ ...subQueryFields,
1246
+ ...defaultFieldsToAdd.map(([key]) => key)
1247
+ ];
1248
+ const columns = selectedFields.map((field) => {
1249
+ return getFieldFromModel(model, field, "to").fieldSelector;
1250
+ });
1251
+ statement2 = `(${columns.join(", ")}) `;
1252
+ }
1253
+ statement2 += compileQueryInput(symbol.value, models, statementParams).main.statement;
1254
+ return statement2;
1224
1255
  }
1225
1256
  Object.assign(toInstruction, defaultFields);
1226
1257
  for (const fieldSlug in toInstruction) {
@@ -1484,11 +1515,22 @@ var Transaction = class {
1484
1515
  return expand(formattedRecord);
1485
1516
  }
1486
1517
  prepareResults(results) {
1487
- return results.map((result, index) => {
1518
+ const relevantResults = results.filter((_, index) => {
1519
+ return this.statements[index].returning;
1520
+ });
1521
+ return relevantResults.map((result, index) => {
1488
1522
  const query = this.queries.at(-index);
1489
1523
  const { queryModel } = splitQuery(query);
1490
1524
  const model = getModelBySlug(this.models, queryModel);
1491
- return { record: this.formatRecord(model, result[0]) };
1525
+ const single = queryModel !== model.pluralSlug;
1526
+ if (single) {
1527
+ return { record: this.formatRecord(model, result[0]) };
1528
+ }
1529
+ return {
1530
+ records: result.map((resultItem) => {
1531
+ return this.formatRecord(model, resultItem);
1532
+ })
1533
+ };
1492
1534
  });
1493
1535
  }
1494
1536
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.10.0",
3
+ "version": "0.10.1-leo-ron-1083-experimental-208",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {