@ronin/compiler 0.8.5 → 0.8.7

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