@ronin/compiler 0.9.0-leo-ron-1083-experimental-201 → 0.9.0-leo-ron-1083-experimental-203

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.d.ts CHANGED
@@ -5919,6 +5919,10 @@ type ModelTriggerField<T extends Array<ModelField> = Array<ModelField>> = {
5919
5919
  slug: T[number]['slug'];
5920
5920
  };
5921
5921
  type ModelTrigger<T extends Array<ModelField> = Array<ModelField>> = {
5922
+ /**
5923
+ * The identifier of the index.
5924
+ */
5925
+ slug?: string;
5922
5926
  /** The type of query for which the trigger should fire. */
5923
5927
  action: 'INSERT' | 'UPDATE' | 'DELETE';
5924
5928
  /** When the trigger should fire in the case that a matching query is executed. */
package/dist/index.js CHANGED
@@ -20,6 +20,12 @@ var RONIN_MODEL_FIELD_REGEX = new RegExp(
20
20
  `${RONIN_MODEL_SYMBOLS.FIELD}[_a-zA-Z0-9]+`,
21
21
  "g"
22
22
  );
23
+ var MODEL_ENTITY_ERROR_CODES = {
24
+ field: "FIELD_NOT_FOUND",
25
+ index: "INDEX_NOT_FOUND",
26
+ trigger: "TRIGGER_NOT_FOUND",
27
+ preset: "PRESET_NOT_FOUND"
28
+ };
23
29
  var RoninError = class extends Error {
24
30
  code;
25
31
  field;
@@ -617,6 +623,7 @@ var getFieldStatement = (models, model, field) => {
617
623
  statement += ` GENERATED ALWAYS AS (${parseFieldExpression(model, "to", symbol?.value)}) ${kind}`;
618
624
  }
619
625
  if (field.type === "link") {
626
+ if (field.kind === "many") return null;
620
627
  const actions = field.actions || {};
621
628
  const targetTable = getModelBySlug(models, field.target).table;
622
629
  statement += ` REFERENCES ${targetTable}("id")`;
@@ -729,14 +736,18 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
729
736
  }
730
737
  };
731
738
  }
732
- if (entity === "field" && model) {
739
+ const existingModel = model;
740
+ if (entity === "field") {
733
741
  if (action === "create") {
734
742
  const field2 = jsonValue;
735
743
  field2.type = field2.type || "string";
736
- dependencyStatements.push({
737
- statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field2)}`,
738
- params: []
739
- });
744
+ const fieldStatement = getFieldStatement(models, existingModel, field2);
745
+ if (fieldStatement) {
746
+ dependencyStatements.push({
747
+ statement: `${statement} ADD COLUMN ${fieldStatement}`,
748
+ params: []
749
+ });
750
+ }
740
751
  } else if (action === "alter") {
741
752
  const newSlug = jsonValue?.slug;
742
753
  if (newSlug) {
@@ -752,7 +763,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
752
763
  });
753
764
  }
754
765
  }
755
- if (entity === "index" && model) {
766
+ if (entity === "index") {
756
767
  const index = jsonValue;
757
768
  const indexName = convertToSnakeCase(slug);
758
769
  const params = [];
@@ -761,9 +772,9 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
761
772
  const columns = index.fields.map((field2) => {
762
773
  let fieldSelector = "";
763
774
  if ("slug" in field2) {
764
- ({ fieldSelector } = getFieldFromModel(model, field2.slug, "to"));
775
+ ({ fieldSelector } = getFieldFromModel(existingModel, field2.slug, "to"));
765
776
  } else if ("expression" in field2) {
766
- fieldSelector = parseFieldExpression(model, "to", field2.expression);
777
+ fieldSelector = parseFieldExpression(existingModel, "to", field2.expression);
767
778
  }
768
779
  if (field2.collation) fieldSelector += ` COLLATE ${field2.collation}`;
769
780
  if (field2.order) fieldSelector += ` ${field2.order}`;
@@ -771,13 +782,13 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
771
782
  });
772
783
  statement2 += ` ON "${tableName}" (${columns.join(", ")})`;
773
784
  if (index.filter) {
774
- const withStatement = handleWith(models, model, params, index.filter);
785
+ const withStatement = handleWith(models, existingModel, params, index.filter);
775
786
  statement2 += ` WHERE (${withStatement})`;
776
787
  }
777
788
  }
778
789
  dependencyStatements.push({ statement: statement2, params });
779
790
  }
780
- if (entity === "trigger" && model) {
791
+ if (entity === "trigger") {
781
792
  const triggerName = convertToSnakeCase(slug);
782
793
  const params = [];
783
794
  let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
@@ -793,7 +804,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
793
804
  });
794
805
  }
795
806
  const fieldSelectors = trigger.fields.map((field2) => {
796
- return getFieldFromModel(model, field2.slug, "to").fieldSelector;
807
+ return getFieldFromModel(existingModel, field2.slug, "to").fieldSelector;
797
808
  });
798
809
  statementParts.push(`OF (${fieldSelectors.join(", ")})`);
799
810
  }
@@ -805,7 +816,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
805
816
  const tableAlias = trigger.action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
806
817
  const withStatement = handleWith(
807
818
  models,
808
- { ...model, tableAlias },
819
+ { ...existingModel, tableAlias },
809
820
  params,
810
821
  trigger.filter
811
822
  );
@@ -814,7 +825,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
814
825
  const effectStatements = trigger.effects.map((effectQuery) => {
815
826
  return compileQueryInput(effectQuery, models, params, {
816
827
  returning: false,
817
- parentModel: model
828
+ parentModel: existingModel
818
829
  }).main.statement;
819
830
  });
820
831
  if (effectStatements.length > 1) statementParts.push("BEGIN");
@@ -826,20 +837,37 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
826
837
  }
827
838
  const pluralType = PLURAL_MODEL_ENTITIES[entity];
828
839
  const field = `${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}`;
840
+ const targetEntityIndex = existingModel[pluralType]?.findIndex(
841
+ (entity2) => entity2.slug === slug
842
+ );
843
+ if ((action === "alter" || action === "drop") && (typeof targetEntityIndex === "undefined" || targetEntityIndex === -1)) {
844
+ throw new RoninError({
845
+ message: `No ${entity} with slug "${slug}" defined in model "${existingModel.name}".`,
846
+ code: MODEL_ENTITY_ERROR_CODES[entity]
847
+ });
848
+ }
829
849
  let json;
830
850
  switch (action) {
831
851
  case "create": {
832
852
  const value = prepareStatementValue(statementParams, jsonValue);
833
853
  json = `json_insert(${field}, '$.${slug}', ${value})`;
854
+ existingModel[pluralType] = [
855
+ ...existingModel[pluralType] || [],
856
+ jsonValue
857
+ ];
834
858
  break;
835
859
  }
836
860
  case "alter": {
837
861
  const value = prepareStatementValue(statementParams, jsonValue);
838
862
  json = `json_set(${field}, '$.${slug}', json_patch(json_extract(${field}, '$.${slug}'), ${value}))`;
863
+ const targetEntity = existingModel[pluralType];
864
+ targetEntity[targetEntityIndex] = jsonValue;
839
865
  break;
840
866
  }
841
867
  case "drop": {
842
868
  json = `json_remove(${field}, '$.${slug}')`;
869
+ const targetEntity = existingModel[pluralType];
870
+ delete targetEntity[targetEntityIndex];
843
871
  }
844
872
  }
845
873
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.9.0-leo-ron-1083-experimental-201",
3
+ "version": "0.9.0-leo-ron-1083-experimental-203",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {