shutro 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/audit/index.d.ts +57 -0
- package/dist/audit/index.js +94 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +11 -0
- package/dist/cli/cli.d.ts +10 -0
- package/dist/cli/cli.js +149 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +47 -0
- package/dist/cli/errors.d.ts +16 -0
- package/dist/cli/errors.js +23 -0
- package/dist/cli/generate/generate.d.ts +33 -0
- package/dist/cli/generate/generate.js +82 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +200 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +65 -0
- package/dist/cli/inspect.d.ts +10 -0
- package/dist/cli/inspect.js +82 -0
- package/dist/cli/packages.d.ts +6 -0
- package/dist/cli/packages.js +6 -0
- package/dist/cli/project.d.ts +18 -0
- package/dist/cli/project.js +39 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/env/env.d.ts +295 -0
- package/dist/core/env/env.js +631 -0
- package/dist/core/env/identity.d.ts +33 -0
- package/dist/core/env/identity.js +49 -0
- package/dist/core/env/inspector.d.ts +55 -0
- package/dist/core/env/inspector.js +85 -0
- package/dist/core/env/time.d.ts +12 -0
- package/dist/core/env/time.js +38 -0
- package/dist/core/errors.d.ts +242 -0
- package/dist/core/errors.js +339 -0
- package/dist/core/index.d.ts +43 -0
- package/dist/core/index.js +34 -0
- package/dist/core/model/company.d.ts +6 -0
- package/dist/core/model/company.js +31 -0
- package/dist/core/model/faces.d.ts +42 -0
- package/dist/core/model/faces.js +1 -0
- package/dist/core/model/field.d.ts +446 -0
- package/dist/core/model/field.js +398 -0
- package/dist/core/model/generated.d.ts +75 -0
- package/dist/core/model/generated.js +1 -0
- package/dist/core/model/id.d.ts +33 -0
- package/dist/core/model/id.js +4 -0
- package/dist/core/model/model.d.ts +347 -0
- package/dist/core/model/model.js +538 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/money/currency.d.ts +39 -0
- package/dist/core/money/currency.js +37 -0
- package/dist/core/money/decimal.d.ts +26 -0
- package/dist/core/money/decimal.js +51 -0
- package/dist/core/money/fx.d.ts +60 -0
- package/dist/core/money/fx.js +109 -0
- package/dist/core/money/money.d.ts +84 -0
- package/dist/core/money/money.js +173 -0
- package/dist/core/money/quantity.d.ts +67 -0
- package/dist/core/money/quantity.js +108 -0
- package/dist/core/money/rounding.d.ts +11 -0
- package/dist/core/money/rounding.js +12 -0
- package/dist/core/recordset/audit.d.ts +25 -0
- package/dist/core/recordset/audit.js +205 -0
- package/dist/core/recordset/compute.d.ts +34 -0
- package/dist/core/recordset/compute.js +405 -0
- package/dist/core/recordset/constraints.d.ts +9 -0
- package/dist/core/recordset/constraints.js +61 -0
- package/dist/core/recordset/domain.d.ts +27 -0
- package/dist/core/recordset/domain.js +228 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +207 -0
- package/dist/core/recordset/recordset.d.ts +101 -0
- package/dist/core/recordset/recordset.js +723 -0
- package/dist/core/recordset/relations.d.ts +15 -0
- package/dist/core/recordset/relations.js +13 -0
- package/dist/core/recordset/rls.d.ts +15 -0
- package/dist/core/recordset/rls.js +108 -0
- package/dist/core/recordset/security.d.ts +42 -0
- package/dist/core/recordset/security.js +184 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/sqlErrors.d.ts +6 -0
- package/dist/core/recordset/sqlErrors.js +39 -0
- package/dist/core/recordset/transitions.d.ts +10 -0
- package/dist/core/recordset/transitions.js +77 -0
- package/dist/core/recordset/types.d.ts +296 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +27 -0
- package/dist/core/recordset/values.js +147 -0
- package/dist/core/recordset/writes.d.ts +37 -0
- package/dist/core/recordset/writes.js +581 -0
- package/dist/core/registry/ddl.d.ts +22 -0
- package/dist/core/registry/ddl.js +127 -0
- package/dist/core/registry/extension.d.ts +94 -0
- package/dist/core/registry/extension.js +90 -0
- package/dist/core/registry/module.d.ts +80 -0
- package/dist/core/registry/module.js +96 -0
- package/dist/core/registry/registry.d.ts +105 -0
- package/dist/core/registry/registry.js +496 -0
- package/dist/driver-pg/driver.d.ts +86 -0
- package/dist/driver-pg/driver.js +176 -0
- package/dist/driver-pg/errors.d.ts +72 -0
- package/dist/driver-pg/errors.js +182 -0
- package/dist/driver-pg/index.d.ts +7 -0
- package/dist/driver-pg/index.js +7 -0
- package/dist/i18n/index.d.ts +25 -0
- package/dist/i18n/index.js +51 -0
- package/dist/migrate/apply.d.ts +78 -0
- package/dist/migrate/apply.js +167 -0
- package/dist/migrate/catalog.d.ts +10 -0
- package/dist/migrate/catalog.js +122 -0
- package/dist/migrate/diff.d.ts +98 -0
- package/dist/migrate/diff.js +160 -0
- package/dist/migrate/errors.d.ts +21 -0
- package/dist/migrate/errors.js +31 -0
- package/dist/migrate/index.d.ts +18 -0
- package/dist/migrate/index.js +18 -0
- package/dist/migrate/render.d.ts +5 -0
- package/dist/migrate/render.js +101 -0
- package/dist/migrate/schema.d.ts +61 -0
- package/dist/migrate/schema.js +195 -0
- package/dist/security/index.d.ts +123 -0
- package/dist/security/index.js +262 -0
- package/dist/sequence/index.d.ts +63 -0
- package/dist/sequence/index.js +106 -0
- package/dist/sql/ast.d.ts +49 -0
- package/dist/sql/ast.js +47 -0
- package/dist/sql/builder/builder.d.ts +215 -0
- package/dist/sql/builder/builder.js +308 -0
- package/dist/sql/builder/expression.d.ts +80 -0
- package/dist/sql/builder/expression.js +126 -0
- package/dist/sql/compile.d.ts +18 -0
- package/dist/sql/compile.js +41 -0
- package/dist/sql/errors.d.ts +13 -0
- package/dist/sql/errors.js +16 -0
- package/dist/sql/index.d.ts +19 -0
- package/dist/sql/index.js +19 -0
- package/dist/sql/tag.d.ts +42 -0
- package/dist/sql/tag.js +95 -0
- package/package.json +82 -4
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compute engine at runtime (RFC 0008): trigger invalidation through the cache, resolution of triggers into the
|
|
3
|
+
* affected records (database + cache), batched recompute in rank order before flushes, computation of non-stored
|
|
4
|
+
* fields on load, and the assignment protocol inside computes.
|
|
5
|
+
*/
|
|
6
|
+
import { compile, sql } from '../../sql/index.js';
|
|
7
|
+
import { ComputeAssignmentError, RecomputeLimitError, ValidationError } from '../errors.js';
|
|
8
|
+
import { defaultDisplayName } from '../model/model.js';
|
|
9
|
+
import { isTracked, trackChange } from './audit.js';
|
|
10
|
+
import { junctionTable } from './relations.js';
|
|
11
|
+
import { normalize } from './values.js';
|
|
12
|
+
const MAX_ROUNDS = 100;
|
|
13
|
+
function addAll(sets, model, key, ids) {
|
|
14
|
+
let byKey = sets.get(model);
|
|
15
|
+
if (byKey === undefined) {
|
|
16
|
+
byKey = new Map();
|
|
17
|
+
sets.set(model, byKey);
|
|
18
|
+
}
|
|
19
|
+
let set = byKey.get(key);
|
|
20
|
+
if (set === undefined) {
|
|
21
|
+
set = new Set();
|
|
22
|
+
byKey.set(key, set);
|
|
23
|
+
}
|
|
24
|
+
for (const id of ids)
|
|
25
|
+
set.add(id);
|
|
26
|
+
}
|
|
27
|
+
function name(field) {
|
|
28
|
+
return `${field.model}.${field.key}`;
|
|
29
|
+
}
|
|
30
|
+
function isProtected(internals, field, id) {
|
|
31
|
+
return internals.compute.protected.get(name(field))?.has(id) ?? false;
|
|
32
|
+
}
|
|
33
|
+
// --------------------------------------------------------------------------------------- reverse resolution
|
|
34
|
+
/** Walks one step backwards: from ids of the step's target model to ids of `step.model`. */
|
|
35
|
+
async function stepBack(internals, step, ids, viaDb) {
|
|
36
|
+
const out = new Set();
|
|
37
|
+
if (ids.size === 0)
|
|
38
|
+
return out;
|
|
39
|
+
const model = internals.registry.get(step.model).model;
|
|
40
|
+
const cached = internals.cache.records.get(step.model);
|
|
41
|
+
if (step.kind === 'many2one') {
|
|
42
|
+
if (cached !== undefined) {
|
|
43
|
+
for (const [id, values] of cached) {
|
|
44
|
+
const v = values.get(step.key);
|
|
45
|
+
if (typeof v === 'number' && ids.has(v))
|
|
46
|
+
out.add(id);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (viaDb) {
|
|
50
|
+
const column = model.columns[step.key] ?? '';
|
|
51
|
+
const rows = await internals.query(compile(sql `SELECT ${sql.id('id')} FROM ${sql.id(model.table)} WHERE ${sql.id(column)} = ANY(${sql.array([...ids], 'bigint')})`));
|
|
52
|
+
for (const r of rows)
|
|
53
|
+
out.add(Number(r.id));
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (step.kind === 'one2many') {
|
|
58
|
+
const target = internals.registry.get(step.target ?? '').model;
|
|
59
|
+
const inverse = step.inverse ?? '';
|
|
60
|
+
const missing = [];
|
|
61
|
+
for (const id of ids) {
|
|
62
|
+
const values = internals.cache.records.get(target.name)?.get(id);
|
|
63
|
+
if (values?.has(inverse)) {
|
|
64
|
+
const parent = values.get(inverse);
|
|
65
|
+
if (typeof parent === 'number')
|
|
66
|
+
out.add(parent);
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
missing.push(id);
|
|
70
|
+
}
|
|
71
|
+
if (viaDb && missing.length > 0) {
|
|
72
|
+
// Rows that no longer exist are simply absent: their parents were triggered by the delete itself.
|
|
73
|
+
const column = target.columns[inverse] ?? '';
|
|
74
|
+
const rows = await internals.query(compile(sql `SELECT ${sql.id(column)} AS ${sql.id('parent')} FROM ${sql.id(target.table)} WHERE ${sql.id('id')} = ANY(${sql.array(missing, 'bigint')})`));
|
|
75
|
+
for (const r of rows)
|
|
76
|
+
if (r.parent !== null)
|
|
77
|
+
out.add(Number(r.parent));
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
// many2many
|
|
82
|
+
if (cached !== undefined) {
|
|
83
|
+
for (const [id, values] of cached) {
|
|
84
|
+
const list = values.get(step.key);
|
|
85
|
+
if (Array.isArray(list) && list.some((c) => ids.has(Number(c))))
|
|
86
|
+
out.add(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (viaDb) {
|
|
90
|
+
const target = internals.registry.get(step.target ?? '').model;
|
|
91
|
+
const j = junctionTable(model, model.fields[step.key], target);
|
|
92
|
+
const rows = await internals.query(compile(sql `SELECT ${sql.id(j.sourceColumn)} AS ${sql.id('src')} FROM ${sql.id(j.table)} WHERE ${sql.id(j.targetColumn)} = ANY(${sql.array([...ids], 'bigint')})`));
|
|
93
|
+
for (const r of rows)
|
|
94
|
+
out.add(Number(r.src));
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
/** The records of the trigger's computed field reached from changed `ids`. */
|
|
99
|
+
async function resolve(internals, trigger, ids, viaDb) {
|
|
100
|
+
let current = new Set(ids);
|
|
101
|
+
for (let i = trigger.reverse.length - 1; i >= 0; i--) {
|
|
102
|
+
const step = trigger.reverse[i];
|
|
103
|
+
current = await stepBack(internals, step, current, viaDb);
|
|
104
|
+
if (current.size === 0)
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
return current;
|
|
108
|
+
}
|
|
109
|
+
// -------------------------------------------------------------------------------------------- invalidation
|
|
110
|
+
/** Drops the cached values of the dependents of `(model, key)` that are in the cache (RFC 0008 §7.2). Cache only. */
|
|
111
|
+
export function invalidateCached(internals, model, key, ids) {
|
|
112
|
+
const triggers = internals.registry.compute.triggers.get(model)?.get(key);
|
|
113
|
+
if (triggers === undefined || ids.length === 0)
|
|
114
|
+
return;
|
|
115
|
+
for (const trigger of triggers) {
|
|
116
|
+
// Cache-only resolution is synchronous: no step queries the database.
|
|
117
|
+
let current = new Set(ids);
|
|
118
|
+
for (let i = trigger.reverse.length - 1; i >= 0 && current.size > 0; i--) {
|
|
119
|
+
current = stepBackCached(internals, trigger.reverse[i], current);
|
|
120
|
+
}
|
|
121
|
+
const byId = internals.cache.records.get(trigger.field.model);
|
|
122
|
+
if (byId === undefined)
|
|
123
|
+
continue;
|
|
124
|
+
const invalidated = [];
|
|
125
|
+
const tracked = isTracked(internals.registry.get(trigger.field.model).model, trigger.field.key);
|
|
126
|
+
for (const id of current) {
|
|
127
|
+
if (isProtected(internals, trigger.field, id))
|
|
128
|
+
continue;
|
|
129
|
+
const values = byId.get(id);
|
|
130
|
+
if (values === undefined || !values.has(trigger.field.key))
|
|
131
|
+
continue;
|
|
132
|
+
if (tracked) {
|
|
133
|
+
// Keep the value for the audit row of the recompute (RFC 0013 §7.2).
|
|
134
|
+
const name = `${trigger.field.model}.${trigger.field.key}`;
|
|
135
|
+
const previous = internals.compute.previous.get(name) ?? new Map();
|
|
136
|
+
if (!previous.has(id))
|
|
137
|
+
previous.set(id, values.get(trigger.field.key));
|
|
138
|
+
internals.compute.previous.set(name, previous);
|
|
139
|
+
}
|
|
140
|
+
values.delete(trigger.field.key);
|
|
141
|
+
invalidated.push(id);
|
|
142
|
+
}
|
|
143
|
+
// Transitive: what depended on the invalidated value is stale too (the graph is acyclic).
|
|
144
|
+
if (invalidated.length > 0)
|
|
145
|
+
invalidateCached(internals, trigger.field.model, trigger.field.key, invalidated);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function stepBackCached(internals, step, ids) {
|
|
149
|
+
const out = new Set();
|
|
150
|
+
const cached = internals.cache.records.get(step.model);
|
|
151
|
+
if (step.kind === 'one2many') {
|
|
152
|
+
const byId = internals.cache.records.get(step.target ?? '');
|
|
153
|
+
for (const id of ids) {
|
|
154
|
+
const parent = byId?.get(id)?.get(step.inverse ?? '');
|
|
155
|
+
if (typeof parent === 'number')
|
|
156
|
+
out.add(parent);
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
if (cached === undefined)
|
|
161
|
+
return out;
|
|
162
|
+
for (const [id, values] of cached) {
|
|
163
|
+
const v = values.get(step.key);
|
|
164
|
+
if (step.kind === 'many2one'
|
|
165
|
+
? typeof v === 'number' && ids.has(v)
|
|
166
|
+
: Array.isArray(v) && v.some((c) => ids.has(Number(c))))
|
|
167
|
+
out.add(id);
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
function addSibling(model, key, fields) {
|
|
172
|
+
const field = model.fields[key];
|
|
173
|
+
if (field?.currencyField !== undefined)
|
|
174
|
+
fields.add(field.currencyField);
|
|
175
|
+
if (field?.uomField !== undefined)
|
|
176
|
+
fields.add(field.uomField);
|
|
177
|
+
}
|
|
178
|
+
/** The load spec covering the `depends` of `fields` (RFC 0004 §7.5), plus the sibling currency / unit fields. */
|
|
179
|
+
export function dependsSpec(internals, model, fields) {
|
|
180
|
+
const root = { fields: new Set(['id']), relations: new Map() };
|
|
181
|
+
for (const f of fields) {
|
|
182
|
+
addSibling(model, f.key, root.fields);
|
|
183
|
+
for (const steps of f.paths) {
|
|
184
|
+
let node = root;
|
|
185
|
+
let current = model;
|
|
186
|
+
steps.forEach((step, i) => {
|
|
187
|
+
const last = i === steps.length - 1;
|
|
188
|
+
if (step.kind === 'one2many' || step.kind === 'many2many') {
|
|
189
|
+
let sub = node.relations.get(step.key);
|
|
190
|
+
if (sub === undefined) {
|
|
191
|
+
sub = { fields: new Set(['id']), relations: new Map() };
|
|
192
|
+
node.relations.set(step.key, sub);
|
|
193
|
+
}
|
|
194
|
+
node = sub;
|
|
195
|
+
}
|
|
196
|
+
else if (step.kind === 'many2one') {
|
|
197
|
+
node.fields.add(step.key);
|
|
198
|
+
if (!last) {
|
|
199
|
+
let sub = node.relations.get(step.key);
|
|
200
|
+
if (sub === undefined) {
|
|
201
|
+
sub = { fields: new Set(['id']), relations: new Map() };
|
|
202
|
+
node.relations.set(step.key, sub);
|
|
203
|
+
}
|
|
204
|
+
node = sub;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
node.fields.add(step.key);
|
|
209
|
+
addSibling(current, step.key, node.fields);
|
|
210
|
+
}
|
|
211
|
+
if (step.target !== undefined)
|
|
212
|
+
current = internals.registry.get(step.target).model;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return root;
|
|
217
|
+
}
|
|
218
|
+
// --------------------------------------------------------------------------------------------- computation
|
|
219
|
+
/** Assigns a computed value from inside its compute (the record accessors' setter). */
|
|
220
|
+
export function assignComputed(host, model, id, key, value) {
|
|
221
|
+
const { internals } = host;
|
|
222
|
+
if (!internals.compute.computing.has(`${model.name}.${key}`))
|
|
223
|
+
throw new ComputeAssignmentError(model.name, key);
|
|
224
|
+
const field = model.fields[key];
|
|
225
|
+
const values = host.values(model.name, id, true);
|
|
226
|
+
const normalized = normalize(model, key, field, value, (k) => values.get(k), {
|
|
227
|
+
lang: internals.env.lang,
|
|
228
|
+
source: internals.sourceLang,
|
|
229
|
+
});
|
|
230
|
+
internals.journal({
|
|
231
|
+
kind: 'value',
|
|
232
|
+
model: model.name,
|
|
233
|
+
id,
|
|
234
|
+
key,
|
|
235
|
+
had: values.has(key),
|
|
236
|
+
prev: values.get(key),
|
|
237
|
+
wasDirty: internals.isDirty(model.name, id, key),
|
|
238
|
+
});
|
|
239
|
+
const previous = internals.compute.previous.get(`${model.name}.${key}`);
|
|
240
|
+
const prev = values.has(key) ? values.get(key) : previous?.get(id);
|
|
241
|
+
previous?.delete(id);
|
|
242
|
+
values.set(key, normalized);
|
|
243
|
+
if (field.store) {
|
|
244
|
+
internals.markDirty(model.name, id, key);
|
|
245
|
+
trackChange(internals, model, id, key, prev, normalized, (k) => values.get(k));
|
|
246
|
+
}
|
|
247
|
+
internals.trigger(model.name, key, [id]);
|
|
248
|
+
}
|
|
249
|
+
function groupByCompute(fields) {
|
|
250
|
+
const groups = new Map();
|
|
251
|
+
for (const f of fields) {
|
|
252
|
+
const list = groups.get(f.compute) ?? [];
|
|
253
|
+
list.push(f);
|
|
254
|
+
groups.set(f.compute, list);
|
|
255
|
+
}
|
|
256
|
+
return [...groups.values()];
|
|
257
|
+
}
|
|
258
|
+
/** Every field sharing the compute of `field` on its model (one call fills all of them). */
|
|
259
|
+
function sharing(internals, field) {
|
|
260
|
+
return internals.registry.compute.fields.filter((f) => f.model === field.model && f.compute === field.compute);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Computes `fields` of `model` for `ids`: loads their `depends` for the whole batch, runs each compute once, then
|
|
264
|
+
* checks required fields. Fields sharing a compute are computed together.
|
|
265
|
+
*/
|
|
266
|
+
export async function computeFields(host, model, ids, fields) {
|
|
267
|
+
if (ids.length === 0 || fields.length === 0)
|
|
268
|
+
return;
|
|
269
|
+
const { internals } = host;
|
|
270
|
+
for (const group of groupByCompute(fields)) {
|
|
271
|
+
const all = sharing(internals, group[0]);
|
|
272
|
+
await host.load(model, ids, dependsSpec(internals, model, all));
|
|
273
|
+
const names = all.map(name);
|
|
274
|
+
for (const n of names) {
|
|
275
|
+
internals.compute.computing.add(n);
|
|
276
|
+
const set = internals.compute.protected.get(n) ?? new Set();
|
|
277
|
+
for (const id of ids)
|
|
278
|
+
set.add(id);
|
|
279
|
+
internals.compute.protected.set(n, set);
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
const compute = group[0].compute;
|
|
283
|
+
if (compute === defaultDisplayName) {
|
|
284
|
+
for (const id of ids) {
|
|
285
|
+
const value = host.values(model.name, id)?.get(model.recName);
|
|
286
|
+
assignComputed(host, model, id, 'displayName', value === null || value === undefined ? '' : String(value));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
const records = host.recordset(model, ids);
|
|
291
|
+
const fn = typeof compute === 'string' ? model.methods[compute] : compute;
|
|
292
|
+
await fn.call(records, records);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
for (const n of names) {
|
|
297
|
+
internals.compute.computing.delete(n);
|
|
298
|
+
// Outside a recompute round there is no re-trigger to guard against: later changes must invalidate.
|
|
299
|
+
if (!internals.compute.recomputing)
|
|
300
|
+
internals.compute.protected.delete(n);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
for (const f of all) {
|
|
304
|
+
for (const id of ids) {
|
|
305
|
+
const values = host.values(model.name, id, true);
|
|
306
|
+
const field = model.fields[f.key];
|
|
307
|
+
if (!values.has(f.key)) {
|
|
308
|
+
if (field.required) {
|
|
309
|
+
throw new ValidationError(model.name, [f.key], `computed field '${f.key}' is required but its compute did not assign it for record ${id}.`, 'Assign every record of the batch in the compute function.');
|
|
310
|
+
}
|
|
311
|
+
values.set(f.key, null);
|
|
312
|
+
if (f.stored)
|
|
313
|
+
internals.markDirty(model.name, id, f.key);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// -------------------------------------------------------------------------------------------------- recompute
|
|
320
|
+
/** Recomputes every stored computed field with pending triggers, in rank order, until nothing is pending (RFC 0008 §7.3). */
|
|
321
|
+
export async function recompute(host) {
|
|
322
|
+
const { internals } = host;
|
|
323
|
+
const graph = internals.registry.compute;
|
|
324
|
+
if (internals.compute.recomputing || !internals.hasPending())
|
|
325
|
+
return;
|
|
326
|
+
internals.compute.recomputing = true;
|
|
327
|
+
const toCompute = new Map();
|
|
328
|
+
try {
|
|
329
|
+
for (let round = 0;; round++) {
|
|
330
|
+
if (round >= MAX_ROUNDS) {
|
|
331
|
+
const pending = [];
|
|
332
|
+
for (const [model, byKey] of toCompute)
|
|
333
|
+
for (const key of byKey.keys())
|
|
334
|
+
pending.push(`${model}.${key}`);
|
|
335
|
+
throw new RecomputeLimitError(round, pending);
|
|
336
|
+
}
|
|
337
|
+
// 1. Resolve raw triggers (database + cache) into affected records of stored fields.
|
|
338
|
+
const just = internals.compute.protected;
|
|
339
|
+
for (const [model, byKey] of internals.takeTriggers()) {
|
|
340
|
+
for (const [key, ids] of byKey) {
|
|
341
|
+
for (const trigger of graph.triggers.get(model)?.get(key) ?? []) {
|
|
342
|
+
if (!trigger.field.stored)
|
|
343
|
+
continue;
|
|
344
|
+
const affected = await resolve(internals, trigger, ids, true);
|
|
345
|
+
const computed = just.get(name(trigger.field));
|
|
346
|
+
if (computed !== undefined)
|
|
347
|
+
for (const id of computed)
|
|
348
|
+
affected.delete(id);
|
|
349
|
+
if (affected.size > 0)
|
|
350
|
+
addAll(toCompute, trigger.field.model, trigger.field.key, affected);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
just.clear();
|
|
355
|
+
for (const [model, byKey] of internals.takeToCompute())
|
|
356
|
+
for (const [key, ids] of byKey)
|
|
357
|
+
addAll(toCompute, model, key, ids);
|
|
358
|
+
// 2. Lowest rank first; fields sharing a compute go together.
|
|
359
|
+
let bestRank = Number.POSITIVE_INFINITY;
|
|
360
|
+
let best = [];
|
|
361
|
+
for (const [model, byKey] of toCompute) {
|
|
362
|
+
for (const key of byKey.keys()) {
|
|
363
|
+
const field = graph.field(model, key);
|
|
364
|
+
if (field === undefined)
|
|
365
|
+
continue;
|
|
366
|
+
// Fields sharing a compute run together, so the group waits for its highest-ranked field's inputs.
|
|
367
|
+
const rank = Math.max(...sharing(internals, field).map((f) => f.rank));
|
|
368
|
+
if (rank < bestRank) {
|
|
369
|
+
bestRank = rank;
|
|
370
|
+
best = [field];
|
|
371
|
+
}
|
|
372
|
+
else if (rank === bestRank)
|
|
373
|
+
best.push(field);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (best.length === 0)
|
|
377
|
+
break;
|
|
378
|
+
const byModel = new Map();
|
|
379
|
+
for (const f of best)
|
|
380
|
+
byModel.set(f.model, [...(byModel.get(f.model) ?? []), f]);
|
|
381
|
+
for (const [modelName, fields] of byModel) {
|
|
382
|
+
const model = internals.registry.get(modelName).model;
|
|
383
|
+
for (const group of groupByCompute(fields)) {
|
|
384
|
+
const all = sharing(internals, group[0]);
|
|
385
|
+
const ids = new Set();
|
|
386
|
+
for (const f of all) {
|
|
387
|
+
const set = toCompute.get(modelName)?.get(f.key);
|
|
388
|
+
if (set === undefined)
|
|
389
|
+
continue;
|
|
390
|
+
for (const id of set)
|
|
391
|
+
ids.add(id);
|
|
392
|
+
toCompute.get(modelName)?.delete(f.key);
|
|
393
|
+
}
|
|
394
|
+
await computeFields(host, model, [...ids], [group[0]]);
|
|
395
|
+
}
|
|
396
|
+
if (toCompute.get(modelName)?.size === 0)
|
|
397
|
+
toCompute.delete(modelName);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
finally {
|
|
402
|
+
internals.compute.recomputing = false;
|
|
403
|
+
internals.compute.protected.clear();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Business constraints at flush points (RFC 0009 §7.1): records marked by triggers are loaded with the constraint's
|
|
3
|
+
* `depends` and checked in one batch per constraint; failing records are a `ConstraintError`.
|
|
4
|
+
*/
|
|
5
|
+
import type { ComputeHost } from './compute.js';
|
|
6
|
+
/** Ids out of what a `check` returned: a recordset, an array of records or ids, or nothing. */
|
|
7
|
+
export declare function failingIds(result: unknown): number[];
|
|
8
|
+
/** Runs every pending constraint check. Skipped inside a recompute round (the outer flush runs it). */
|
|
9
|
+
export declare function checkConstraints(host: ComputeHost): Promise<void>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Business constraints at flush points (RFC 0009 §7.1): records marked by triggers are loaded with the constraint's
|
|
3
|
+
* `depends` and checked in one batch per constraint; failing records are a `ConstraintError`.
|
|
4
|
+
*/
|
|
5
|
+
import { ConstraintError } from '../errors.js';
|
|
6
|
+
/** Ids out of what a `check` returned: a recordset, an array of records or ids, or nothing. */
|
|
7
|
+
export function failingIds(result) {
|
|
8
|
+
if (result === undefined || result === null || result === false)
|
|
9
|
+
return [];
|
|
10
|
+
const ids = result.ids;
|
|
11
|
+
if (Array.isArray(ids))
|
|
12
|
+
return ids.map(Number);
|
|
13
|
+
if (Array.isArray(result)) {
|
|
14
|
+
return result.flatMap((r) => {
|
|
15
|
+
if (typeof r === 'number')
|
|
16
|
+
return [r];
|
|
17
|
+
const inner = r.ids;
|
|
18
|
+
return Array.isArray(inner) ? inner.map(Number) : [];
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
function loadSpec(model, constraint) {
|
|
24
|
+
const fields = new Set(['id']);
|
|
25
|
+
for (const key of constraint.depends) {
|
|
26
|
+
const field = model.fields[key];
|
|
27
|
+
if (field === undefined || field.kind === 'one2many' || field.kind === 'many2many')
|
|
28
|
+
continue;
|
|
29
|
+
fields.add(key);
|
|
30
|
+
if (field.currencyField !== undefined)
|
|
31
|
+
fields.add(field.currencyField);
|
|
32
|
+
if (field.uomField !== undefined)
|
|
33
|
+
fields.add(field.uomField);
|
|
34
|
+
}
|
|
35
|
+
return { fields, relations: new Map() };
|
|
36
|
+
}
|
|
37
|
+
/** Runs every pending constraint check. Skipped inside a recompute round (the outer flush runs it). */
|
|
38
|
+
export async function checkConstraints(host) {
|
|
39
|
+
const { internals } = host;
|
|
40
|
+
if (internals.compute.recomputing || !internals.hasChecks())
|
|
41
|
+
return;
|
|
42
|
+
for (const [modelName, byKey] of internals.takeToCheck()) {
|
|
43
|
+
const model = internals.registry.get(modelName).model;
|
|
44
|
+
for (const [key, idSet] of byKey) {
|
|
45
|
+
const constraint = model.constraints.find((c) => c.key === key);
|
|
46
|
+
const ids = [...idSet].filter((id) => id > 0); // drafts are never checked
|
|
47
|
+
if (constraint === undefined || ids.length === 0)
|
|
48
|
+
continue;
|
|
49
|
+
await host.load(model, ids, loadSpec(model, constraint));
|
|
50
|
+
const records = host.recordset(model, ids);
|
|
51
|
+
const check = constraint.check;
|
|
52
|
+
const failing = failingIds(await check.call(records, records));
|
|
53
|
+
if (failing.length === 0)
|
|
54
|
+
continue;
|
|
55
|
+
const message = typeof constraint.message === 'string'
|
|
56
|
+
? constraint.message
|
|
57
|
+
: constraint.message(host.recordset(model, failing));
|
|
58
|
+
throw new ConstraintError(model.name, key, constraint.depends, failing, message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles a search domain (RFC 0005 §7.1) plus automatic scoping (`active`, companies) into WHERE fragments:
|
|
3
|
+
* field conditions ANDed, `or` / `and` / `not`, dotted relation paths (`'partner.name'`, `'order.user'`) and
|
|
4
|
+
* `{ lines: { any | none: domain } }` on x2many fields. Relations compile to sub-selects, never joins, so rows are
|
|
5
|
+
* never duplicated (RFC 0005 R8).
|
|
6
|
+
*/
|
|
7
|
+
import { type SqlFragment } from '../../sql/index.js';
|
|
8
|
+
import type { Env } from '../env/env.js';
|
|
9
|
+
import type { AnyModel } from '../model/model.js';
|
|
10
|
+
/** Resolves a model by name (the registry), needed for relation paths. */
|
|
11
|
+
export type ModelResolver = (name: string) => AnyModel;
|
|
12
|
+
/**
|
|
13
|
+
* Extra conditions on a path's target, ANDed inside its sub-select: user searches pass the target's read rules so a
|
|
14
|
+
* path cannot reveal records the user cannot read. Rules themselves compile without one.
|
|
15
|
+
*/
|
|
16
|
+
export type TargetScope = (target: AnyModel) => SqlFragment[];
|
|
17
|
+
/** The environment language and the source language (RFC 0011). */
|
|
18
|
+
export interface TranslationScope {
|
|
19
|
+
readonly lang: string;
|
|
20
|
+
readonly source: string;
|
|
21
|
+
}
|
|
22
|
+
/** Compiles the user's domain into conditions (ANDed). */
|
|
23
|
+
export declare function compileDomain(model: AnyModel, where: Readonly<Record<string, unknown>> | undefined, translation?: TranslationScope, resolve?: ModelResolver, scope?: TargetScope): SqlFragment[];
|
|
24
|
+
/** Conditions the environment adds to every search (RFC 0005 §7.4): archived records and company scope. */
|
|
25
|
+
export declare function scopingConditions(model: AnyModel, env: Env): SqlFragment[];
|
|
26
|
+
/** `ORDER BY` from `'field'`, `'field asc'`, `'field desc'` entries, with an `id` tiebreaker. */
|
|
27
|
+
export declare function compileOrder(model: AnyModel, order: readonly string[] | undefined, translation?: TranslationScope): SqlFragment;
|