|
30 | 30 | * `requiredWhen` / `readonlyWhen`. The recompute's own write is an update of |
31 | 31 | * that row, judged by those rules: one by one they judge every intermediate |
32 | 32 | * value (an atomic move A→B then B→A over a cap refuses the first), so |
33 | | - * deferred they would judge only the last. A declared bound (`min`, `max`, |
34 | | - * `minLength`, `maxLength`, `pattern`, `unique`, `required`) on a value that |
35 | | - * write stores, the summary itself or a stored computed field, is judged the |
36 | | - * same way and counts as a rule reading that value. |
| 33 | + * deferred they would judge only the last. That write also judges every |
| 34 | + * value it stores against its declaration, so the same holds without a |
| 35 | + * rule: a declared bound on the summary itself (`min`, `max`, `minLength`, |
| 36 | + * `maxLength`, `pattern`, `precision`, `unique`, `required`, or an |
| 37 | + * object-level unique index naming it) counts as reading it, and EVERY |
| 38 | + * stored computed field of its object counts as read, bound or not, since |
| 39 | + * the write computes it and judges it whatever it declares (a bound, an |
| 40 | + * option list, a value domain, or a number that is not finite). |
37 | 41 | * |
38 | 42 | * A stored computed field (an `expression` on a field that is not a |
39 | 43 | * `formula`) that reads another row one hop is stored from what it read at its |
@@ -64,11 +68,26 @@ const PARENT_ROOT = 'parent'; |
64 | 68 | const PARENT_PREDICATE_SLOTS = ['requiredWhen', 'readonlyWhen'] as const; |
65 | 69 |
|
66 | 70 | /** |
67 | | - * The declared bounds a write judges on a stored value (`validateRecord`'s |
68 | | - * `min_value` / `max_value` / `min_length` / `max_length` / pattern and |
69 | | - * `required` checks, and a unique index). |
| 71 | + * The declared bounds a write judges on the summary's stored value |
| 72 | + * (`validateRecord`'s `min_value` / `max_value` / `min_length` / `max_length` |
| 73 | + * / `max_precision` and `required` checks, and a field-level unique index). |
| 74 | + * `pattern` / `regex` are read by no check today: an over-read, which costs |
| 75 | + * only statements. A declared index with `unique` is read beside this list. |
70 | 76 | */ |
71 | | -const WRITE_BOUND_KEYS = ['min', 'max', 'minLength', 'maxLength', 'pattern', 'regex', 'unique', 'required'] as const; |
| 77 | +const SUMMARY_BOUND_KEYS = ['min', 'max', 'minLength', 'maxLength', 'pattern', 'regex', 'precision', 'unique', 'required'] as const; |
| 78 | + |
| 79 | +/** The fields an object-level declared index with `unique` names (`'global'`, `'organization'`, or a stored bare `true`). */ |
| 80 | +function uniquelyIndexed(indexes: unknown): Set<string> { |
| 81 | + const out = new Set<string>(); |
| 82 | + if (!Array.isArray(indexes)) return out; |
| 83 | + for (const index of indexes) { |
| 84 | + if (!index || typeof index !== 'object') continue; |
| 85 | + const { unique, fields } = index as { unique?: unknown; fields?: unknown }; |
| 86 | + if (unique === undefined || unique === null || unique === false || !Array.isArray(fields)) continue; |
| 87 | + for (const field of fields) if (typeof field === 'string') out.add(field); |
| 88 | + } |
| 89 | + return out; |
| 90 | +} |
72 | 91 |
|
73 | 92 | /** The CEL roots a rule reads its own row under: the row, and the row before the write. */ |
74 | 93 | const OWN_ROW_ROOTS = ['record', 'previous'] as const; |
@@ -115,7 +134,7 @@ export function ruleReadKey(object: string, field: string): string { |
115 | 134 | } |
116 | 135 |
|
117 | 136 | /** An object definition as this module reads it. */ |
118 | | -type ObjectDef = { name?: unknown; fields?: Record<string, Record<string, unknown> | null | undefined>; validations?: unknown[] } | null | undefined; |
| 137 | +type ObjectDef = { name?: unknown; fields?: Record<string, Record<string, unknown> | null | undefined>; validations?: unknown[]; indexes?: unknown } | null | undefined; |
119 | 138 |
|
120 | 139 | /** The parts of a roll-up descriptor the chain closure reads. */ |
121 | 140 | export interface ChainLink { |
@@ -189,15 +208,23 @@ export function summaryFieldsReadByRules(objects: Iterable<unknown>, links: Iter |
189 | 208 | for (const root of OWN_ROW_ROOTS) for (const field of fieldsReadOn(source, root)) own.add(field); |
190 | 209 | } |
191 | 210 | } |
192 | | - // (iii) A declared bound on a field the recompute's own write stores: the |
193 | | - // summary itself, or a stored computed field (an `expression` on a field |
194 | | - // that is not a `formula`). The write computes it onto the payload and |
195 | | - // judges it (`validateRecord`, a unique index), so one by one every |
196 | | - // intermediate value is judged; the closure below adds what it reads. |
| 211 | + // (iii) What the recompute's own write judges without a rule. It stores |
| 212 | + // the summary and every stored computed field (an `expression` on a field |
| 213 | + // that is not a `formula`), computed onto its payload, and judges each |
| 214 | + // against its declaration (`validateRecord`, a unique index), so one by |
| 215 | + // one every intermediate value is judged. The summary counts when it |
| 216 | + // declares a bound or a unique index names it; a stored computed field |
| 217 | + // counts always, since its type alone can refuse a value (an option, a |
| 218 | + // value domain, a number that is not finite). The closure below adds |
| 219 | + // what a stored computed field reads. |
| 220 | + const indexed = uniquelyIndexed(schema?.indexes); |
197 | 221 | for (const [field, def] of Object.entries(fields)) { |
198 | 222 | if (!def || typeof def !== 'object') continue; |
199 | | - const stored = def.type === 'summary' || (def.type !== 'formula' && celSource(def.expression) !== undefined); |
200 | | - if (stored && WRITE_BOUND_KEYS.some((key) => def[key] !== undefined && def[key] !== null && def[key] !== false)) own.add(field); |
| 223 | + if (def.type === 'summary') { |
| 224 | + if (indexed.has(field) || SUMMARY_BOUND_KEYS.some((key) => def[key] !== undefined && def[key] !== null && def[key] !== false)) own.add(field); |
| 225 | + } else if (def.type !== 'formula' && celSource(def.expression) !== undefined) { |
| 226 | + own.add(field); |
| 227 | + } |
201 | 228 | } |
202 | 229 | if (name) for (const field of own) out.add(ruleReadKey(name, field)); |
203 | 230 | // A stored computed field reading another row one hop (`record.inv.line_total`) |
|
0 commit comments