@ronin/compiler 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +17 -2
- package/dist/index.d.ts +14273 -8
- package/dist/index.js +80 -34
- 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,8 +96,9 @@ var splitQuery = (query) => {
|
|
96
96
|
};
|
97
97
|
|
98
98
|
// src/utils/statement.ts
|
99
|
-
var prepareStatementValue = (statementValues, value
|
100
|
-
if (
|
99
|
+
var prepareStatementValue = (statementValues, value) => {
|
100
|
+
if (value === null) return "NULL";
|
101
|
+
if (!statementValues) return JSON.stringify(value);
|
101
102
|
let formattedValue = value;
|
102
103
|
if (Array.isArray(value) || isObject(value)) {
|
103
104
|
formattedValue = JSON.stringify(value);
|
@@ -122,7 +123,7 @@ var composeFieldValues = (schemas, schema, statementValues, instructionName, val
|
|
122
123
|
conditionValue = `(${compileQueryInput(
|
123
124
|
value[RONIN_SCHEMA_SYMBOLS.QUERY],
|
124
125
|
schemas,
|
125
|
-
|
126
|
+
statementValues
|
126
127
|
).readStatement})`;
|
127
128
|
} else if (typeof value === "string" && value.startsWith(RONIN_SCHEMA_SYMBOLS.FIELD)) {
|
128
129
|
let targetTable = `"${options.rootTable}"`;
|
@@ -139,11 +140,11 @@ 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(statementValues, value
|
143
|
+
const preparedValue = prepareStatementValue(statementValues, value);
|
143
144
|
conditionValue = `IIF(${conditionSelector} IS NULL, ${preparedValue}, json_patch(${conditionSelector}, ${preparedValue}))`;
|
144
145
|
}
|
145
146
|
} else if (collectStatementValue) {
|
146
|
-
conditionValue = prepareStatementValue(statementValues, value
|
147
|
+
conditionValue = prepareStatementValue(statementValues, value);
|
147
148
|
}
|
148
149
|
if (options.type === "fields") return conditionSelector;
|
149
150
|
if (options.type === "values") return conditionValue;
|
@@ -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
|
}
|
@@ -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,8 +408,11 @@ var SYSTEM_SCHEMAS = [
|
|
403
408
|
{ slug: "pluralSlug", type: "string" },
|
404
409
|
{ slug: "idPrefix", type: "string" },
|
405
410
|
{ slug: "identifiers", type: "group" },
|
406
|
-
{ slug: "identifiers.
|
407
|
-
{ slug: "identifiers.slug", type: "string" }
|
411
|
+
{ slug: "identifiers.name", type: "string" },
|
412
|
+
{ slug: "identifiers.slug", type: "string" },
|
413
|
+
{ slug: "fields", type: "json" },
|
414
|
+
{ slug: "indexes", type: "json" },
|
415
|
+
{ slug: "triggers", type: "json" }
|
408
416
|
]
|
409
417
|
},
|
410
418
|
{
|
@@ -412,6 +420,10 @@ var SYSTEM_SCHEMAS = [
|
|
412
420
|
pluralName: "Fields",
|
413
421
|
slug: "field",
|
414
422
|
pluralSlug: "fields",
|
423
|
+
identifiers: {
|
424
|
+
name: "name",
|
425
|
+
slug: "slug"
|
426
|
+
},
|
415
427
|
fields: [
|
416
428
|
...SYSTEM_FIELDS,
|
417
429
|
{ slug: "name", type: "string" },
|
@@ -440,6 +452,10 @@ var SYSTEM_SCHEMAS = [
|
|
440
452
|
pluralName: "Indexes",
|
441
453
|
slug: "index",
|
442
454
|
pluralSlug: "indexes",
|
455
|
+
identifiers: {
|
456
|
+
name: "slug",
|
457
|
+
slug: "slug"
|
458
|
+
},
|
443
459
|
fields: [
|
444
460
|
...SYSTEM_FIELDS,
|
445
461
|
{ slug: "slug", type: "string", required: true },
|
@@ -458,6 +474,10 @@ var SYSTEM_SCHEMAS = [
|
|
458
474
|
pluralName: "Triggers",
|
459
475
|
slug: "trigger",
|
460
476
|
pluralSlug: "triggers",
|
477
|
+
identifiers: {
|
478
|
+
name: "slug",
|
479
|
+
slug: "slug"
|
480
|
+
},
|
461
481
|
fields: [
|
462
482
|
...SYSTEM_FIELDS,
|
463
483
|
{ slug: "slug", type: "string", required: true },
|
@@ -478,6 +498,10 @@ var prepareSchema = (schema) => {
|
|
478
498
|
if (!copiedSchema.name) copiedSchema.name = slugToName(copiedSchema.slug);
|
479
499
|
if (!copiedSchema.pluralName)
|
480
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";
|
481
505
|
return copiedSchema;
|
482
506
|
};
|
483
507
|
var addSystemSchemas = (schemas) => {
|
@@ -493,6 +517,10 @@ var addSystemSchemas = (schemas) => {
|
|
493
517
|
list.push({
|
494
518
|
pluralSlug: fieldSlug,
|
495
519
|
slug: fieldSlug,
|
520
|
+
identifiers: {
|
521
|
+
name: "id",
|
522
|
+
slug: "id"
|
523
|
+
},
|
496
524
|
fields: [
|
497
525
|
{
|
498
526
|
slug: "source",
|
@@ -574,9 +602,14 @@ var getFieldStatement = (field) => {
|
|
574
602
|
return statement;
|
575
603
|
};
|
576
604
|
var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements) => {
|
577
|
-
const {
|
578
|
-
|
579
|
-
|
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;
|
580
613
|
const instructionName = mappedInstructions[queryType];
|
581
614
|
const instructionList = queryInstructions[instructionName];
|
582
615
|
const kind = getSchemaBySlug(SYSTEM_SCHEMAS, querySchema).pluralSlug;
|
@@ -640,7 +673,7 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
640
673
|
}
|
641
674
|
}
|
642
675
|
writeStatements.push(statement2);
|
643
|
-
return;
|
676
|
+
return queryInstructions;
|
644
677
|
}
|
645
678
|
if (kind === "triggers") {
|
646
679
|
const triggerName = convertToSnakeCase(slug);
|
@@ -666,8 +699,7 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
666
699
|
statementParts.push("WHEN", `(${withStatement})`);
|
667
700
|
}
|
668
701
|
const effectStatements = effectQueries.map((effectQuery) => {
|
669
|
-
return compileQueryInput(effectQuery, schemas, {
|
670
|
-
statementValues,
|
702
|
+
return compileQueryInput(effectQuery, schemas, statementValues, {
|
671
703
|
disableReturning: true
|
672
704
|
}).readStatement;
|
673
705
|
});
|
@@ -677,23 +709,27 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
677
709
|
statement2 += ` ${statementParts.join(" ")}`;
|
678
710
|
}
|
679
711
|
writeStatements.push(statement2);
|
680
|
-
return;
|
712
|
+
return queryInstructions;
|
681
713
|
}
|
682
714
|
let statement = `${tableAction} TABLE "${tableName}"`;
|
683
715
|
if (kind === "schemas") {
|
684
|
-
const
|
716
|
+
const providedFields = instructionList?.fields || [];
|
717
|
+
const fields = [...SYSTEM_FIELDS, ...providedFields];
|
718
|
+
if (queryType === "create" || queryType === "set") {
|
719
|
+
queryInstructions.to = prepareSchema(queryInstructions.to);
|
720
|
+
}
|
685
721
|
if (queryType === "create") {
|
686
722
|
const columns = fields.map(getFieldStatement).filter(Boolean);
|
687
723
|
statement += ` (${columns.join(", ")})`;
|
688
724
|
} else if (queryType === "set") {
|
689
|
-
const newSlug = queryInstructions.to?.
|
725
|
+
const newSlug = queryInstructions.to?.pluralSlug;
|
690
726
|
if (newSlug) {
|
691
|
-
const newTable = convertToSnakeCase(
|
727
|
+
const newTable = convertToSnakeCase(newSlug);
|
692
728
|
statement += ` RENAME TO "${newTable}"`;
|
693
729
|
}
|
694
730
|
}
|
695
731
|
writeStatements.push(statement);
|
696
|
-
return;
|
732
|
+
return queryInstructions;
|
697
733
|
}
|
698
734
|
if (kind === "fields") {
|
699
735
|
if (queryType === "create") {
|
@@ -715,6 +751,7 @@ var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements)
|
|
715
751
|
}
|
716
752
|
writeStatements.push(statement);
|
717
753
|
}
|
754
|
+
return queryInstructions;
|
718
755
|
};
|
719
756
|
var slugToName = (slug) => {
|
720
757
|
const name = slug.replace(/([a-z])([A-Z])/g, "$1 $2");
|
@@ -879,7 +916,7 @@ var handleIncluding = (schemas, statementValues, schema, instruction, rootTable)
|
|
879
916
|
}
|
880
917
|
},
|
881
918
|
schemas,
|
882
|
-
|
919
|
+
statementValues
|
883
920
|
);
|
884
921
|
relatedTableSelector = `(${subSelect.readStatement})`;
|
885
922
|
}
|
@@ -1006,9 +1043,7 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1006
1043
|
...subQueryInstructions.including
|
1007
1044
|
};
|
1008
1045
|
}
|
1009
|
-
return compileQueryInput(subQuery, schemas,
|
1010
|
-
statementValues
|
1011
|
-
}).readStatement;
|
1046
|
+
return compileQueryInput(subQuery, schemas, statementValues).readStatement;
|
1012
1047
|
}
|
1013
1048
|
Object.assign(toInstruction, defaultFields);
|
1014
1049
|
for (const fieldSlug in toInstruction) {
|
@@ -1031,7 +1066,8 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1031
1066
|
}
|
1032
1067
|
},
|
1033
1068
|
schemas,
|
1034
|
-
|
1069
|
+
statementValues,
|
1070
|
+
{ disableReturning: true }
|
1035
1071
|
);
|
1036
1072
|
return readStatement;
|
1037
1073
|
};
|
@@ -1080,8 +1116,8 @@ var handleTo = (schemas, schema, statementValues, queryType, writeStatements, in
|
|
1080
1116
|
return statement;
|
1081
1117
|
};
|
1082
1118
|
|
1083
|
-
// src/index.ts
|
1084
|
-
var compileQueryInput = (query, defaultSchemas, options) => {
|
1119
|
+
// src/utils/index.ts
|
1120
|
+
var compileQueryInput = (query, defaultSchemas, statementValues, options) => {
|
1085
1121
|
const parsedQuery = splitQuery(query);
|
1086
1122
|
const { queryType, querySchema, queryInstructions } = parsedQuery;
|
1087
1123
|
const schemas = addSystemSchemas(defaultSchemas);
|
@@ -1089,9 +1125,13 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1089
1125
|
const single = querySchema !== schema.pluralSlug;
|
1090
1126
|
let instructions = formatIdentifiers(schema, queryInstructions);
|
1091
1127
|
let table = getTableForSchema(schema);
|
1092
|
-
const statementValues = options?.statementValues || [];
|
1093
1128
|
const writeStatements = [];
|
1094
|
-
addSchemaQueries(
|
1129
|
+
instructions = addSchemaQueries(
|
1130
|
+
schemas,
|
1131
|
+
statementValues,
|
1132
|
+
{ queryType, querySchema, queryInstructions: instructions },
|
1133
|
+
writeStatements
|
1134
|
+
);
|
1095
1135
|
const columns = handleSelecting(schema, statementValues, {
|
1096
1136
|
selecting: instructions?.selecting,
|
1097
1137
|
including: instructions?.including
|
@@ -1231,9 +1271,15 @@ var compileQueryInput = (query, defaultSchemas, options) => {
|
|
1231
1271
|
return {
|
1232
1272
|
writeStatements,
|
1233
1273
|
readStatement: finalStatement,
|
1234
|
-
values: statementValues
|
1274
|
+
values: statementValues || []
|
1235
1275
|
};
|
1236
1276
|
};
|
1277
|
+
|
1278
|
+
// src/index.ts
|
1279
|
+
var compileQuery = (query, schemas, options) => {
|
1280
|
+
const statementValues = options?.inlineValues ? null : [];
|
1281
|
+
return compileQueryInput(query, schemas, statementValues);
|
1282
|
+
};
|
1237
1283
|
export {
|
1238
|
-
|
1284
|
+
compileQuery
|
1239
1285
|
};
|