@ronin/compiler 0.18.2 → 0.18.3
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/dist/index.d.ts +3 -34
- package/dist/index.js +21 -89
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -21,14 +21,12 @@ declare const QUERY_SYMBOLS: {
|
|
21
21
|
readonly EXPRESSION: "__RONIN_EXPRESSION";
|
22
22
|
readonly FIELD: "__RONIN_FIELD_";
|
23
23
|
readonly FIELD_PARENT: "__RONIN_FIELD_PARENT_";
|
24
|
-
readonly FIELD_PARENT_OLD: "__RONIN_FIELD_PARENT_OLD_";
|
25
|
-
readonly FIELD_PARENT_NEW: "__RONIN_FIELD_PARENT_NEW_";
|
26
24
|
readonly VALUE: "__RONIN_VALUE";
|
27
25
|
};
|
28
26
|
|
29
27
|
type QueryTypeEnum = (typeof DML_QUERY_TYPES)[number];
|
30
28
|
type ModelQueryTypeEnum = (typeof DDL_QUERY_TYPES)[number];
|
31
|
-
type ModelEntityEnum = 'field' | 'index' | '
|
29
|
+
type ModelEntityEnum = 'field' | 'index' | 'preset';
|
32
30
|
type FieldValue = string | number | boolean | null | unknown;
|
33
31
|
type FieldSelector = Record<string, FieldValue | StoredObject>;
|
34
32
|
type StoredObject = {
|
@@ -126,11 +124,10 @@ type CreateQuery = {
|
|
126
124
|
};
|
127
125
|
type AlterQuery = {
|
128
126
|
model: string;
|
129
|
-
to?: Partial<Omit<PublicModel, 'fields' | 'indexes' | '
|
127
|
+
to?: Partial<Omit<PublicModel, 'fields' | 'indexes' | 'presets' | 'idPrefix'>>;
|
130
128
|
create?: {
|
131
129
|
field?: Omit<ModelField, 'system'>;
|
132
130
|
index?: Omit<ModelIndex, 'system'>;
|
133
|
-
trigger?: Omit<ModelTrigger, 'system'>;
|
134
131
|
preset?: Omit<ModelPreset, 'system'>;
|
135
132
|
};
|
136
133
|
alter?: {
|
@@ -139,9 +136,6 @@ type AlterQuery = {
|
|
139
136
|
} | {
|
140
137
|
index?: string;
|
141
138
|
to?: Partial<Omit<ModelIndex, 'system'>>;
|
142
|
-
} | {
|
143
|
-
trigger?: string;
|
144
|
-
to?: Omit<ModelTrigger, 'system'>;
|
145
139
|
} | {
|
146
140
|
preset?: string;
|
147
141
|
to?: Omit<ModelPreset, 'system'>;
|
@@ -298,30 +292,6 @@ type ModelIndex<T extends ModelEntityList<ModelField> = ModelEntityList<ModelFie
|
|
298
292
|
*/
|
299
293
|
filter?: WithInstruction;
|
300
294
|
};
|
301
|
-
type ModelTriggerField<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = {
|
302
|
-
/**
|
303
|
-
* The slug of the field that should cause the trigger to fire if the value of the
|
304
|
-
* field has changed.
|
305
|
-
*/
|
306
|
-
slug: keyof T;
|
307
|
-
};
|
308
|
-
type ModelTrigger<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = {
|
309
|
-
/** The identifier of the trigger. */
|
310
|
-
slug: string;
|
311
|
-
/** The type of query for which the trigger should fire. */
|
312
|
-
action: 'INSERT' | 'UPDATE' | 'DELETE';
|
313
|
-
/** When the trigger should fire in the case that a matching query is executed. */
|
314
|
-
when: 'BEFORE' | 'DURING' | 'AFTER';
|
315
|
-
/** A list of queries that should be executed when the trigger fires. */
|
316
|
-
effects: Array<Record<typeof QUERY_SYMBOLS.QUERY, Query>>;
|
317
|
-
/** A list of field slugs for which the trigger should fire. */
|
318
|
-
fields?: Array<ModelTriggerField<T>>;
|
319
|
-
/**
|
320
|
-
* An object containing query instructions used to determine whether the trigger should
|
321
|
-
* fire, or not.
|
322
|
-
*/
|
323
|
-
filter?: WithInstruction;
|
324
|
-
};
|
325
295
|
type ModelPreset = {
|
326
296
|
/** The visual display name of the preset. */
|
327
297
|
name?: string;
|
@@ -369,7 +339,6 @@ interface Model<T extends ModelEntityList<ModelField> = ModelEntityList<ModelFie
|
|
369
339
|
};
|
370
340
|
fields: T;
|
371
341
|
indexes?: ModelEntityList<ModelIndex<T>>;
|
372
|
-
triggers?: ModelEntityList<ModelTrigger<T>>;
|
373
342
|
presets?: ModelEntityList<ModelPreset>;
|
374
343
|
}
|
375
344
|
type PublicModel<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = Omit<Partial<Model<T>>, 'slug' | 'identifiers' | 'system' | 'tableAlias'> & {
|
@@ -499,4 +468,4 @@ declare class Transaction {
|
|
499
468
|
|
500
469
|
declare const CLEAN_ROOT_MODEL: PublicModel;
|
501
470
|
|
502
|
-
export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, DDL_QUERY_TYPES, DML_QUERY_TYPES, DML_QUERY_TYPES_READ, DML_QUERY_TYPES_WRITE, type DropQuery, type ExpandedResult, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type ListQuery, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset,
|
471
|
+
export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, DDL_QUERY_TYPES, DML_QUERY_TYPES, DML_QUERY_TYPES_READ, DML_QUERY_TYPES_WRITE, type DropQuery, type ExpandedResult, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type ListQuery, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, QUERY_SYMBOLS, QUERY_TYPES, type Query, type QueryInstructionType as QueryInstruction, type QuerySchemaType, type QueryType, CLEAN_ROOT_MODEL as ROOT_MODEL, type RegularResult, type RemoveInstructions, type RemoveQuery, type RemoveInstructions as RemoveQueryInstructions, type Result, type ResultRecord, type ResultRecordBase, RoninError, type SetInstructions, type SetQuery, type SetInstructions as SetQueryInstructions, type Statement, type StoredObject, Transaction, type WithInstruction, getQuerySymbol };
|
package/dist/index.js
CHANGED
@@ -16,10 +16,6 @@ var QUERY_SYMBOLS = {
|
|
16
16
|
FIELD: "__RONIN_FIELD_",
|
17
17
|
// Represents the value of a field in the model of a parent query.
|
18
18
|
FIELD_PARENT: "__RONIN_FIELD_PARENT_",
|
19
|
-
// Represents the old value of a field in the parent model. Used for triggers.
|
20
|
-
FIELD_PARENT_OLD: "__RONIN_FIELD_PARENT_OLD_",
|
21
|
-
// Represents the new value of a field in the parent model. Used for triggers.
|
22
|
-
FIELD_PARENT_NEW: "__RONIN_FIELD_PARENT_NEW_",
|
23
19
|
// Represents a value provided to a query preset.
|
24
20
|
VALUE: "__RONIN_VALUE"
|
25
21
|
};
|
@@ -46,7 +42,6 @@ var composeMountingPath = (single, key, mountingPath) => {
|
|
46
42
|
var MODEL_ENTITY_ERROR_CODES = {
|
47
43
|
field: "FIELD_NOT_FOUND",
|
48
44
|
index: "INDEX_NOT_FOUND",
|
49
|
-
trigger: "TRIGGER_NOT_FOUND",
|
50
45
|
preset: "PRESET_NOT_FOUND"
|
51
46
|
};
|
52
47
|
var RoninError = class extends Error {
|
@@ -1318,11 +1313,6 @@ var parseFieldExpression = (model, instructionName, expression, parentModel) =>
|
|
1318
1313
|
if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT)) {
|
1319
1314
|
rootModel = parentModel;
|
1320
1315
|
toReplace = QUERY_SYMBOLS.FIELD_PARENT;
|
1321
|
-
if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT_OLD)) {
|
1322
|
-
rootModel.tableAlias = toReplace = QUERY_SYMBOLS.FIELD_PARENT_OLD;
|
1323
|
-
} else if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT_NEW)) {
|
1324
|
-
rootModel.tableAlias = toReplace = QUERY_SYMBOLS.FIELD_PARENT_NEW;
|
1325
|
-
}
|
1326
1316
|
}
|
1327
1317
|
const fieldSlug = match.replace(toReplace, "");
|
1328
1318
|
const field = getFieldFromModel(rootModel, fieldSlug, { instructionName });
|
@@ -1622,7 +1612,6 @@ var ROOT_MODEL = {
|
|
1622
1612
|
// which makes the statement shorter.
|
1623
1613
|
fields: { type: "json", defaultValue: {} },
|
1624
1614
|
indexes: { type: "json", defaultValue: {} },
|
1625
|
-
triggers: { type: "json", defaultValue: {} },
|
1626
1615
|
presets: { type: "json", defaultValue: {} }
|
1627
1616
|
}
|
1628
1617
|
};
|
@@ -1709,11 +1698,11 @@ var getFieldStatement = (models, model, field) => {
|
|
1709
1698
|
const modelList = models.some((item) => item.slug === model.slug) ? models : [...models, model];
|
1710
1699
|
const targetTable = getModelBySlug(modelList, field.target).table;
|
1711
1700
|
statement += ` REFERENCES ${targetTable}("id")`;
|
1712
|
-
for (const
|
1713
|
-
if (!Object.hasOwn(actions,
|
1714
|
-
const
|
1715
|
-
const action = actions[
|
1716
|
-
statement += ` ON ${
|
1701
|
+
for (const cause in actions) {
|
1702
|
+
if (!Object.hasOwn(actions, cause)) continue;
|
1703
|
+
const causeName = cause.toUpperCase().slice(2);
|
1704
|
+
const action = actions[cause];
|
1705
|
+
statement += ` ON ${causeName} ${action}`;
|
1717
1706
|
}
|
1718
1707
|
}
|
1719
1708
|
return statement;
|
@@ -1721,7 +1710,6 @@ var getFieldStatement = (models, model, field) => {
|
|
1721
1710
|
var PLURAL_MODEL_ENTITIES = {
|
1722
1711
|
field: "fields",
|
1723
1712
|
index: "indexes",
|
1724
|
-
trigger: "triggers",
|
1725
1713
|
preset: "presets"
|
1726
1714
|
};
|
1727
1715
|
var PLURAL_MODEL_ENTITIES_VALUES = Object.values(PLURAL_MODEL_ENTITIES);
|
@@ -1854,29 +1842,23 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
|
|
1854
1842
|
statement: `CREATE TABLE "${modelWithPresets.table}" (${columns.join(", ")})`,
|
1855
1843
|
params: []
|
1856
1844
|
});
|
1857
|
-
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
const query2 = {
|
1865
|
-
alter: {
|
1866
|
-
model: modelWithPresets.slug,
|
1867
|
-
create: {
|
1868
|
-
[modelEntity]: { slug: itemSlug, ...item }
|
1869
|
-
}
|
1845
|
+
const entityList = modelWithPresets.indexes;
|
1846
|
+
for (const [itemSlug, item] of Object.entries(entityList || {})) {
|
1847
|
+
const query2 = {
|
1848
|
+
alter: {
|
1849
|
+
model: modelWithPresets.slug,
|
1850
|
+
create: {
|
1851
|
+
index: { slug: itemSlug, ...item }
|
1870
1852
|
}
|
1871
|
-
}
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
}
|
1853
|
+
}
|
1854
|
+
};
|
1855
|
+
const tempModels = [
|
1856
|
+
...models.filter((model2) => model2.slug !== modelWithPresets.slug),
|
1857
|
+
{ ...modelWithPresets, indexes: {} }
|
1858
|
+
];
|
1859
|
+
transformMetaQuery(tempModels, dependencyStatements, null, query2, {
|
1860
|
+
inlineDefaults: options.inlineDefaults
|
1861
|
+
});
|
1880
1862
|
}
|
1881
1863
|
queryTypeDetails = { with: modelWithPresets };
|
1882
1864
|
getSystemModels(models, modelWithPresets).map((systemModel) => {
|
@@ -2034,56 +2016,6 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
|
|
2034
2016
|
}
|
2035
2017
|
dependencyStatements.push({ statement, params: [] });
|
2036
2018
|
}
|
2037
|
-
if (entity === "trigger") {
|
2038
|
-
const triggerName = convertToSnakeCase(slug);
|
2039
|
-
let statement = `${statementAction} TRIGGER "${triggerName}"`;
|
2040
|
-
if (action === "create") {
|
2041
|
-
const trigger = jsonValue;
|
2042
|
-
const statementParts = [`${trigger.when} ${trigger.action}`];
|
2043
|
-
if (trigger.fields) {
|
2044
|
-
if (trigger.action !== "UPDATE") {
|
2045
|
-
throw new RoninError({
|
2046
|
-
message: `When ${actionReadable} ${PLURAL_MODEL_ENTITIES[entity]}, targeting specific fields requires the \`UPDATE\` action.`,
|
2047
|
-
code: "INVALID_MODEL_VALUE",
|
2048
|
-
fields: ["action"]
|
2049
|
-
});
|
2050
|
-
}
|
2051
|
-
const fieldSelectors = trigger.fields.map((field2) => {
|
2052
|
-
return getFieldFromModel(existingModel, field2.slug, {
|
2053
|
-
modelEntityType: "trigger",
|
2054
|
-
modelEntityName: triggerName
|
2055
|
-
}).fieldSelector;
|
2056
|
-
});
|
2057
|
-
statementParts.push(`OF (${fieldSelectors.join(", ")})`);
|
2058
|
-
}
|
2059
|
-
statementParts.push("ON", `"${existingModel.table}"`);
|
2060
|
-
if (trigger.filter || trigger.effects.some((query2) => findInObject(query2, QUERY_SYMBOLS.FIELD))) {
|
2061
|
-
statementParts.push("FOR EACH ROW");
|
2062
|
-
}
|
2063
|
-
if (trigger.filter) {
|
2064
|
-
const tableAlias = trigger.action === "DELETE" ? QUERY_SYMBOLS.FIELD_PARENT_OLD : QUERY_SYMBOLS.FIELD_PARENT_NEW;
|
2065
|
-
const withStatement = handleWith(
|
2066
|
-
models,
|
2067
|
-
{ ...existingModel, tableAlias },
|
2068
|
-
null,
|
2069
|
-
trigger.filter
|
2070
|
-
);
|
2071
|
-
statementParts.push("WHEN", `(${withStatement})`);
|
2072
|
-
}
|
2073
|
-
const effectStatements = trigger.effects.map((effectQuery) => {
|
2074
|
-
return compileQueryInput(effectQuery[QUERY_SYMBOLS.QUERY], models, null, {
|
2075
|
-
returning: false,
|
2076
|
-
parentModel: existingModel,
|
2077
|
-
inlineDefaults: options.inlineDefaults
|
2078
|
-
}).main.statement;
|
2079
|
-
});
|
2080
|
-
statementParts.push("BEGIN");
|
2081
|
-
statementParts.push(`${effectStatements.join("; ")};`);
|
2082
|
-
statementParts.push("END");
|
2083
|
-
statement += ` ${statementParts.join(" ")}`;
|
2084
|
-
}
|
2085
|
-
dependencyStatements.push({ statement, params: [] });
|
2086
|
-
}
|
2087
2019
|
const field = `${QUERY_SYMBOLS.FIELD}${pluralType}`;
|
2088
2020
|
let json;
|
2089
2021
|
switch (action) {
|