@ronin/compiler 0.18.1 → 0.18.2-triggers-experimental-440
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 +27 -93
- 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 {
|
@@ -1052,10 +1047,12 @@ var handleUsing = (model, instructions) => {
|
|
1052
1047
|
if (currentValue) {
|
1053
1048
|
let newValue;
|
1054
1049
|
if (Array.isArray(currentValue)) {
|
1055
|
-
newValue =
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1050
|
+
newValue = Array.from(
|
1051
|
+
/* @__PURE__ */ new Set([
|
1052
|
+
...replacedUsingFilter[instructionName],
|
1053
|
+
...currentValue
|
1054
|
+
])
|
1055
|
+
);
|
1059
1056
|
} else if (isObject(currentValue)) {
|
1060
1057
|
newValue = {
|
1061
1058
|
...replacedUsingFilter[instructionName],
|
@@ -1316,11 +1313,6 @@ var parseFieldExpression = (model, instructionName, expression, parentModel) =>
|
|
1316
1313
|
if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT)) {
|
1317
1314
|
rootModel = parentModel;
|
1318
1315
|
toReplace = QUERY_SYMBOLS.FIELD_PARENT;
|
1319
|
-
if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT_OLD)) {
|
1320
|
-
rootModel.tableAlias = toReplace = QUERY_SYMBOLS.FIELD_PARENT_OLD;
|
1321
|
-
} else if (match.startsWith(QUERY_SYMBOLS.FIELD_PARENT_NEW)) {
|
1322
|
-
rootModel.tableAlias = toReplace = QUERY_SYMBOLS.FIELD_PARENT_NEW;
|
1323
|
-
}
|
1324
1316
|
}
|
1325
1317
|
const fieldSlug = match.replace(toReplace, "");
|
1326
1318
|
const field = getFieldFromModel(rootModel, fieldSlug, { instructionName });
|
@@ -1620,7 +1612,6 @@ var ROOT_MODEL = {
|
|
1620
1612
|
// which makes the statement shorter.
|
1621
1613
|
fields: { type: "json", defaultValue: {} },
|
1622
1614
|
indexes: { type: "json", defaultValue: {} },
|
1623
|
-
triggers: { type: "json", defaultValue: {} },
|
1624
1615
|
presets: { type: "json", defaultValue: {} }
|
1625
1616
|
}
|
1626
1617
|
};
|
@@ -1707,11 +1698,11 @@ var getFieldStatement = (models, model, field) => {
|
|
1707
1698
|
const modelList = models.some((item) => item.slug === model.slug) ? models : [...models, model];
|
1708
1699
|
const targetTable = getModelBySlug(modelList, field.target).table;
|
1709
1700
|
statement += ` REFERENCES ${targetTable}("id")`;
|
1710
|
-
for (const
|
1711
|
-
if (!Object.hasOwn(actions,
|
1712
|
-
const
|
1713
|
-
const action = actions[
|
1714
|
-
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}`;
|
1715
1706
|
}
|
1716
1707
|
}
|
1717
1708
|
return statement;
|
@@ -1719,7 +1710,6 @@ var getFieldStatement = (models, model, field) => {
|
|
1719
1710
|
var PLURAL_MODEL_ENTITIES = {
|
1720
1711
|
field: "fields",
|
1721
1712
|
index: "indexes",
|
1722
|
-
trigger: "triggers",
|
1723
1713
|
preset: "presets"
|
1724
1714
|
};
|
1725
1715
|
var PLURAL_MODEL_ENTITIES_VALUES = Object.values(PLURAL_MODEL_ENTITIES);
|
@@ -1852,29 +1842,23 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
|
|
1852
1842
|
statement: `CREATE TABLE "${modelWithPresets.table}" (${columns.join(", ")})`,
|
1853
1843
|
params: []
|
1854
1844
|
});
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
const query2 = {
|
1863
|
-
alter: {
|
1864
|
-
model: modelWithPresets.slug,
|
1865
|
-
create: {
|
1866
|
-
[modelEntity]: { slug: itemSlug, ...item }
|
1867
|
-
}
|
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 }
|
1868
1852
|
}
|
1869
|
-
}
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
}
|
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
|
+
});
|
1878
1862
|
}
|
1879
1863
|
queryTypeDetails = { with: modelWithPresets };
|
1880
1864
|
getSystemModels(models, modelWithPresets).map((systemModel) => {
|
@@ -2032,56 +2016,6 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
|
|
2032
2016
|
}
|
2033
2017
|
dependencyStatements.push({ statement, params: [] });
|
2034
2018
|
}
|
2035
|
-
if (entity === "trigger") {
|
2036
|
-
const triggerName = convertToSnakeCase(slug);
|
2037
|
-
let statement = `${statementAction} TRIGGER "${triggerName}"`;
|
2038
|
-
if (action === "create") {
|
2039
|
-
const trigger = jsonValue;
|
2040
|
-
const statementParts = [`${trigger.when} ${trigger.action}`];
|
2041
|
-
if (trigger.fields) {
|
2042
|
-
if (trigger.action !== "UPDATE") {
|
2043
|
-
throw new RoninError({
|
2044
|
-
message: `When ${actionReadable} ${PLURAL_MODEL_ENTITIES[entity]}, targeting specific fields requires the \`UPDATE\` action.`,
|
2045
|
-
code: "INVALID_MODEL_VALUE",
|
2046
|
-
fields: ["action"]
|
2047
|
-
});
|
2048
|
-
}
|
2049
|
-
const fieldSelectors = trigger.fields.map((field2) => {
|
2050
|
-
return getFieldFromModel(existingModel, field2.slug, {
|
2051
|
-
modelEntityType: "trigger",
|
2052
|
-
modelEntityName: triggerName
|
2053
|
-
}).fieldSelector;
|
2054
|
-
});
|
2055
|
-
statementParts.push(`OF (${fieldSelectors.join(", ")})`);
|
2056
|
-
}
|
2057
|
-
statementParts.push("ON", `"${existingModel.table}"`);
|
2058
|
-
if (trigger.filter || trigger.effects.some((query2) => findInObject(query2, QUERY_SYMBOLS.FIELD))) {
|
2059
|
-
statementParts.push("FOR EACH ROW");
|
2060
|
-
}
|
2061
|
-
if (trigger.filter) {
|
2062
|
-
const tableAlias = trigger.action === "DELETE" ? QUERY_SYMBOLS.FIELD_PARENT_OLD : QUERY_SYMBOLS.FIELD_PARENT_NEW;
|
2063
|
-
const withStatement = handleWith(
|
2064
|
-
models,
|
2065
|
-
{ ...existingModel, tableAlias },
|
2066
|
-
null,
|
2067
|
-
trigger.filter
|
2068
|
-
);
|
2069
|
-
statementParts.push("WHEN", `(${withStatement})`);
|
2070
|
-
}
|
2071
|
-
const effectStatements = trigger.effects.map((effectQuery) => {
|
2072
|
-
return compileQueryInput(effectQuery[QUERY_SYMBOLS.QUERY], models, null, {
|
2073
|
-
returning: false,
|
2074
|
-
parentModel: existingModel,
|
2075
|
-
inlineDefaults: options.inlineDefaults
|
2076
|
-
}).main.statement;
|
2077
|
-
});
|
2078
|
-
statementParts.push("BEGIN");
|
2079
|
-
statementParts.push(`${effectStatements.join("; ")};`);
|
2080
|
-
statementParts.push("END");
|
2081
|
-
statement += ` ${statementParts.join(" ")}`;
|
2082
|
-
}
|
2083
|
-
dependencyStatements.push({ statement, params: [] });
|
2084
|
-
}
|
2085
2019
|
const field = `${QUERY_SYMBOLS.FIELD}${pluralType}`;
|
2086
2020
|
let json;
|
2087
2021
|
switch (action) {
|