@ronin/compiler 0.10.0 → 0.10.1

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 +42 -28
  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: {
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",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {