@ronin/compiler 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +175 -225
- 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
|
-
|
737
|
-
|
738
|
-
|
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
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
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
|
788
|
-
switch (
|
789
|
-
case "
|
790
|
-
if (
|
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
|
-
|
708
|
+
actionReadable = "creating";
|
794
709
|
break;
|
795
710
|
}
|
796
|
-
case "
|
797
|
-
if (
|
798
|
-
|
711
|
+
case "alter": {
|
712
|
+
if (entity === "model") tableAction = "ALTER";
|
713
|
+
actionReadable = "updating";
|
799
714
|
break;
|
800
715
|
}
|
801
|
-
case "
|
802
|
-
if (
|
716
|
+
case "drop": {
|
717
|
+
if (entity === "model" || entity === "index" || entity === "trigger") {
|
803
718
|
tableAction = "DROP";
|
804
719
|
}
|
805
|
-
|
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 =
|
727
|
+
const usableSlug = entity === "model" ? slug : modelSlug;
|
813
728
|
const tableName = convertToSnakeCase(pluralize(usableSlug));
|
814
|
-
const targetModel =
|
815
|
-
|
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 (
|
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 (
|
833
|
+
if (entity === "trigger") {
|
850
834
|
const triggerName = convertToSnakeCase(slug);
|
851
835
|
const params = [];
|
852
836
|
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
|
853
|
-
if (
|
837
|
+
if (action === "create") {
|
854
838
|
const currentModel = targetModel;
|
855
|
-
const { when, action } = instructionList;
|
856
|
-
const statementParts = [`${when} ${
|
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 (
|
845
|
+
if (action2 !== "UPDATE") {
|
862
846
|
throw new RoninError({
|
863
|
-
message: `When ${
|
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((
|
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 =
|
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
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
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
|