@ronin/compiler 0.8.5 → 0.8.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +55 -130
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -5985,4 +5985,4 @@ declare const compileQueries: (queries: Array<Query>, models: Array<PublicModel>
|
|
5985
5985
|
inlineParams?: boolean;
|
5986
5986
|
}) => Array<Statement>;
|
5987
5987
|
|
5988
|
-
export { type PublicModel as Model, type ModelField, type ModelIndex, type ModelTrigger, type Query, type Statement, compileQueries };
|
5988
|
+
export { type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, type Query, type Statement, compileQueries };
|
package/dist/index.js
CHANGED
@@ -587,11 +587,6 @@ var addDefaultModelPresets = (list, model) => {
|
|
587
587
|
}
|
588
588
|
return model;
|
589
589
|
};
|
590
|
-
var mappedInstructions = {
|
591
|
-
create: "to",
|
592
|
-
alter: "with",
|
593
|
-
drop: "with"
|
594
|
-
};
|
595
590
|
var typesInSQLite = {
|
596
591
|
link: "TEXT",
|
597
592
|
string: "TEXT",
|
@@ -640,98 +635,41 @@ var PLURAL_MODEL_ENTITIES = {
|
|
640
635
|
};
|
641
636
|
var transformMetaQuery = (models, dependencyStatements, statementParams, query) => {
|
642
637
|
const { queryType } = splitQuery(query);
|
643
|
-
|
644
|
-
|
645
|
-
|
638
|
+
const subAltering = query.alter && !("to" in query.alter);
|
639
|
+
const action = subAltering && query.alter ? Object.keys(query.alter).filter((key) => key !== "model")[0] : queryType;
|
640
|
+
const actionReadable = action === "create" ? "creating" : action === "alter" ? "altering" : "dropping";
|
641
|
+
const entity = subAltering && query.alter ? Object.keys(query.alter[action])[0] : "model";
|
642
|
+
let slug = entity === "model" && action === "create" ? null : query[queryType].model;
|
643
|
+
let modelSlug = slug;
|
644
|
+
let jsonValue;
|
646
645
|
if (query.create) {
|
647
646
|
const init = query.create.model;
|
648
|
-
|
649
|
-
|
650
|
-
to: details
|
651
|
-
};
|
652
|
-
}
|
653
|
-
if (query.drop) {
|
654
|
-
queryInstructions = {
|
655
|
-
with: { slug: query.drop.model }
|
656
|
-
};
|
647
|
+
jsonValue = "to" in query.create ? { slug: init, ...query.create.to } : init;
|
648
|
+
slug = modelSlug = jsonValue.slug;
|
657
649
|
}
|
658
650
|
if (query.alter) {
|
659
|
-
const modelSlugTest = query.alter.model;
|
660
651
|
if ("to" in query.alter) {
|
661
|
-
|
662
|
-
with: { slug: modelSlugTest },
|
663
|
-
to: query.alter.to
|
664
|
-
};
|
652
|
+
jsonValue = query.alter.to;
|
665
653
|
} else {
|
666
|
-
|
667
|
-
(key) => key !== "model"
|
668
|
-
)[0];
|
669
|
-
const details = query.alter[action];
|
670
|
-
entity = Object.keys(details)[0];
|
671
|
-
let jsonSlug = details[entity];
|
672
|
-
let jsonValue2;
|
654
|
+
slug = query.alter[action][entity];
|
673
655
|
if ("create" in query.alter) {
|
674
656
|
const item = query.alter.create[entity];
|
675
|
-
|
676
|
-
|
677
|
-
queryInstructions = {
|
678
|
-
to: {
|
679
|
-
model: { slug: modelSlugTest },
|
680
|
-
...jsonValue2
|
681
|
-
}
|
682
|
-
};
|
683
|
-
}
|
684
|
-
if ("alter" in query.alter) {
|
685
|
-
jsonValue2 = query.alter.alter.to;
|
686
|
-
queryInstructions = {
|
687
|
-
with: { model: { slug: modelSlugTest }, slug: jsonSlug },
|
688
|
-
to: jsonValue2
|
689
|
-
};
|
690
|
-
}
|
691
|
-
if ("drop" in query.alter) {
|
692
|
-
queryInstructions = {
|
693
|
-
with: { model: { slug: modelSlugTest }, slug: jsonSlug }
|
694
|
-
};
|
657
|
+
slug = item.slug || `${entity}Slug`;
|
658
|
+
jsonValue = { slug, ...item };
|
695
659
|
}
|
660
|
+
if ("alter" in query.alter) jsonValue = query.alter.alter.to;
|
696
661
|
}
|
697
662
|
}
|
698
|
-
if (!
|
699
|
-
const
|
700
|
-
const
|
701
|
-
|
702
|
-
let actionReadable = null;
|
703
|
-
switch (action) {
|
704
|
-
case "create": {
|
705
|
-
if (entity === "model" || entity === "index" || entity === "trigger") {
|
706
|
-
tableAction = "CREATE";
|
707
|
-
}
|
708
|
-
actionReadable = "creating";
|
709
|
-
break;
|
710
|
-
}
|
711
|
-
case "alter": {
|
712
|
-
if (entity === "model") tableAction = "ALTER";
|
713
|
-
actionReadable = "updating";
|
714
|
-
break;
|
715
|
-
}
|
716
|
-
case "drop": {
|
717
|
-
if (entity === "model" || entity === "index" || entity === "trigger") {
|
718
|
-
tableAction = "DROP";
|
719
|
-
}
|
720
|
-
actionReadable = "deleting";
|
721
|
-
break;
|
722
|
-
}
|
723
|
-
}
|
724
|
-
const slug = instructionList?.slug?.being || instructionList?.slug;
|
725
|
-
const modelInstruction = instructionList?.model;
|
726
|
-
const modelSlug = modelInstruction?.slug?.being || modelInstruction?.slug;
|
727
|
-
const usableSlug = entity === "model" ? slug : modelSlug;
|
728
|
-
const tableName = convertToSnakeCase(pluralize(usableSlug));
|
729
|
-
const targetModel = entity === "model" && action === "create" ? null : getModelBySlug(models, usableSlug);
|
663
|
+
if (!(modelSlug && slug)) return query;
|
664
|
+
const tableAction = ["model", "index", "trigger"].includes(entity) ? action.toUpperCase() : "ALTER";
|
665
|
+
const tableName = convertToSnakeCase(pluralize(modelSlug));
|
666
|
+
const model = action === "create" && entity === "model" ? null : getModelBySlug(models, modelSlug);
|
730
667
|
const statement = `${tableAction} TABLE "${tableName}"`;
|
731
668
|
if (entity === "model") {
|
732
669
|
let queryTypeDetails;
|
733
670
|
if (action === "create") {
|
734
|
-
const
|
671
|
+
const newModel = jsonValue;
|
672
|
+
const modelWithFields = addDefaultModelFields(newModel, true);
|
735
673
|
const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
|
736
674
|
const { fields } = modelWithPresets;
|
737
675
|
const columns = fields.map((field) => getFieldStatement(models, modelWithPresets, field)).filter(Boolean);
|
@@ -742,8 +680,9 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
742
680
|
models.push(modelWithPresets);
|
743
681
|
queryTypeDetails = { to: modelWithPresets };
|
744
682
|
}
|
745
|
-
if (action === "alter") {
|
746
|
-
const
|
683
|
+
if (action === "alter" && model) {
|
684
|
+
const newModel = jsonValue;
|
685
|
+
const modelWithFields = addDefaultModelFields(newModel, false);
|
747
686
|
const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
|
748
687
|
const newSlug = modelWithPresets.pluralSlug;
|
749
688
|
if (newSlug) {
|
@@ -753,20 +692,18 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
753
692
|
params: []
|
754
693
|
});
|
755
694
|
}
|
756
|
-
Object.assign(
|
695
|
+
Object.assign(model, modelWithPresets);
|
757
696
|
queryTypeDetails = {
|
758
697
|
with: {
|
759
|
-
slug
|
698
|
+
slug
|
760
699
|
},
|
761
700
|
to: modelWithPresets
|
762
701
|
};
|
763
702
|
}
|
764
|
-
if (action === "drop") {
|
765
|
-
models.splice(models.indexOf(
|
703
|
+
if (action === "drop" && model) {
|
704
|
+
models.splice(models.indexOf(model), 1);
|
766
705
|
dependencyStatements.push({ statement, params: [] });
|
767
|
-
queryTypeDetails = {
|
768
|
-
with: { slug: usableSlug }
|
769
|
-
};
|
706
|
+
queryTypeDetails = { with: { slug } };
|
770
707
|
}
|
771
708
|
const queryTypeAction = action === "create" ? "add" : action === "alter" ? "set" : "remove";
|
772
709
|
return {
|
@@ -775,15 +712,16 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
775
712
|
}
|
776
713
|
};
|
777
714
|
}
|
778
|
-
if (entity === "field") {
|
715
|
+
if (entity === "field" && model) {
|
779
716
|
if (action === "create") {
|
780
|
-
|
717
|
+
const field = jsonValue;
|
718
|
+
field.type = field.type || "string";
|
781
719
|
dependencyStatements.push({
|
782
|
-
statement: `${statement} ADD COLUMN ${getFieldStatement(models,
|
720
|
+
statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field)}`,
|
783
721
|
params: []
|
784
722
|
});
|
785
723
|
} else if (action === "alter") {
|
786
|
-
const newSlug =
|
724
|
+
const newSlug = jsonValue?.slug;
|
787
725
|
if (newSlug) {
|
788
726
|
dependencyStatements.push({
|
789
727
|
statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`,
|
@@ -797,81 +735,69 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
797
735
|
});
|
798
736
|
}
|
799
737
|
}
|
800
|
-
if (entity === "index") {
|
738
|
+
if (entity === "index" && model) {
|
739
|
+
const index = jsonValue;
|
801
740
|
const indexName = convertToSnakeCase(slug);
|
802
|
-
const unique = instructionList?.unique;
|
803
|
-
const filterQuery = instructionList?.filter;
|
804
|
-
const fields = instructionList?.fields;
|
805
741
|
const params = [];
|
806
|
-
let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
|
742
|
+
let statement2 = `${tableAction}${index?.unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
|
807
743
|
if (action === "create") {
|
808
|
-
const
|
809
|
-
const columns = fields.map((field) => {
|
744
|
+
const columns = index.fields.map((field) => {
|
810
745
|
let fieldSelector = "";
|
811
746
|
if ("slug" in field) {
|
812
747
|
({ fieldSelector } = getFieldFromModel(model, field.slug, "to"));
|
813
748
|
} else if ("expression" in field) {
|
814
|
-
fieldSelector = parseFieldExpression(model, "to", field.expression
|
749
|
+
fieldSelector = parseFieldExpression(model, "to", field.expression);
|
815
750
|
}
|
816
751
|
if (field.collation) fieldSelector += ` COLLATE ${field.collation}`;
|
817
752
|
if (field.order) fieldSelector += ` ${field.order}`;
|
818
753
|
return fieldSelector;
|
819
754
|
});
|
820
755
|
statement2 += ` ON "${tableName}" (${columns.join(", ")})`;
|
821
|
-
if (
|
822
|
-
const withStatement = handleWith(
|
823
|
-
models,
|
824
|
-
targetModel,
|
825
|
-
params,
|
826
|
-
filterQuery
|
827
|
-
);
|
756
|
+
if (index.filter) {
|
757
|
+
const withStatement = handleWith(models, model, params, index.filter);
|
828
758
|
statement2 += ` WHERE (${withStatement})`;
|
829
759
|
}
|
830
760
|
}
|
831
761
|
dependencyStatements.push({ statement: statement2, params });
|
832
762
|
}
|
833
|
-
if (entity === "trigger") {
|
763
|
+
if (entity === "trigger" && model) {
|
834
764
|
const triggerName = convertToSnakeCase(slug);
|
835
765
|
const params = [];
|
836
766
|
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
|
837
767
|
if (action === "create") {
|
838
|
-
const
|
839
|
-
const {
|
840
|
-
|
841
|
-
|
842
|
-
const filterQuery = instructionList?.filter;
|
843
|
-
const fields = instructionList?.fields;
|
844
|
-
if (fields) {
|
845
|
-
if (action2 !== "UPDATE") {
|
768
|
+
const trigger = jsonValue;
|
769
|
+
const statementParts = [`${trigger.when} ${trigger.action}`];
|
770
|
+
if (trigger.fields) {
|
771
|
+
if (trigger.action !== "UPDATE") {
|
846
772
|
throw new RoninError({
|
847
773
|
message: `When ${actionReadable} ${PLURAL_MODEL_ENTITIES[entity]}, targeting specific fields requires the \`UPDATE\` action.`,
|
848
774
|
code: "INVALID_MODEL_VALUE",
|
849
775
|
fields: ["action"]
|
850
776
|
});
|
851
777
|
}
|
852
|
-
const fieldSelectors = fields.map((field) => {
|
853
|
-
return getFieldFromModel(
|
778
|
+
const fieldSelectors = trigger.fields.map((field) => {
|
779
|
+
return getFieldFromModel(model, field.slug, "to").fieldSelector;
|
854
780
|
});
|
855
781
|
statementParts.push(`OF (${fieldSelectors.join(", ")})`);
|
856
782
|
}
|
857
783
|
statementParts.push("ON", `"${tableName}"`);
|
858
|
-
if (
|
784
|
+
if (trigger.filter || trigger.effects.some((query2) => findInObject(query2, RONIN_MODEL_SYMBOLS.FIELD))) {
|
859
785
|
statementParts.push("FOR EACH ROW");
|
860
786
|
}
|
861
|
-
if (
|
862
|
-
const tableAlias =
|
787
|
+
if (trigger.filter) {
|
788
|
+
const tableAlias = trigger.action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
|
863
789
|
const withStatement = handleWith(
|
864
790
|
models,
|
865
|
-
{ ...
|
791
|
+
{ ...model, tableAlias },
|
866
792
|
params,
|
867
|
-
|
793
|
+
trigger.filter
|
868
794
|
);
|
869
795
|
statementParts.push("WHEN", `(${withStatement})`);
|
870
796
|
}
|
871
|
-
const effectStatements =
|
797
|
+
const effectStatements = trigger.effects.map((effectQuery) => {
|
872
798
|
return compileQueryInput(effectQuery, models, params, {
|
873
799
|
returning: false,
|
874
|
-
parentModel:
|
800
|
+
parentModel: model
|
875
801
|
}).main.statement;
|
876
802
|
});
|
877
803
|
if (effectStatements.length > 1) statementParts.push("BEGIN");
|
@@ -883,14 +809,13 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
883
809
|
}
|
884
810
|
const pluralType = PLURAL_MODEL_ENTITIES[entity];
|
885
811
|
const jsonAction = action === "create" ? "insert" : action === "alter" ? "patch" : "remove";
|
886
|
-
const jsonValue = action === "create" ? { ...instructionList, model: void 0 } : action === "alter" ? queryInstructions.to : null;
|
887
812
|
let json = `json_${jsonAction}(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${slug}'`;
|
888
813
|
if (jsonValue) json += `, ${prepareStatementValue(statementParams, jsonValue)}`;
|
889
814
|
json += ")";
|
890
815
|
return {
|
891
816
|
set: {
|
892
817
|
model: {
|
893
|
-
with: { slug:
|
818
|
+
with: { slug: modelSlug },
|
894
819
|
to: {
|
895
820
|
[pluralType]: { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json }
|
896
821
|
}
|