@ronin/compiler 0.8.6 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/dist/index.js +48 -19
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -633,6 +633,13 @@ var PLURAL_MODEL_ENTITIES = {
633
633
  trigger: "triggers",
634
634
  preset: "presets"
635
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
+ };
636
643
  var transformMetaQuery = (models, dependencyStatements, statementParams, query) => {
637
644
  const { queryType } = splitQuery(query);
638
645
  const subAltering = query.alter && !("to" in query.alter);
@@ -671,14 +678,23 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
671
678
  const newModel = jsonValue;
672
679
  const modelWithFields = addDefaultModelFields(newModel, true);
673
680
  const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
674
- const { fields } = modelWithPresets;
675
- 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((field2) => getFieldStatement(models, modelWithPresets, field2)).filter(Boolean);
676
688
  dependencyStatements.push({
677
689
  statement: `${statement} (${columns.join(", ")})`,
678
690
  params: []
679
691
  });
680
692
  models.push(modelWithPresets);
681
- 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 };
682
698
  }
683
699
  if (action === "alter" && model) {
684
700
  const newModel = jsonValue;
@@ -714,10 +730,10 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
714
730
  }
715
731
  if (entity === "field" && model) {
716
732
  if (action === "create") {
717
- const field = jsonValue;
718
- field.type = field.type || "string";
733
+ const field2 = jsonValue;
734
+ field2.type = field2.type || "string";
719
735
  dependencyStatements.push({
720
- statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field)}`,
736
+ statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field2)}`,
721
737
  params: []
722
738
  });
723
739
  } else if (action === "alter") {
@@ -741,15 +757,15 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
741
757
  const params = [];
742
758
  let statement2 = `${tableAction}${index?.unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
743
759
  if (action === "create") {
744
- const columns = index.fields.map((field) => {
760
+ const columns = index.fields.map((field2) => {
745
761
  let fieldSelector = "";
746
- if ("slug" in field) {
747
- ({ fieldSelector } = getFieldFromModel(model, field.slug, "to"));
748
- } else if ("expression" in field) {
749
- fieldSelector = parseFieldExpression(model, "to", field.expression);
762
+ if ("slug" in field2) {
763
+ ({ fieldSelector } = getFieldFromModel(model, field2.slug, "to"));
764
+ } else if ("expression" in field2) {
765
+ fieldSelector = parseFieldExpression(model, "to", field2.expression);
750
766
  }
751
- if (field.collation) fieldSelector += ` COLLATE ${field.collation}`;
752
- if (field.order) fieldSelector += ` ${field.order}`;
767
+ if (field2.collation) fieldSelector += ` COLLATE ${field2.collation}`;
768
+ if (field2.order) fieldSelector += ` ${field2.order}`;
753
769
  return fieldSelector;
754
770
  });
755
771
  statement2 += ` ON "${tableName}" (${columns.join(", ")})`;
@@ -775,8 +791,8 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
775
791
  fields: ["action"]
776
792
  });
777
793
  }
778
- const fieldSelectors = trigger.fields.map((field) => {
779
- return getFieldFromModel(model, field.slug, "to").fieldSelector;
794
+ const fieldSelectors = trigger.fields.map((field2) => {
795
+ return getFieldFromModel(model, field2.slug, "to").fieldSelector;
780
796
  });
781
797
  statementParts.push(`OF (${fieldSelectors.join(", ")})`);
782
798
  }
@@ -808,10 +824,23 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
808
824
  dependencyStatements.push({ statement: statement2, params });
809
825
  }
810
826
  const pluralType = PLURAL_MODEL_ENTITIES[entity];
811
- const jsonAction = action === "create" ? "insert" : action === "alter" ? "patch" : "remove";
812
- let json = `json_${jsonAction}(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${slug}'`;
813
- if (jsonValue) json += `, ${prepareStatementValue(statementParams, jsonValue)}`;
814
- json += ")";
827
+ const field = `${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}`;
828
+ let json;
829
+ switch (action) {
830
+ case "create": {
831
+ const value = prepareStatementValue(statementParams, jsonValue);
832
+ json = `json_insert(${field}, '$.${slug}', ${value})`;
833
+ break;
834
+ }
835
+ case "alter": {
836
+ const value = prepareStatementValue(statementParams, jsonValue);
837
+ json = `json_set(${field}, '$.${slug}', json_patch(json_extract(${field}, '$.${slug}'), ${value}))`;
838
+ break;
839
+ }
840
+ case "drop": {
841
+ json = `json_remove(${field}, '$.${slug}')`;
842
+ }
843
+ }
815
844
  return {
816
845
  set: {
817
846
  model: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.8.6",
3
+ "version": "0.8.8",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {