@ronin/compiler 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +175 -225
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -318,151 +318,6 @@ var handleWith = (models, model, statementParams, instruction, parentModel) => {
318
318
  return `(${subStatement})`;
319
319
  };
320
320
 
321
- // src/utils/meta.ts
322
- var PLURAL_MODEL_ENTITIES = {
323
- field: "fields",
324
- index: "indexes",
325
- trigger: "triggers",
326
- preset: "presets"
327
- };
328
- var transformMetaQuery = (models, dependencyStatements, statementParams, query) => {
329
- if (query.create) {
330
- const init = query.create.model;
331
- const details = "to" in query.create ? { slug: init, ...query.create.to } : init;
332
- const modelWithFields = addDefaultModelFields(details, true);
333
- const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
334
- const instructions = {
335
- to: modelWithPresets
336
- };
337
- addModelQueries(models, dependencyStatements, {
338
- queryType: "add",
339
- queryModel: "model",
340
- queryInstructions: instructions
341
- });
342
- return {
343
- add: {
344
- model: instructions
345
- }
346
- };
347
- }
348
- if (query.drop) {
349
- const slug = query.drop.model;
350
- const instructions = {
351
- with: { slug }
352
- };
353
- addModelQueries(models, dependencyStatements, {
354
- queryType: "remove",
355
- queryModel: "model",
356
- queryInstructions: instructions
357
- });
358
- return {
359
- remove: {
360
- model: instructions
361
- }
362
- };
363
- }
364
- if (query.alter) {
365
- const slug = query.alter.model;
366
- if ("to" in query.alter) {
367
- const modelWithFields = addDefaultModelFields(query.alter.to, false);
368
- const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
369
- const instructions = {
370
- with: { slug },
371
- to: modelWithPresets
372
- };
373
- addModelQueries(models, dependencyStatements, {
374
- queryType: "set",
375
- queryModel: "model",
376
- queryInstructions: instructions
377
- });
378
- return {
379
- set: {
380
- model: instructions
381
- }
382
- };
383
- }
384
- if ("create" in query.alter) {
385
- const type2 = Object.keys(query.alter.create)[0];
386
- const pluralType2 = PLURAL_MODEL_ENTITIES[type2];
387
- const item = query.alter.create[type2];
388
- const completeItem = { slug: item.slug || `${type2}Slug`, ...item };
389
- addModelQueries(models, dependencyStatements, {
390
- queryType: "add",
391
- queryModel: type2,
392
- queryInstructions: {
393
- to: {
394
- model: { slug },
395
- ...completeItem
396
- }
397
- }
398
- });
399
- const value = prepareStatementValue(statementParams, completeItem);
400
- const json2 = `json_insert(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType2}, '$.${completeItem.slug}', ${value})`;
401
- const expression2 = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json2 };
402
- return {
403
- set: {
404
- model: {
405
- with: { slug },
406
- to: {
407
- [pluralType2]: expression2
408
- }
409
- }
410
- }
411
- };
412
- }
413
- if ("alter" in query.alter) {
414
- const type2 = Object.keys(query.alter.alter)[0];
415
- const pluralType2 = PLURAL_MODEL_ENTITIES[type2];
416
- const itemSlug2 = query.alter.alter[type2];
417
- const newItem = query.alter.alter.to;
418
- addModelQueries(models, dependencyStatements, {
419
- queryType: "set",
420
- queryModel: type2,
421
- queryInstructions: {
422
- with: { model: { slug }, slug: itemSlug2 },
423
- to: newItem
424
- }
425
- });
426
- const value = prepareStatementValue(statementParams, newItem);
427
- const json2 = `json_patch(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType2}, '$.${itemSlug2}', ${value})`;
428
- const expression2 = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json2 };
429
- return {
430
- set: {
431
- model: {
432
- with: { slug },
433
- to: {
434
- [pluralType2]: expression2
435
- }
436
- }
437
- }
438
- };
439
- }
440
- const type = Object.keys(query.alter.drop)[0];
441
- const pluralType = PLURAL_MODEL_ENTITIES[type];
442
- const itemSlug = query.alter.drop[type];
443
- addModelQueries(models, dependencyStatements, {
444
- queryType: "remove",
445
- queryModel: type,
446
- queryInstructions: {
447
- with: { model: { slug }, slug: itemSlug }
448
- }
449
- });
450
- const json = `json_insert(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${itemSlug}')`;
451
- const expression = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json };
452
- return {
453
- set: {
454
- model: {
455
- with: { slug },
456
- to: {
457
- [pluralType]: expression
458
- }
459
- }
460
- }
461
- };
462
- }
463
- return query;
464
- };
465
-
466
321
  // src/utils/model.ts
467
322
  import title from "title";
468
323
  var getModelBySlug = (models, slug) => {
@@ -733,9 +588,9 @@ var addDefaultModelPresets = (list, model) => {
733
588
  return model;
734
589
  };
735
590
  var mappedInstructions = {
736
- add: "to",
737
- set: "with",
738
- remove: "with"
591
+ create: "to",
592
+ alter: "with",
593
+ drop: "with"
739
594
  };
740
595
  var typesInSQLite = {
741
596
  link: "TEXT",
@@ -777,49 +632,179 @@ var getFieldStatement = (models, model, field) => {
777
632
  }
778
633
  return statement;
779
634
  };
780
- var addModelQueries = (models, dependencyStatements, queryDetails) => {
781
- const { queryType, queryModel, queryInstructions } = queryDetails;
782
- if (!["add", "set", "remove"].includes(queryType)) return;
783
- if (!["model", "field", "index", "trigger", "preset"].includes(queryModel)) return;
784
- const instructionName = mappedInstructions[queryType];
635
+ var PLURAL_MODEL_ENTITIES = {
636
+ field: "fields",
637
+ index: "indexes",
638
+ trigger: "triggers",
639
+ preset: "presets"
640
+ };
641
+ var transformMetaQuery = (models, dependencyStatements, statementParams, query) => {
642
+ const { queryType } = splitQuery(query);
643
+ let action = queryType;
644
+ let entity = "model";
645
+ let queryInstructions;
646
+ if (query.create) {
647
+ 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
+ };
657
+ }
658
+ if (query.alter) {
659
+ const modelSlugTest = query.alter.model;
660
+ if ("to" in query.alter) {
661
+ queryInstructions = {
662
+ with: { slug: modelSlugTest },
663
+ to: query.alter.to
664
+ };
665
+ } 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;
673
+ if ("create" in query.alter) {
674
+ 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
+ };
695
+ }
696
+ }
697
+ }
698
+ if (!queryInstructions) return query;
699
+ const instructionName = mappedInstructions[action];
785
700
  const instructionList = queryInstructions[instructionName];
786
701
  let tableAction = "ALTER";
787
- let queryTypeReadable = null;
788
- switch (queryType) {
789
- case "add": {
790
- if (queryModel === "model" || queryModel === "index" || queryModel === "trigger") {
702
+ let actionReadable = null;
703
+ switch (action) {
704
+ case "create": {
705
+ if (entity === "model" || entity === "index" || entity === "trigger") {
791
706
  tableAction = "CREATE";
792
707
  }
793
- queryTypeReadable = "creating";
708
+ actionReadable = "creating";
794
709
  break;
795
710
  }
796
- case "set": {
797
- if (queryModel === "model") tableAction = "ALTER";
798
- queryTypeReadable = "updating";
711
+ case "alter": {
712
+ if (entity === "model") tableAction = "ALTER";
713
+ actionReadable = "updating";
799
714
  break;
800
715
  }
801
- case "remove": {
802
- if (queryModel === "model" || queryModel === "index" || queryModel === "trigger") {
716
+ case "drop": {
717
+ if (entity === "model" || entity === "index" || entity === "trigger") {
803
718
  tableAction = "DROP";
804
719
  }
805
- queryTypeReadable = "deleting";
720
+ actionReadable = "deleting";
806
721
  break;
807
722
  }
808
723
  }
809
724
  const slug = instructionList?.slug?.being || instructionList?.slug;
810
725
  const modelInstruction = instructionList?.model;
811
726
  const modelSlug = modelInstruction?.slug?.being || modelInstruction?.slug;
812
- const usableSlug = queryModel === "model" ? slug : modelSlug;
727
+ const usableSlug = entity === "model" ? slug : modelSlug;
813
728
  const tableName = convertToSnakeCase(pluralize(usableSlug));
814
- const targetModel = queryModel === "model" && queryType === "add" ? null : getModelBySlug(models, usableSlug);
815
- if (queryModel === "index") {
729
+ const targetModel = entity === "model" && action === "create" ? null : getModelBySlug(models, usableSlug);
730
+ const statement = `${tableAction} TABLE "${tableName}"`;
731
+ if (entity === "model") {
732
+ let queryTypeDetails;
733
+ if (action === "create") {
734
+ const modelWithFields = addDefaultModelFields(queryInstructions.to, true);
735
+ const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
736
+ const { fields } = modelWithPresets;
737
+ const columns = fields.map((field) => getFieldStatement(models, modelWithPresets, field)).filter(Boolean);
738
+ dependencyStatements.push({
739
+ statement: `${statement} (${columns.join(", ")})`,
740
+ params: []
741
+ });
742
+ models.push(modelWithPresets);
743
+ queryTypeDetails = { to: modelWithPresets };
744
+ }
745
+ if (action === "alter") {
746
+ const modelWithFields = addDefaultModelFields(queryInstructions.to, false);
747
+ const modelWithPresets = addDefaultModelPresets(models, modelWithFields);
748
+ const newSlug = modelWithPresets.pluralSlug;
749
+ if (newSlug) {
750
+ const newTable = convertToSnakeCase(newSlug);
751
+ dependencyStatements.push({
752
+ statement: `${statement} RENAME TO "${newTable}"`,
753
+ params: []
754
+ });
755
+ }
756
+ Object.assign(targetModel, modelWithPresets);
757
+ queryTypeDetails = {
758
+ with: {
759
+ slug: usableSlug
760
+ },
761
+ to: modelWithPresets
762
+ };
763
+ }
764
+ if (action === "drop") {
765
+ models.splice(models.indexOf(targetModel), 1);
766
+ dependencyStatements.push({ statement, params: [] });
767
+ queryTypeDetails = {
768
+ with: { slug: usableSlug }
769
+ };
770
+ }
771
+ const queryTypeAction = action === "create" ? "add" : action === "alter" ? "set" : "remove";
772
+ return {
773
+ [queryTypeAction]: {
774
+ model: queryTypeDetails
775
+ }
776
+ };
777
+ }
778
+ if (entity === "field") {
779
+ if (action === "create") {
780
+ if (!instructionList.type) instructionList.type = "string";
781
+ dependencyStatements.push({
782
+ statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`,
783
+ params: []
784
+ });
785
+ } else if (action === "alter") {
786
+ const newSlug = queryInstructions.to?.slug;
787
+ if (newSlug) {
788
+ dependencyStatements.push({
789
+ statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`,
790
+ params: []
791
+ });
792
+ }
793
+ } else if (action === "drop") {
794
+ dependencyStatements.push({
795
+ statement: `${statement} DROP COLUMN "${slug}"`,
796
+ params: []
797
+ });
798
+ }
799
+ }
800
+ if (entity === "index") {
816
801
  const indexName = convertToSnakeCase(slug);
817
802
  const unique = instructionList?.unique;
818
803
  const filterQuery = instructionList?.filter;
819
804
  const fields = instructionList?.fields;
820
805
  const params = [];
821
806
  let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
822
- if (queryType === "add") {
807
+ if (action === "create") {
823
808
  const model = targetModel;
824
809
  const columns = fields.map((field) => {
825
810
  let fieldSelector = "";
@@ -844,23 +829,22 @@ var addModelQueries = (models, dependencyStatements, queryDetails) => {
844
829
  }
845
830
  }
846
831
  dependencyStatements.push({ statement: statement2, params });
847
- return;
848
832
  }
849
- if (queryModel === "trigger") {
833
+ if (entity === "trigger") {
850
834
  const triggerName = convertToSnakeCase(slug);
851
835
  const params = [];
852
836
  let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
853
- if (queryType === "add") {
837
+ if (action === "create") {
854
838
  const currentModel = targetModel;
855
- const { when, action } = instructionList;
856
- const statementParts = [`${when} ${action}`];
839
+ const { when, action: action2 } = instructionList;
840
+ const statementParts = [`${when} ${action2}`];
857
841
  const effectQueries = instructionList?.effects;
858
842
  const filterQuery = instructionList?.filter;
859
843
  const fields = instructionList?.fields;
860
844
  if (fields) {
861
- if (action !== "UPDATE") {
845
+ if (action2 !== "UPDATE") {
862
846
  throw new RoninError({
863
- message: `When ${queryTypeReadable} ${PLURAL_MODEL_ENTITIES[queryModel]}, targeting specific fields requires the \`UPDATE\` action.`,
847
+ message: `When ${actionReadable} ${PLURAL_MODEL_ENTITIES[entity]}, targeting specific fields requires the \`UPDATE\` action.`,
864
848
  code: "INVALID_MODEL_VALUE",
865
849
  fields: ["action"]
866
850
  });
@@ -871,11 +855,11 @@ var addModelQueries = (models, dependencyStatements, queryDetails) => {
871
855
  statementParts.push(`OF (${fieldSelectors.join(", ")})`);
872
856
  }
873
857
  statementParts.push("ON", `"${tableName}"`);
874
- if (filterQuery || effectQueries.some((query) => findInObject(query, RONIN_MODEL_SYMBOLS.FIELD))) {
858
+ if (filterQuery || effectQueries.some((query2) => findInObject(query2, RONIN_MODEL_SYMBOLS.FIELD))) {
875
859
  statementParts.push("FOR EACH ROW");
876
860
  }
877
861
  if (filterQuery) {
878
- const tableAlias = action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
862
+ const tableAlias = action2 === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
879
863
  const withStatement = handleWith(
880
864
  models,
881
865
  { ...currentModel, tableAlias },
@@ -896,57 +880,23 @@ var addModelQueries = (models, dependencyStatements, queryDetails) => {
896
880
  statement2 += ` ${statementParts.join(" ")}`;
897
881
  }
898
882
  dependencyStatements.push({ statement: statement2, params });
899
- return;
900
- }
901
- const statement = `${tableAction} TABLE "${tableName}"`;
902
- if (queryModel === "model") {
903
- if (queryType === "add") {
904
- const newModel = queryInstructions.to;
905
- const { fields } = newModel;
906
- const columns = fields.map((field) => getFieldStatement(models, newModel, field)).filter(Boolean);
907
- dependencyStatements.push({
908
- statement: `${statement} (${columns.join(", ")})`,
909
- params: []
910
- });
911
- models.push(newModel);
912
- } else if (queryType === "set") {
913
- const newSlug = queryInstructions.to?.pluralSlug;
914
- if (newSlug) {
915
- const newTable = convertToSnakeCase(newSlug);
916
- dependencyStatements.push({
917
- statement: `${statement} RENAME TO "${newTable}"`,
918
- params: []
919
- });
920
- }
921
- Object.assign(targetModel, queryInstructions.to);
922
- } else if (queryType === "remove") {
923
- models.splice(models.indexOf(targetModel), 1);
924
- dependencyStatements.push({ statement, params: [] });
925
- }
926
- return;
927
883
  }
928
- if (queryModel === "field") {
929
- if (queryType === "add") {
930
- if (!instructionList.type) instructionList.type = "string";
931
- dependencyStatements.push({
932
- statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`,
933
- params: []
934
- });
935
- } else if (queryType === "set") {
936
- const newSlug = queryInstructions.to?.slug;
937
- if (newSlug) {
938
- dependencyStatements.push({
939
- statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`,
940
- params: []
941
- });
884
+ const pluralType = PLURAL_MODEL_ENTITIES[entity];
885
+ const jsonAction = action === "create" ? "insert" : action === "alter" ? "patch" : "remove";
886
+ const jsonValue = action === "create" ? { ...instructionList, model: void 0 } : action === "alter" ? queryInstructions.to : null;
887
+ let json = `json_${jsonAction}(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${slug}'`;
888
+ if (jsonValue) json += `, ${prepareStatementValue(statementParams, jsonValue)}`;
889
+ json += ")";
890
+ return {
891
+ set: {
892
+ model: {
893
+ with: { slug: usableSlug },
894
+ to: {
895
+ [pluralType]: { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json }
896
+ }
942
897
  }
943
- } else if (queryType === "remove") {
944
- dependencyStatements.push({
945
- statement: `${statement} DROP COLUMN "${slug}"`,
946
- params: []
947
- });
948
898
  }
949
- }
899
+ };
950
900
  };
951
901
 
952
902
  // src/instructions/before-after.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {