@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 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
- let action = queryType;
644
- let entity = "model";
645
- let queryInstructions;
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
- 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
- };
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
- queryInstructions = {
662
- with: { slug: modelSlugTest },
663
- to: query.alter.to
664
- };
652
+ jsonValue = query.alter.to;
665
653
  } 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;
654
+ slug = query.alter[action][entity];
673
655
  if ("create" in query.alter) {
674
656
  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
- };
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 (!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);
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 modelWithFields = addDefaultModelFields(queryInstructions.to, true);
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 modelWithFields = addDefaultModelFields(queryInstructions.to, false);
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(targetModel, modelWithPresets);
695
+ Object.assign(model, modelWithPresets);
757
696
  queryTypeDetails = {
758
697
  with: {
759
- slug: usableSlug
698
+ slug
760
699
  },
761
700
  to: modelWithPresets
762
701
  };
763
702
  }
764
- if (action === "drop") {
765
- models.splice(models.indexOf(targetModel), 1);
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
- if (!instructionList.type) instructionList.type = "string";
717
+ const field = jsonValue;
718
+ field.type = field.type || "string";
781
719
  dependencyStatements.push({
782
- statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`,
720
+ statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field)}`,
783
721
  params: []
784
722
  });
785
723
  } else if (action === "alter") {
786
- const newSlug = queryInstructions.to?.slug;
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 model = targetModel;
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, model);
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 (filterQuery) {
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 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") {
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(currentModel, field.slug, "to").fieldSelector;
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 (filterQuery || effectQueries.some((query2) => findInObject(query2, RONIN_MODEL_SYMBOLS.FIELD))) {
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 (filterQuery) {
862
- const tableAlias = action2 === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
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
- { ...currentModel, tableAlias },
791
+ { ...model, tableAlias },
866
792
  params,
867
- filterQuery
793
+ trigger.filter
868
794
  );
869
795
  statementParts.push("WHEN", `(${withStatement})`);
870
796
  }
871
- const effectStatements = effectQueries.map((effectQuery) => {
797
+ const effectStatements = trigger.effects.map((effectQuery) => {
872
798
  return compileQueryInput(effectQuery, models, params, {
873
799
  returning: false,
874
- parentModel: currentModel
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: usableSlug },
818
+ with: { slug: modelSlug },
894
819
  to: {
895
820
  [pluralType]: { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json }
896
821
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {