@ronin/compiler 0.9.0-leo-ron-1083-experimental-200 → 0.9.0-leo-ron-1083-experimental-202
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +40 -13
- package/package.json +1 -1
package/README.md
CHANGED
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
@@ -617,6 +617,7 @@ var getFieldStatement = (models, model, field) => {
|
|
617
617
|
statement += ` GENERATED ALWAYS AS (${parseFieldExpression(model, "to", symbol?.value)}) ${kind}`;
|
618
618
|
}
|
619
619
|
if (field.type === "link") {
|
620
|
+
if (field.kind === "many") return null;
|
620
621
|
const actions = field.actions || {};
|
621
622
|
const targetTable = getModelBySlug(models, field.target).table;
|
622
623
|
statement += ` REFERENCES ${targetTable}("id")`;
|
@@ -729,14 +730,18 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
729
730
|
}
|
730
731
|
};
|
731
732
|
}
|
732
|
-
|
733
|
+
const existingModel = model;
|
734
|
+
if (entity === "field") {
|
733
735
|
if (action === "create") {
|
734
736
|
const field2 = jsonValue;
|
735
737
|
field2.type = field2.type || "string";
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
738
|
+
const fieldStatement = getFieldStatement(models, existingModel, field2);
|
739
|
+
if (fieldStatement) {
|
740
|
+
dependencyStatements.push({
|
741
|
+
statement: `${statement} ADD COLUMN ${fieldStatement}`,
|
742
|
+
params: []
|
743
|
+
});
|
744
|
+
}
|
740
745
|
} else if (action === "alter") {
|
741
746
|
const newSlug = jsonValue?.slug;
|
742
747
|
if (newSlug) {
|
@@ -752,7 +757,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
752
757
|
});
|
753
758
|
}
|
754
759
|
}
|
755
|
-
if (entity === "index"
|
760
|
+
if (entity === "index") {
|
756
761
|
const index = jsonValue;
|
757
762
|
const indexName = convertToSnakeCase(slug);
|
758
763
|
const params = [];
|
@@ -761,9 +766,9 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
761
766
|
const columns = index.fields.map((field2) => {
|
762
767
|
let fieldSelector = "";
|
763
768
|
if ("slug" in field2) {
|
764
|
-
({ fieldSelector } = getFieldFromModel(
|
769
|
+
({ fieldSelector } = getFieldFromModel(existingModel, field2.slug, "to"));
|
765
770
|
} else if ("expression" in field2) {
|
766
|
-
fieldSelector = parseFieldExpression(
|
771
|
+
fieldSelector = parseFieldExpression(existingModel, "to", field2.expression);
|
767
772
|
}
|
768
773
|
if (field2.collation) fieldSelector += ` COLLATE ${field2.collation}`;
|
769
774
|
if (field2.order) fieldSelector += ` ${field2.order}`;
|
@@ -771,13 +776,13 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
771
776
|
});
|
772
777
|
statement2 += ` ON "${tableName}" (${columns.join(", ")})`;
|
773
778
|
if (index.filter) {
|
774
|
-
const withStatement = handleWith(models,
|
779
|
+
const withStatement = handleWith(models, existingModel, params, index.filter);
|
775
780
|
statement2 += ` WHERE (${withStatement})`;
|
776
781
|
}
|
777
782
|
}
|
778
783
|
dependencyStatements.push({ statement: statement2, params });
|
779
784
|
}
|
780
|
-
if (entity === "trigger"
|
785
|
+
if (entity === "trigger") {
|
781
786
|
const triggerName = convertToSnakeCase(slug);
|
782
787
|
const params = [];
|
783
788
|
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
|
@@ -793,7 +798,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
793
798
|
});
|
794
799
|
}
|
795
800
|
const fieldSelectors = trigger.fields.map((field2) => {
|
796
|
-
return getFieldFromModel(
|
801
|
+
return getFieldFromModel(existingModel, field2.slug, "to").fieldSelector;
|
797
802
|
});
|
798
803
|
statementParts.push(`OF (${fieldSelectors.join(", ")})`);
|
799
804
|
}
|
@@ -805,7 +810,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
805
810
|
const tableAlias = trigger.action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
|
806
811
|
const withStatement = handleWith(
|
807
812
|
models,
|
808
|
-
{ ...
|
813
|
+
{ ...existingModel, tableAlias },
|
809
814
|
params,
|
810
815
|
trigger.filter
|
811
816
|
);
|
@@ -814,7 +819,7 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
814
819
|
const effectStatements = trigger.effects.map((effectQuery) => {
|
815
820
|
return compileQueryInput(effectQuery, models, params, {
|
816
821
|
returning: false,
|
817
|
-
parentModel:
|
822
|
+
parentModel: existingModel
|
818
823
|
}).main.statement;
|
819
824
|
});
|
820
825
|
if (effectStatements.length > 1) statementParts.push("BEGIN");
|
@@ -826,20 +831,42 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
826
831
|
}
|
827
832
|
const pluralType = PLURAL_MODEL_ENTITIES[entity];
|
828
833
|
const field = `${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}`;
|
834
|
+
const targetEntityIndex = existingModel[pluralType]?.findIndex(
|
835
|
+
(entity2) => entity2.slug === slug
|
836
|
+
);
|
837
|
+
if ((action === "alter" || action === "drop") && (typeof targetEntityIndex === "undefined" || targetEntityIndex === -1)) {
|
838
|
+
throw new RoninError({
|
839
|
+
message: `No ${entity} with slug "${slug}" defined in model "${existingModel.name}".`,
|
840
|
+
code: {
|
841
|
+
field: "FIELD_NOT_FOUND",
|
842
|
+
index: "INDEX_NOT_FOUND",
|
843
|
+
trigger: "TRIGGER_NOT_FOUND",
|
844
|
+
preset: "PRESET_NOT_FOUND"
|
845
|
+
}[entity]
|
846
|
+
});
|
847
|
+
}
|
829
848
|
let json;
|
830
849
|
switch (action) {
|
831
850
|
case "create": {
|
832
851
|
const value = prepareStatementValue(statementParams, jsonValue);
|
833
852
|
json = `json_insert(${field}, '$.${slug}', ${value})`;
|
853
|
+
existingModel[pluralType] = [
|
854
|
+
...existingModel[pluralType] || [],
|
855
|
+
jsonValue
|
856
|
+
];
|
834
857
|
break;
|
835
858
|
}
|
836
859
|
case "alter": {
|
837
860
|
const value = prepareStatementValue(statementParams, jsonValue);
|
838
861
|
json = `json_set(${field}, '$.${slug}', json_patch(json_extract(${field}, '$.${slug}'), ${value}))`;
|
862
|
+
const targetEntity = existingModel[pluralType];
|
863
|
+
targetEntity[targetEntityIndex] = jsonValue;
|
839
864
|
break;
|
840
865
|
}
|
841
866
|
case "drop": {
|
842
867
|
json = `json_remove(${field}, '$.${slug}')`;
|
868
|
+
const targetEntity = existingModel[pluralType];
|
869
|
+
delete targetEntity[targetEntityIndex];
|
843
870
|
}
|
844
871
|
}
|
845
872
|
return {
|