@ronin/compiler 0.3.1 → 0.5.0
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.
- package/README.md +34 -13
- package/dist/index.d.ts +17 -14233
- package/dist/index.js +151 -90
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
// src/utils/
|
1
|
+
// src/utils/helpers.ts
|
2
2
|
import { init as cuid } from "@paralleldrive/cuid2";
|
3
3
|
var RONIN_SCHEMA_SYMBOLS = {
|
4
4
|
// Represents a sub query.
|
@@ -33,7 +33,7 @@ var DOUBLE_QUOTE_REGEX = /"/g;
|
|
33
33
|
var AMPERSAND_REGEX = /\s*&+\s*/g;
|
34
34
|
var SPECIAL_CHARACTERS_REGEX = /[^\w\s-]+/g;
|
35
35
|
var SPLIT_REGEX = /(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|[\s.\-_]+/;
|
36
|
-
var generateRecordId = (prefix) => `${prefix
|
36
|
+
var generateRecordId = (prefix) => `${prefix}_${cuid({ length: 16 })()}`;
|
37
37
|
var capitalize = (str) => {
|
38
38
|
if (!str || str.length === 0) return "";
|
39
39
|
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
|
@@ -96,18 +96,19 @@ var splitQuery = (query) => {
|
|
96
96
|
};
|
97
97
|
|
98
98
|
// src/utils/statement.ts
|
99
|
-
var prepareStatementValue = (
|
100
|
-
if (
|
99
|
+
var prepareStatementValue = (statementParams, value) => {
|
100
|
+
if (value === null) return "NULL";
|
101
|
+
if (!statementParams) return JSON.stringify(value);
|
101
102
|
let formattedValue = value;
|
102
103
|
if (Array.isArray(value) || isObject(value)) {
|
103
104
|
formattedValue = JSON.stringify(value);
|
104
105
|
} else if (typeof value === "boolean") {
|
105
106
|
formattedValue = value ? 1 : 0;
|
106
107
|
}
|
107
|
-
const index =
|
108
|
+
const index = statementParams.push(formattedValue);
|
108
109
|
return `?${index}`;
|
109
110
|
};
|
110
|
-
var composeFieldValues = (schemas, schema,
|
111
|
+
var composeFieldValues = (schemas, schema, statementParams, instructionName, value, options) => {
|
111
112
|
const { field: schemaField, fieldSelector: selector } = getFieldFromSchema(
|
112
113
|
schema,
|
113
114
|
options.fieldSlug,
|
@@ -122,8 +123,8 @@ var composeFieldValues = (schemas, schema, statementValues, instructionName, val
|
|
122
123
|
conditionValue = `(${compileQueryInput(
|
123
124
|
value[RONIN_SCHEMA_SYMBOLS.QUERY],
|
124
125
|
schemas,
|
125
|
-
|
126
|
-
).
|
126
|
+
statementParams
|
127
|
+
).main.statement})`;
|
127
128
|
} else if (typeof value === "string" && value.startsWith(RONIN_SCHEMA_SYMBOLS.FIELD)) {
|
128
129
|
let targetTable = `"${options.rootTable}"`;
|
129
130
|
let toReplace = RONIN_SCHEMA_SYMBOLS.FIELD;
|
@@ -139,21 +140,21 @@ var composeFieldValues = (schemas, schema, statementValues, instructionName, val
|
|
139
140
|
} else if (schemaField.type === "json" && instructionName === "to") {
|
140
141
|
conditionSelector = `"${schemaField.slug}"`;
|
141
142
|
if (collectStatementValue) {
|
142
|
-
const preparedValue = prepareStatementValue(
|
143
|
+
const preparedValue = prepareStatementValue(statementParams, value);
|
143
144
|
conditionValue = `IIF(${conditionSelector} IS NULL, ${preparedValue}, json_patch(${conditionSelector}, ${preparedValue}))`;
|
144
145
|
}
|
145
146
|
} else if (collectStatementValue) {
|
146
|
-
conditionValue = prepareStatementValue(
|
147
|
+
conditionValue = prepareStatementValue(statementParams, value);
|
147
148
|
}
|
148
149
|
if (options.type === "fields") return conditionSelector;
|
149
150
|
if (options.type === "values") return conditionValue;
|
150
151
|
return `${conditionSelector} ${WITH_CONDITIONS[options.condition || "being"](conditionValue, value)}`;
|
151
152
|
};
|
152
|
-
var composeConditions = (schemas, schema,
|
153
|
+
var composeConditions = (schemas, schema, statementParams, instructionName, value, options) => {
|
153
154
|
const isNested = isObject(value) && Object.keys(value).length > 0;
|
154
155
|
if (isNested && Object.keys(value).every((key) => key in WITH_CONDITIONS)) {
|
155
156
|
const conditions = Object.entries(value).map(
|
156
|
-
([conditionType, checkValue]) => composeConditions(schemas, schema,
|
157
|
+
([conditionType, checkValue]) => composeConditions(schemas, schema, statementParams, instructionName, checkValue, {
|
157
158
|
...options,
|
158
159
|
condition: conditionType
|
159
160
|
})
|
@@ -174,7 +175,7 @@ var composeConditions = (schemas, schema, statementValues, instructionName, valu
|
|
174
175
|
return composeFieldValues(
|
175
176
|
schemas,
|
176
177
|
schema,
|
177
|
-
|
178
|
+
statementParams,
|
178
179
|
instructionName,
|
179
180
|
value,
|
180
181
|
{ ...options, fieldSlug: options.fieldSlug }
|
@@ -203,7 +204,7 @@ var composeConditions = (schemas, schema, statementValues, instructionName, valu
|
|
203
204
|
return composeConditions(
|
204
205
|
schemas,
|
205
206
|
schema,
|
206
|
-
|
207
|
+
statementParams,
|
207
208
|
instructionName,
|
208
209
|
recordTarget,
|
209
210
|
options
|
@@ -213,7 +214,7 @@ var composeConditions = (schemas, schema, statementValues, instructionName, valu
|
|
213
214
|
if (isNested) {
|
214
215
|
const conditions = Object.entries(value).map(([field, value2]) => {
|
215
216
|
const nestedFieldSlug = options.fieldSlug ? `${options.fieldSlug}.${field}` : field;
|
216
|
-
return composeConditions(schemas, schema,
|
217
|
+
return composeConditions(schemas, schema, statementParams, instructionName, value2, {
|
217
218
|
...options,
|
218
219
|
fieldSlug: nestedFieldSlug
|
219
220
|
});
|
@@ -227,7 +228,7 @@ var composeConditions = (schemas, schema, statementValues, instructionName, valu
|
|
227
228
|
(filter) => composeConditions(
|
228
229
|
schemas,
|
229
230
|
schema,
|
230
|
-
|
231
|
+
statementParams,
|
231
232
|
instructionName,
|
232
233
|
filter,
|
233
234
|
options
|
@@ -250,10 +251,10 @@ var formatIdentifiers = ({ identifiers }, queryInstructions) => {
|
|
250
251
|
return queryInstructions;
|
251
252
|
const newNestedInstructions = { ...nestedInstructions };
|
252
253
|
for (const oldKey of Object.keys(newNestedInstructions)) {
|
253
|
-
if (oldKey !== "
|
254
|
-
const identifierName = oldKey === "
|
254
|
+
if (oldKey !== "nameIdentifier" && oldKey !== "slugIdentifier") continue;
|
255
|
+
const identifierName = oldKey === "nameIdentifier" ? "name" : "slug";
|
255
256
|
const value = newNestedInstructions[oldKey];
|
256
|
-
const newKey = identifiers
|
257
|
+
const newKey = identifiers[identifierName];
|
257
258
|
newNestedInstructions[newKey] = value;
|
258
259
|
delete newNestedInstructions[oldKey];
|
259
260
|
}
|
@@ -286,11 +287,11 @@ var WITH_CONDITIONS = {
|
|
286
287
|
lessThan: (value) => `< ${value}`,
|
287
288
|
lessOrEqual: (value) => `<= ${value}`
|
288
289
|
};
|
289
|
-
var handleWith = (schemas, schema,
|
290
|
+
var handleWith = (schemas, schema, statementParams, instruction, rootTable) => {
|
290
291
|
const subStatement = composeConditions(
|
291
292
|
schemas,
|
292
293
|
schema,
|
293
|
-
|
294
|
+
statementParams,
|
294
295
|
"with",
|
295
296
|
instruction,
|
296
297
|
{ rootTable }
|
@@ -395,6 +396,10 @@ var SYSTEM_SCHEMAS = [
|
|
395
396
|
pluralName: "Schemas",
|
396
397
|
slug: "schema",
|
397
398
|
pluralSlug: "schemas",
|
399
|
+
identifiers: {
|
400
|
+
name: "name",
|
401
|
+
slug: "slug"
|
402
|
+
},
|
398
403
|
fields: [
|
399
404
|
...SYSTEM_FIELDS,
|
400
405
|
{ slug: "name", type: "string" },
|
@@ -403,7 +408,7 @@ var SYSTEM_SCHEMAS = [
|
|
403
408
|
{ slug: "pluralSlug", type: "string" },
|
404
409
|
{ slug: "idPrefix", type: "string" },
|
405
410
|
{ slug: "identifiers", type: "group" },
|
406
|
-
{ slug: "identifiers.
|
411
|
+
{ slug: "identifiers.name", type: "string" },
|
407
412
|
{ slug: "identifiers.slug", type: "string" },
|
408
413
|
{ slug: "fields", type: "json" },
|
409
414
|
{ slug: "indexes", type: "json" },
|
@@ -415,6 +420,10 @@ var SYSTEM_SCHEMAS = [
|
|
415
420
|
pluralName: "Fields",
|
416
421
|
slug: "field",
|
417
422
|
pluralSlug: "fields",
|
423
|
+
identifiers: {
|
424
|
+
name: "name",
|
425
|
+
slug: "slug"
|
426
|
+
},
|
418
427
|
fields: [
|
419
428
|
...SYSTEM_FIELDS,
|
420
429
|
{ slug: "name", type: "string" },
|
@@ -443,6 +452,10 @@ var SYSTEM_SCHEMAS = [
|
|
443
452
|
pluralName: "Indexes",
|
444
453
|
slug: "index",
|
445
454
|
pluralSlug: "indexes",
|
455
|
+
identifiers: {
|
456
|
+
name: "slug",
|
457
|
+
slug: "slug"
|
458
|
+
},
|
446
459
|
fields: [
|
447
460
|
...SYSTEM_FIELDS,
|
448
461
|
{ slug: "slug", type: "string", required: true },
|
@@ -461,6 +474,10 @@ var SYSTEM_SCHEMAS = [
|
|
461
474
|
pluralName: "Triggers",
|
462
475
|
slug: "trigger",
|
463
476
|
pluralSlug: "triggers",
|
477
|
+
identifiers: {
|
478
|
+
name: "slug",
|
479
|
+
slug: "slug"
|
480
|
+
},
|
464
481
|
fields: [
|
465
482
|
...SYSTEM_FIELDS,
|
466
483
|
{ slug: "slug", type: "string", required: true },
|
@@ -481,6 +498,10 @@ var prepareSchema = (schema) => {
|
|
481
498
|
if (!copiedSchema.name) copiedSchema.name = slugToName(copiedSchema.slug);
|
482
499
|
if (!copiedSchema.pluralName)
|
483
500
|
copiedSchema.pluralName = slugToName(copiedSchema.pluralSlug);
|
501
|
+
if (!copiedSchema.idPrefix) copiedSchema.idPrefix = copiedSchema.slug.slice(0, 3);
|
502
|
+
if (!copiedSchema.identifiers) copiedSchema.identifiers = {};
|
503
|
+
if (!copiedSchema.identifiers.name) copiedSchema.identifiers.name = "id";
|
504
|
+
if (!copiedSchema.identifiers.slug) copiedSchema.identifiers.slug = "id";
|
484
505
|
return copiedSchema;
|
485
506
|
};
|
486
507
|
var addSystemSchemas = (schemas) => {
|
@@ -496,6 +517,10 @@ var addSystemSchemas = (schemas) => {
|
|
496
517
|
list.push({
|
497
518
|
pluralSlug: fieldSlug,
|
498
519
|
slug: fieldSlug,
|
520
|
+
identifiers: {
|
521
|
+
name: "id",
|
522
|
+
slug: "id"
|
523
|
+
},
|
499
524
|
fields: [
|
500
525
|
{
|
501
526
|
slug: "source",
|
@@ -576,10 +601,15 @@ var getFieldStatement = (field) => {
|
|
576
601
|
}
|
577
602
|
return statement;
|
578
603
|
};
|
579
|
-
var addSchemaQueries = (schemas,
|
580
|
-
const {
|
581
|
-
|
582
|
-
|
604
|
+
var addSchemaQueries = (schemas, queryDetails, dependencyStatements) => {
|
605
|
+
const {
|
606
|
+
queryType,
|
607
|
+
querySchema,
|
608
|
+
queryInstructions: queryInstructionsRaw
|
609
|
+
} = queryDetails;
|
610
|
+
const queryInstructions = queryInstructionsRaw;
|
611
|
+
if (!["create", "set", "drop"].includes(queryType)) return queryInstructions;
|
612
|
+
if (!SYSTEM_SCHEMA_SLUGS.includes(querySchema)) return queryInstructions;
|
583
613
|
const instructionName = mappedInstructions[queryType];
|
584
614
|
const instructionList = queryInstructions[instructionName];
|
585
615
|
const kind = getSchemaBySlug(SYSTEM_SCHEMAS, querySchema).pluralSlug;
|
@@ -623,30 +653,33 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
623
653
|
fields: ["schema.slug"]
|
624
654
|
});
|
625
655
|
}
|
626
|
-
const
|
656
|
+
const usableSlug = kind === "schemas" ? slug : schemaSlug;
|
657
|
+
const tableName = convertToSnakeCase(pluralize(usableSlug));
|
658
|
+
const targetSchema = kind === "schemas" && queryType === "create" ? null : getSchemaBySlug(schemas, usableSlug);
|
627
659
|
if (kind === "indexes") {
|
628
660
|
const indexName = convertToSnakeCase(slug);
|
629
661
|
const unique = instructionList?.unique;
|
630
662
|
const filterQuery = instructionList?.filter;
|
663
|
+
const params = [];
|
631
664
|
let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
|
632
665
|
if (queryType === "create") {
|
633
666
|
statement2 += ` ON "${tableName}"`;
|
634
667
|
if (filterQuery) {
|
635
|
-
const targetSchema = getSchemaBySlug(schemas, schemaSlug);
|
636
668
|
const withStatement = handleWith(
|
637
669
|
schemas,
|
638
670
|
targetSchema,
|
639
|
-
|
671
|
+
params,
|
640
672
|
filterQuery
|
641
673
|
);
|
642
674
|
statement2 += ` WHERE (${withStatement})`;
|
643
675
|
}
|
644
676
|
}
|
645
|
-
|
646
|
-
return;
|
677
|
+
dependencyStatements.push({ statement: statement2, params });
|
678
|
+
return queryInstructions;
|
647
679
|
}
|
648
680
|
if (kind === "triggers") {
|
649
681
|
const triggerName = convertToSnakeCase(slug);
|
682
|
+
const params = [];
|
650
683
|
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
|
651
684
|
if (queryType === "create") {
|
652
685
|
const cause = slugToName(instructionList?.cause).toUpperCase();
|
@@ -657,46 +690,52 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
657
690
|
statementParts.push("FOR EACH ROW");
|
658
691
|
}
|
659
692
|
if (filterQuery) {
|
660
|
-
const targetSchema = getSchemaBySlug(schemas, schemaSlug);
|
661
693
|
const tablePlaceholder = cause.endsWith("DELETE") ? RONIN_SCHEMA_SYMBOLS.FIELD_OLD : RONIN_SCHEMA_SYMBOLS.FIELD_NEW;
|
662
694
|
const withStatement = handleWith(
|
663
695
|
schemas,
|
664
696
|
targetSchema,
|
665
|
-
|
697
|
+
params,
|
666
698
|
filterQuery,
|
667
699
|
tablePlaceholder
|
668
700
|
);
|
669
701
|
statementParts.push("WHEN", `(${withStatement})`);
|
670
702
|
}
|
671
703
|
const effectStatements = effectQueries.map((effectQuery) => {
|
672
|
-
return compileQueryInput(effectQuery, schemas, {
|
673
|
-
|
674
|
-
|
675
|
-
}).readStatement;
|
704
|
+
return compileQueryInput(effectQuery, schemas, params, {
|
705
|
+
returning: false
|
706
|
+
}).main.statement;
|
676
707
|
});
|
677
708
|
if (effectStatements.length > 1) statementParts.push("BEGIN");
|
678
709
|
statementParts.push(effectStatements.join("; "));
|
679
710
|
if (effectStatements.length > 1) statementParts.push("END");
|
680
711
|
statement2 += ` ${statementParts.join(" ")}`;
|
681
712
|
}
|
682
|
-
|
683
|
-
return;
|
713
|
+
dependencyStatements.push({ statement: statement2, params });
|
714
|
+
return queryInstructions;
|
684
715
|
}
|
685
716
|
let statement = `${tableAction} TABLE "${tableName}"`;
|
686
717
|
if (kind === "schemas") {
|
687
|
-
const
|
718
|
+
const providedFields = instructionList?.fields || [];
|
719
|
+
const fields = [...SYSTEM_FIELDS, ...providedFields];
|
720
|
+
if (queryType === "create" || queryType === "set") {
|
721
|
+
queryInstructions.to = prepareSchema(queryInstructions.to);
|
722
|
+
}
|
688
723
|
if (queryType === "create") {
|
689
724
|
const columns = fields.map(getFieldStatement).filter(Boolean);
|
690
725
|
statement += ` (${columns.join(", ")})`;
|
726
|
+
schemas.push(queryInstructions.to);
|
691
727
|
} else if (queryType === "set") {
|
692
|
-
const newSlug = queryInstructions.to?.
|
728
|
+
const newSlug = queryInstructions.to?.pluralSlug;
|
693
729
|
if (newSlug) {
|
694
|
-
const newTable = convertToSnakeCase(
|
730
|
+
const newTable = convertToSnakeCase(newSlug);
|
695
731
|
statement += ` RENAME TO "${newTable}"`;
|
696
732
|
}
|
733
|
+
Object.assign(targetSchema, queryInstructions.to);
|
734
|
+
} else if (queryType === "drop") {
|
735
|
+
schemas.splice(schemas.indexOf(targetSchema), 1);
|
697
736
|
}
|
698
|
-
|
699
|
-
return;
|
737
|
+
dependencyStatements.push({ statement, params: [] });
|
738
|
+
return queryInstructions;
|
700
739
|
}
|
701
740
|
if (kind === "fields") {
|
702
741
|
if (queryType === "create") {
|
@@ -716,8 +755,9 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
716
755
|
} else if (queryType === "drop") {
|
717
756
|
statement += ` DROP COLUMN "${slug}"`;
|
718
757
|
}
|
719
|
-
|
758
|
+
dependencyStatements.push({ statement, params: [] });
|
720
759
|
}
|
760
|
+
return queryInstructions;
|
721
761
|
};
|
722
762
|
var slugToName = (slug) => {
|
723
763
|
const name = slug.replace(/([a-z])([A-Z])/g, "$1 $2");
|
@@ -739,7 +779,7 @@ var pluralize = (word) => {
|
|
739
779
|
// src/instructions/before-after.ts
|
740
780
|
var CURSOR_SEPARATOR = ",";
|
741
781
|
var CURSOR_NULL_PLACEHOLDER = "RONIN_NULL";
|
742
|
-
var handleBeforeOrAfter = (schema,
|
782
|
+
var handleBeforeOrAfter = (schema, statementParams, instructions, rootTable) => {
|
743
783
|
if (!(instructions.before || instructions.after)) {
|
744
784
|
throw new RoninError({
|
745
785
|
message: "The `before` or `after` instruction must not be empty.",
|
@@ -765,15 +805,15 @@ var handleBeforeOrAfter = (schema, statementValues, instructions, rootTable) =>
|
|
765
805
|
}
|
766
806
|
const { field } = getFieldFromSchema(schema, key, "orderedBy");
|
767
807
|
if (field.type === "boolean") {
|
768
|
-
return prepareStatementValue(
|
808
|
+
return prepareStatementValue(statementParams, value === "true");
|
769
809
|
}
|
770
810
|
if (field.type === "number") {
|
771
|
-
return prepareStatementValue(
|
811
|
+
return prepareStatementValue(statementParams, Number.parseInt(value));
|
772
812
|
}
|
773
813
|
if (field.type === "date") {
|
774
814
|
return `'${new Date(Number.parseInt(value)).toJSON()}'`;
|
775
815
|
}
|
776
|
-
return prepareStatementValue(
|
816
|
+
return prepareStatementValue(statementParams, value);
|
777
817
|
});
|
778
818
|
const compareOperators = [
|
779
819
|
// Reverse the comparison operators if we're querying for records before.
|
@@ -816,7 +856,7 @@ var handleBeforeOrAfter = (schema, statementValues, instructions, rootTable) =>
|
|
816
856
|
};
|
817
857
|
|
818
858
|
// src/instructions/for.ts
|
819
|
-
var handleFor = (schemas, schema,
|
859
|
+
var handleFor = (schemas, schema, statementParams, instruction, rootTable) => {
|
820
860
|
let statement = "";
|
821
861
|
if (!instruction) return statement;
|
822
862
|
for (const shortcut in instruction) {
|
@@ -837,7 +877,7 @@ var handleFor = (schemas, schema, statementValues, instruction, rootTable) => {
|
|
837
877
|
const subStatement = composeConditions(
|
838
878
|
schemas,
|
839
879
|
schema,
|
840
|
-
|
880
|
+
statementParams,
|
841
881
|
"for",
|
842
882
|
replacedForFilter,
|
843
883
|
{ rootTable }
|
@@ -848,7 +888,7 @@ var handleFor = (schemas, schema, statementValues, instruction, rootTable) => {
|
|
848
888
|
};
|
849
889
|
|
850
890
|
// src/instructions/including.ts
|
851
|
-
var handleIncluding = (schemas,
|
891
|
+
var handleIncluding = (schemas, statementParams, schema, instruction, rootTable) => {
|
852
892
|
let statement = "";
|
853
893
|
let rootTableSubQuery;
|
854
894
|
let rootTableName = rootTable;
|
@@ -882,9 +922,9 @@ var handleIncluding = (schemas, statementValues, schema, instruction, rootTable)
|
|
882
922
|
}
|
883
923
|
},
|
884
924
|
schemas,
|
885
|
-
|
925
|
+
statementParams
|
886
926
|
);
|
887
|
-
relatedTableSelector = `(${subSelect.
|
927
|
+
relatedTableSelector = `(${subSelect.main.statement})`;
|
888
928
|
}
|
889
929
|
statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;
|
890
930
|
if (joinType === "LEFT") {
|
@@ -895,7 +935,7 @@ var handleIncluding = (schemas, statementValues, schema, instruction, rootTable)
|
|
895
935
|
const subStatement = composeConditions(
|
896
936
|
schemas,
|
897
937
|
relatedSchema,
|
898
|
-
|
938
|
+
statementParams,
|
899
939
|
"including",
|
900
940
|
queryInstructions?.with,
|
901
941
|
{
|
@@ -949,7 +989,7 @@ var handleOrderedBy = (schema, instruction, rootTable) => {
|
|
949
989
|
};
|
950
990
|
|
951
991
|
// src/instructions/selecting.ts
|
952
|
-
var handleSelecting = (schema,
|
992
|
+
var handleSelecting = (schema, statementParams, instructions) => {
|
953
993
|
let statement = instructions.selecting ? instructions.selecting.map((slug) => {
|
954
994
|
return getFieldFromSchema(schema, slug, "selecting").fieldSelector;
|
955
995
|
}).join(", ") : "*";
|
@@ -960,14 +1000,14 @@ var handleSelecting = (schema, statementValues, instructions) => {
|
|
960
1000
|
).filter(([_, value]) => {
|
961
1001
|
return !(isObject(value) && Object.hasOwn(value, RONIN_SCHEMA_SYMBOLS.QUERY));
|
962
1002
|
}).map(([key, value]) => {
|
963
|
-
return `${prepareStatementValue(
|
1003
|
+
return `${prepareStatementValue(statementParams, value)} as "${key}"`;
|
964
1004
|
}).join(", ");
|
965
1005
|
}
|
966
1006
|
return statement;
|
967
1007
|
};
|
968
1008
|
|
969
1009
|
// src/instructions/to.ts
|
970
|
-
var handleTo = (schemas, schema,
|
1010
|
+
var handleTo = (schemas, schema, statementParams, queryType, dependencyStatements, instructions, rootTable) => {
|
971
1011
|
const currentTime = (/* @__PURE__ */ new Date()).toISOString();
|
972
1012
|
const { with: withInstruction, to: toInstruction } = instructions;
|
973
1013
|
const defaultFields = {};
|
@@ -1009,9 +1049,7 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1009
1049
|
...subQueryInstructions.including
|
1010
1050
|
};
|
1011
1051
|
}
|
1012
|
-
return compileQueryInput(subQuery, schemas,
|
1013
|
-
statementValues
|
1014
|
-
}).readStatement;
|
1052
|
+
return compileQueryInput(subQuery, schemas, statementParams).main.statement;
|
1015
1053
|
}
|
1016
1054
|
Object.assign(toInstruction, defaultFields);
|
1017
1055
|
for (const fieldSlug in toInstruction) {
|
@@ -1027,28 +1065,28 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1027
1065
|
const source = queryType === "create" ? { id: toInstruction.id } : withInstruction;
|
1028
1066
|
const recordDetails = { source };
|
1029
1067
|
if (value) recordDetails.target = value;
|
1030
|
-
|
1068
|
+
return compileQueryInput(
|
1031
1069
|
{
|
1032
1070
|
[subQueryType]: {
|
1033
1071
|
[associativeSchemaSlug]: subQueryType === "create" ? { to: recordDetails } : { with: recordDetails }
|
1034
1072
|
}
|
1035
1073
|
},
|
1036
1074
|
schemas,
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1075
|
+
[],
|
1076
|
+
{ returning: false }
|
1077
|
+
).main;
|
1040
1078
|
};
|
1041
1079
|
if (Array.isArray(fieldValue)) {
|
1042
|
-
|
1080
|
+
dependencyStatements.push(composeStatement("drop"));
|
1043
1081
|
for (const record of fieldValue) {
|
1044
|
-
|
1082
|
+
dependencyStatements.push(composeStatement("create", record));
|
1045
1083
|
}
|
1046
1084
|
} else if (isObject(fieldValue)) {
|
1047
1085
|
for (const recordToAdd of fieldValue.containing || []) {
|
1048
|
-
|
1086
|
+
dependencyStatements.push(composeStatement("create", recordToAdd));
|
1049
1087
|
}
|
1050
1088
|
for (const recordToRemove of fieldValue.notContaining || []) {
|
1051
|
-
|
1089
|
+
dependencyStatements.push(composeStatement("drop", recordToRemove));
|
1052
1090
|
}
|
1053
1091
|
}
|
1054
1092
|
}
|
@@ -1056,7 +1094,7 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1056
1094
|
let statement = composeConditions(
|
1057
1095
|
schemas,
|
1058
1096
|
schema,
|
1059
|
-
|
1097
|
+
statementParams,
|
1060
1098
|
"to",
|
1061
1099
|
toInstruction,
|
1062
1100
|
{
|
@@ -1068,7 +1106,7 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1068
1106
|
const deepStatement = composeConditions(
|
1069
1107
|
schemas,
|
1070
1108
|
schema,
|
1071
|
-
|
1109
|
+
statementParams,
|
1072
1110
|
"to",
|
1073
1111
|
toInstruction,
|
1074
1112
|
{
|
@@ -1083,19 +1121,22 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1083
1121
|
return statement;
|
1084
1122
|
};
|
1085
1123
|
|
1086
|
-
// src/index.ts
|
1087
|
-
var compileQueryInput = (query,
|
1124
|
+
// src/utils/index.ts
|
1125
|
+
var compileQueryInput = (query, schemas, statementParams, options) => {
|
1088
1126
|
const parsedQuery = splitQuery(query);
|
1089
1127
|
const { queryType, querySchema, queryInstructions } = parsedQuery;
|
1090
|
-
const schemas = addSystemSchemas(defaultSchemas);
|
1091
1128
|
const schema = getSchemaBySlug(schemas, querySchema);
|
1092
1129
|
const single = querySchema !== schema.pluralSlug;
|
1093
1130
|
let instructions = formatIdentifiers(schema, queryInstructions);
|
1094
1131
|
let table = getTableForSchema(schema);
|
1095
|
-
const
|
1096
|
-
const
|
1097
|
-
addSchemaQueries(
|
1098
|
-
|
1132
|
+
const dependencyStatements = [];
|
1133
|
+
const returning = options?.returning ?? true;
|
1134
|
+
instructions = addSchemaQueries(
|
1135
|
+
schemas,
|
1136
|
+
{ queryType, querySchema, queryInstructions: instructions },
|
1137
|
+
dependencyStatements
|
1138
|
+
);
|
1139
|
+
const columns = handleSelecting(schema, statementParams, {
|
1099
1140
|
selecting: instructions?.selecting,
|
1100
1141
|
including: instructions?.including
|
1101
1142
|
});
|
@@ -1124,7 +1165,7 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1124
1165
|
statement: including,
|
1125
1166
|
rootTableSubQuery,
|
1126
1167
|
rootTableName
|
1127
|
-
} = handleIncluding(schemas,
|
1168
|
+
} = handleIncluding(schemas, statementParams, schema, instructions?.including, table);
|
1128
1169
|
if (rootTableSubQuery && rootTableName) {
|
1129
1170
|
table = rootTableName;
|
1130
1171
|
statement += `(${rootTableSubQuery}) as ${rootTableName} `;
|
@@ -1147,9 +1188,9 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1147
1188
|
const toStatement = handleTo(
|
1148
1189
|
schemas,
|
1149
1190
|
schema,
|
1150
|
-
|
1191
|
+
statementParams,
|
1151
1192
|
queryType,
|
1152
|
-
|
1193
|
+
dependencyStatements,
|
1153
1194
|
{ with: instructions.with, to: instructions.to },
|
1154
1195
|
isJoining ? table : void 0
|
1155
1196
|
);
|
@@ -1160,7 +1201,7 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1160
1201
|
const withStatement = handleWith(
|
1161
1202
|
schemas,
|
1162
1203
|
schema,
|
1163
|
-
|
1204
|
+
statementParams,
|
1164
1205
|
instructions?.with,
|
1165
1206
|
isJoining ? table : void 0
|
1166
1207
|
);
|
@@ -1170,7 +1211,7 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1170
1211
|
const forStatement = handleFor(
|
1171
1212
|
schemas,
|
1172
1213
|
schema,
|
1173
|
-
|
1214
|
+
statementParams,
|
1174
1215
|
instructions?.for,
|
1175
1216
|
isJoining ? table : void 0
|
1176
1217
|
);
|
@@ -1198,7 +1239,7 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1198
1239
|
}
|
1199
1240
|
const beforeAndAfterStatement = handleBeforeOrAfter(
|
1200
1241
|
schema,
|
1201
|
-
|
1242
|
+
statementParams,
|
1202
1243
|
{
|
1203
1244
|
before: instructions.before,
|
1204
1245
|
after: instructions.after,
|
@@ -1227,16 +1268,36 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1227
1268
|
if (queryType === "get" && !isJoiningMultipleRows) {
|
1228
1269
|
statement += handleLimitedTo(single, instructions?.limitedTo);
|
1229
1270
|
}
|
1230
|
-
if (["create", "set", "drop"].includes(queryType) &&
|
1271
|
+
if (["create", "set", "drop"].includes(queryType) && returning) {
|
1231
1272
|
statement += "RETURNING * ";
|
1232
1273
|
}
|
1233
|
-
const
|
1274
|
+
const mainStatement = {
|
1275
|
+
statement: statement.trimEnd(),
|
1276
|
+
params: statementParams || []
|
1277
|
+
};
|
1278
|
+
if (returning) mainStatement.returning = true;
|
1234
1279
|
return {
|
1235
|
-
|
1236
|
-
|
1237
|
-
values: statementValues
|
1280
|
+
dependencies: dependencyStatements,
|
1281
|
+
main: mainStatement
|
1238
1282
|
};
|
1239
1283
|
};
|
1284
|
+
|
1285
|
+
// src/index.ts
|
1286
|
+
var compileQueries = (queries, schemas, options) => {
|
1287
|
+
const schemaList = addSystemSchemas(schemas);
|
1288
|
+
const dependencyStatements = [];
|
1289
|
+
const mainStatements = [];
|
1290
|
+
for (const query of queries) {
|
1291
|
+
const result = compileQueryInput(
|
1292
|
+
query,
|
1293
|
+
schemaList,
|
1294
|
+
options?.inlineValues ? null : []
|
1295
|
+
);
|
1296
|
+
dependencyStatements.push(...result.dependencies);
|
1297
|
+
mainStatements.push(result.main);
|
1298
|
+
}
|
1299
|
+
return [...dependencyStatements, ...mainStatements];
|
1300
|
+
};
|
1240
1301
|
export {
|
1241
|
-
|
1302
|
+
compileQueries
|
1242
1303
|
};
|