@sequenceholdings/orm 2.1.0 → 2.1.1
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/README.md +38 -1
- package/dist/authoring.cjs +1358 -45
- package/dist/authoring.cjs.map +1 -1
- package/dist/authoring.d.cts +223 -2
- package/dist/authoring.d.ts +223 -2
- package/dist/authoring.js +1349 -47
- package/dist/authoring.js.map +1 -1
- package/dist/canonical.cjs +662 -38
- package/dist/canonical.cjs.map +1 -1
- package/dist/canonical.d.cts +696 -53
- package/dist/canonical.d.ts +696 -53
- package/dist/canonical.js +647 -39
- package/dist/canonical.js.map +1 -1
- package/dist/cli.cjs +1808 -247
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +1809 -248
- package/dist/cli.js.map +1 -1
- package/dist/client.cjs +90 -25
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +39 -22
- package/dist/client.d.ts +39 -22
- package/dist/client.js +87 -26
- package/dist/client.js.map +1 -1
- package/dist/compiler.cjs +1325 -285
- package/dist/compiler.cjs.map +1 -1
- package/dist/compiler.d.cts +23 -2
- package/dist/compiler.d.ts +23 -2
- package/dist/compiler.js +1325 -285
- package/dist/compiler.js.map +1 -1
- package/dist/constants-YoFGjIBM.d.cts +99 -0
- package/dist/constants-YoFGjIBM.d.ts +99 -0
- package/dist/dynamic-input.d.cts +1 -0
- package/dist/dynamic-input.d.ts +1 -0
- package/dist/index.cjs +1605 -140
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +230 -113
- package/dist/index.d.ts +230 -113
- package/dist/index.js +1583 -142
- package/dist/index.js.map +1 -1
- package/dist/ir.cjs +727 -38
- package/dist/ir.cjs.map +1 -1
- package/dist/ir.d.cts +539 -21
- package/dist/ir.d.ts +539 -21
- package/dist/ir.js +722 -39
- package/dist/ir.js.map +1 -1
- package/dist/{naming-DCn94LJ4.d.ts → naming-CPfx7EfF.d.ts} +14 -1
- package/dist/{naming-DPglwzkB.d.cts → naming-CU9ThP_E.d.cts} +14 -1
- package/dist/policy-1iZbGnZZ.d.cts +24 -0
- package/dist/policy-1iZbGnZZ.d.ts +24 -0
- package/dist/predicate-v2.cjs +726 -42
- package/dist/predicate-v2.cjs.map +1 -1
- package/dist/predicate-v2.d.cts +2 -1
- package/dist/predicate-v2.d.ts +2 -1
- package/dist/predicate-v2.js +726 -42
- package/dist/predicate-v2.js.map +1 -1
- package/dist/sdl.cjs +858 -54
- package/dist/sdl.cjs.map +1 -1
- package/dist/sdl.d.cts +7 -3
- package/dist/sdl.d.ts +7 -3
- package/dist/sdl.js +856 -55
- package/dist/sdl.js.map +1 -1
- package/package.json +9 -4
- package/templates/orm-namespace/CLAUDE.md +6 -4
- package/templates/orm-namespace/codegen.preset.d.cts +7 -0
- package/templates/orm-namespace/codegen.ts +2 -1
- package/templates/orm-namespace/graphql/example/queries.ts +4 -3
- package/templates/orm-namespace/package.json +2 -1
- package/templates/orm-namespace/schema/tables.ts +3 -2
- package/templates/orm-namespace/sequence.config.ts +7 -11
package/dist/predicate-v2.cjs
CHANGED
|
@@ -32,10 +32,476 @@ var PolicyCompileError = class extends Error {
|
|
|
32
32
|
|
|
33
33
|
// src/constants.ts
|
|
34
34
|
var COLUMN_IDENTIFIER_PATTERN = /^_?[a-z][a-z0-9_]*$/;
|
|
35
|
-
var
|
|
35
|
+
var MAX_IDENTIFIER_LENGTH = 63;
|
|
36
|
+
var CAPABILITY_KEY_PATTERN = /^(?:org\/|app\/[a-z][a-z0-9-]{0,63}\/)?[a-z0-9-]{1,64}$/;
|
|
37
|
+
var CELL_PARTS = ["at", "by", "rev", "source", "conflicts"];
|
|
38
|
+
var CELL_PART_SUFFIX = {
|
|
39
|
+
at: "__at",
|
|
40
|
+
by: "__by",
|
|
41
|
+
rev: "__rev",
|
|
42
|
+
source: "__src",
|
|
43
|
+
conflicts: "__lost"
|
|
44
|
+
};
|
|
45
|
+
var MAX_TRACKED_COLUMN_NAME = MAX_IDENTIFIER_LENGTH - 6;
|
|
46
|
+
function cellColumnName(column, part) {
|
|
47
|
+
return `${column}${CELL_PART_SUFFIX[part]}`;
|
|
48
|
+
}
|
|
49
|
+
var MATERIALIZE_ACTOR = "orm:materialize";
|
|
50
|
+
JSON.stringify({ sub: MATERIALIZE_ACTOR, org: null, roles: [], capabilities: [] });
|
|
51
|
+
var quoteIdentifier = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
52
|
+
var quoteLiteral = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
53
|
+
|
|
54
|
+
// src/checks/compile.ts
|
|
55
|
+
function tokenize(source) {
|
|
56
|
+
const tokens = [];
|
|
57
|
+
for (let position = 0; position < source.length; ) {
|
|
58
|
+
const char = source[position];
|
|
59
|
+
if (/\s/.test(char)) {
|
|
60
|
+
position++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (char === "(" || char === ")" || char === ",") {
|
|
64
|
+
tokens.push({ kind: char === "(" ? "lparen" : char === ")" ? "rparen" : "comma", value: char, position });
|
|
65
|
+
position++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (char === "'") {
|
|
69
|
+
const start = position++;
|
|
70
|
+
let value = "";
|
|
71
|
+
let closed = false;
|
|
72
|
+
while (position < source.length) {
|
|
73
|
+
if (source[position] !== "'") {
|
|
74
|
+
value += source[position++];
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (source[position + 1] === "'") {
|
|
78
|
+
value += "'";
|
|
79
|
+
position += 2;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
position++;
|
|
83
|
+
closed = true;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
if (!closed) throw new Error(`unterminated string at position ${start}`);
|
|
87
|
+
if (/[\x00-\x1f\x7f]/.test(value)) throw new Error(`control character in string at position ${start}`);
|
|
88
|
+
tokens.push({ kind: "string", value, position: start });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const operator = /^(<=|>=|<>|!=|!~|=|<|>|~)/.exec(source.slice(position));
|
|
92
|
+
if (operator) {
|
|
93
|
+
tokens.push({ kind: "operator", value: operator[0] === "<>" ? "!=" : operator[0], position });
|
|
94
|
+
position += operator[0].length;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const number = /^-?\d+(?:\.\d+)?/.exec(source.slice(position));
|
|
98
|
+
if (number) {
|
|
99
|
+
tokens.push({ kind: "number", value: number[0], position });
|
|
100
|
+
position += number[0].length;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const identifier3 = /^[a-zA-Z_][a-zA-Z_0-9]*/.exec(source.slice(position));
|
|
104
|
+
if (identifier3) {
|
|
105
|
+
tokens.push({ kind: "identifier", value: identifier3[0], position });
|
|
106
|
+
position += identifier3[0].length;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
throw new Error(`unexpected character '${char}' at position ${position}`);
|
|
110
|
+
}
|
|
111
|
+
tokens.push({ kind: "end", value: "", position: source.length });
|
|
112
|
+
if (tokens.length > 256) throw new Error("check expression exceeds 256 tokens");
|
|
113
|
+
return tokens;
|
|
114
|
+
}
|
|
115
|
+
var Parser = class {
|
|
116
|
+
constructor(tokens, columns) {
|
|
117
|
+
this.tokens = tokens;
|
|
118
|
+
this.columns = /* @__PURE__ */ new Map();
|
|
119
|
+
for (const column of columns) {
|
|
120
|
+
for (const alias of [column.name, column.columnName]) {
|
|
121
|
+
const previous = this.columns.get(alias);
|
|
122
|
+
if (previous && previous.columnName !== column.columnName) {
|
|
123
|
+
throw new Error(`ambiguous check column '${alias}'`);
|
|
124
|
+
}
|
|
125
|
+
this.columns.set(alias, column);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
index = 0;
|
|
130
|
+
depth = 0;
|
|
131
|
+
columns;
|
|
132
|
+
parse() {
|
|
133
|
+
const expression = this.parseOr();
|
|
134
|
+
this.expect("end", "end of expression");
|
|
135
|
+
return expression;
|
|
136
|
+
}
|
|
137
|
+
parseOr() {
|
|
138
|
+
let left = this.parseAnd();
|
|
139
|
+
while (this.matchKeyword("or")) left = `(${left} OR ${this.parseAnd()})`;
|
|
140
|
+
return left;
|
|
141
|
+
}
|
|
142
|
+
parseAnd() {
|
|
143
|
+
let left = this.parseNot();
|
|
144
|
+
while (this.matchKeyword("and")) left = `(${left} AND ${this.parseNot()})`;
|
|
145
|
+
return left;
|
|
146
|
+
}
|
|
147
|
+
parseNot() {
|
|
148
|
+
if (this.matchKeyword("not")) return `(NOT ${this.parseNot()})`;
|
|
149
|
+
return this.parsePrimary();
|
|
150
|
+
}
|
|
151
|
+
parsePrimary() {
|
|
152
|
+
if (this.peek().kind === "lparen") {
|
|
153
|
+
if (++this.depth > 32) throw new Error("check expression nesting exceeds 32 levels");
|
|
154
|
+
this.advance();
|
|
155
|
+
const expression = this.parseOr();
|
|
156
|
+
this.expect("rparen", "')'");
|
|
157
|
+
this.depth--;
|
|
158
|
+
return `(${expression})`;
|
|
159
|
+
}
|
|
160
|
+
return this.parseComparison();
|
|
161
|
+
}
|
|
162
|
+
parseComparison() {
|
|
163
|
+
const left = this.parseValue();
|
|
164
|
+
if (this.matchKeyword("is")) {
|
|
165
|
+
const negate = this.matchKeyword("not");
|
|
166
|
+
this.expectKeyword("null");
|
|
167
|
+
return `${left.sql} IS ${negate ? "NOT " : ""}NULL`;
|
|
168
|
+
}
|
|
169
|
+
if (this.matchKeyword("between")) {
|
|
170
|
+
const lower = this.parseValue();
|
|
171
|
+
this.expectKeyword("and");
|
|
172
|
+
const upper = this.parseValue();
|
|
173
|
+
return `${left.sql} BETWEEN ${lower.sql} AND ${upper.sql}`;
|
|
174
|
+
}
|
|
175
|
+
if (this.matchKeyword("in")) {
|
|
176
|
+
this.expect("lparen", "'(' after IN");
|
|
177
|
+
const values = [this.parseValue()];
|
|
178
|
+
while (this.peek().kind === "comma") {
|
|
179
|
+
this.advance();
|
|
180
|
+
values.push(this.parseValue());
|
|
181
|
+
}
|
|
182
|
+
this.expect("rparen", "')' after IN values");
|
|
183
|
+
if (values.some((value) => !value.literal)) throw new Error("IN values must be literals");
|
|
184
|
+
return `${left.sql} IN (${values.map((value) => value.sql).join(", ")})`;
|
|
185
|
+
}
|
|
186
|
+
const operator = this.expect("operator", "a comparison operator");
|
|
187
|
+
const right = this.parseValue();
|
|
188
|
+
if (operator.value === "~" || operator.value === "!~") {
|
|
189
|
+
if (left.column && left.column.type !== "text") throw new Error("regex checks require a text column");
|
|
190
|
+
if (!right.literal) throw new Error("regex pattern must be a literal");
|
|
191
|
+
}
|
|
192
|
+
return `${left.sql} ${operator.value} ${right.sql}`;
|
|
193
|
+
}
|
|
194
|
+
parseValue() {
|
|
195
|
+
const token = this.advance();
|
|
196
|
+
if (token.kind === "string") return { sql: quoteLiteral(token.value), literal: true };
|
|
197
|
+
if (token.kind === "number") {
|
|
198
|
+
const number = Number(token.value);
|
|
199
|
+
if (!Number.isFinite(number) || Number.isInteger(number) && !Number.isSafeInteger(number)) {
|
|
200
|
+
throw new Error(`unsafe numeric literal at position ${token.position}`);
|
|
201
|
+
}
|
|
202
|
+
return { sql: token.value, literal: true };
|
|
203
|
+
}
|
|
204
|
+
if (token.kind !== "identifier") throw new Error(`expected a column or literal at position ${token.position}`);
|
|
205
|
+
const keyword = token.value.toLowerCase();
|
|
206
|
+
if (keyword === "true" || keyword === "false" || keyword === "null") return { sql: keyword.toUpperCase(), literal: true };
|
|
207
|
+
if (keyword === "btrim") {
|
|
208
|
+
this.expect("lparen", "'(' after btrim");
|
|
209
|
+
const column2 = this.parseColumn();
|
|
210
|
+
this.expect("rparen", "')' after btrim column");
|
|
211
|
+
if (column2.type !== "text") throw new Error("btrim requires a text column");
|
|
212
|
+
return { sql: `btrim(${quoteIdentifier(column2.columnName)})`, column: column2 };
|
|
213
|
+
}
|
|
214
|
+
const column = this.columns.get(token.value);
|
|
215
|
+
if (!column) throw new Error(`unknown check column '${token.value}'`);
|
|
216
|
+
return { sql: quoteIdentifier(column.columnName), column };
|
|
217
|
+
}
|
|
218
|
+
parseColumn() {
|
|
219
|
+
const token = this.expect("identifier", "a column name");
|
|
220
|
+
const column = this.columns.get(token.value);
|
|
221
|
+
if (!column) throw new Error(`unknown check column '${token.value}'`);
|
|
222
|
+
return column;
|
|
223
|
+
}
|
|
224
|
+
peek() {
|
|
225
|
+
return this.tokens[this.index];
|
|
226
|
+
}
|
|
227
|
+
advance() {
|
|
228
|
+
const token = this.peek();
|
|
229
|
+
if (token.kind !== "end") this.index++;
|
|
230
|
+
return token;
|
|
231
|
+
}
|
|
232
|
+
matchKeyword(keyword) {
|
|
233
|
+
if (this.peek().kind !== "identifier" || this.peek().value.toLowerCase() !== keyword) return false;
|
|
234
|
+
this.advance();
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
expectKeyword(keyword) {
|
|
238
|
+
if (!this.matchKeyword(keyword)) throw new Error(`expected '${keyword}' at position ${this.peek().position}`);
|
|
239
|
+
}
|
|
240
|
+
expect(kind, description) {
|
|
241
|
+
const token = this.peek();
|
|
242
|
+
if (token.kind !== kind) throw new Error(`expected ${description} at position ${token.position}`);
|
|
243
|
+
return this.advance();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
function compileCheckExpression(source, columns) {
|
|
247
|
+
return new Parser(tokenize(source), columns).parse();
|
|
248
|
+
}
|
|
249
|
+
var policyVariantSchema = zod.z.object({
|
|
250
|
+
/** Claims-only guard (capability()/role()/claim() comparisons — no columns). */
|
|
251
|
+
when: zod.z.string().min(1).max(1e3),
|
|
252
|
+
/** Exactly one of: allow-all bypass, or a row predicate. */
|
|
253
|
+
allow: zod.z.literal(true).optional(),
|
|
254
|
+
policy: zod.z.string().min(1).max(2e3).optional()
|
|
255
|
+
}).refine((v) => v.allow === true !== (typeof v.policy === "string"), {
|
|
256
|
+
message: "A variant declares exactly one of allow: true or policy"
|
|
257
|
+
});
|
|
258
|
+
var policyExpressionSchema = zod.z.union([
|
|
259
|
+
zod.z.string().min(1).max(2e3),
|
|
260
|
+
zod.z.object({
|
|
261
|
+
default: zod.z.string().min(1).max(2e3),
|
|
262
|
+
variants: zod.z.array(policyVariantSchema).max(10).default([])
|
|
263
|
+
})
|
|
264
|
+
]);
|
|
265
|
+
var identifier = zod.z.string().min(1).max(63).regex(/^[a-z][a-z0-9_]*$/);
|
|
266
|
+
var durationMs = zod.z.number().int().positive().max(24 * 60 * 60 * 1e3);
|
|
267
|
+
var RESOLVED_VIEW_SYSTEM_COLUMNS = {
|
|
268
|
+
writebackStatus: { name: "writebackStatus", columnName: "_writeback_status", type: "text" },
|
|
269
|
+
writebackError: { name: "writebackError", columnName: "_writeback_error", type: "text" }
|
|
270
|
+
};
|
|
271
|
+
var RESOLVED_SOURCE_SYSTEM_COLUMNS = {
|
|
272
|
+
tombstoned: { name: "sysTombstoned", columnName: "_tombstoned" },
|
|
273
|
+
fetchedAt: { name: "sysFetchedAt", columnName: "_fetched_at" },
|
|
274
|
+
writebackStatus: { name: "sysWritebackStatus", columnName: "_writeback_status" },
|
|
275
|
+
writebackError: { name: "sysWritebackError", columnName: "_writeback_error" },
|
|
276
|
+
writebackAt: { name: "sysWritebackAt", columnName: "_writeback_at" }
|
|
277
|
+
};
|
|
278
|
+
var RESOLVED_VIEW_CELL_PARTS = ["source", "at", "by", "conflicts"];
|
|
279
|
+
/* @__PURE__ */ new Set([
|
|
280
|
+
...Object.values(RESOLVED_VIEW_SYSTEM_COLUMNS).flatMap((c) => [c.name, c.columnName]),
|
|
281
|
+
...Object.values(RESOLVED_SOURCE_SYSTEM_COLUMNS).flatMap((c) => [c.name, c.columnName])
|
|
282
|
+
]);
|
|
283
|
+
var COMPARATORS = ["latest", "highest", "lowest", "priority", "nonNullFirst", "custom"];
|
|
284
|
+
var canonicalFunctionRefSchema = zod.z.object({
|
|
285
|
+
/** `managed` = a Managed Function id/slug; `native` = an in-process Atlas function (reserved). */
|
|
286
|
+
kind: zod.z.enum(["managed", "native"]),
|
|
287
|
+
id: zod.z.string().min(1).max(200)
|
|
288
|
+
}).strict();
|
|
289
|
+
var canonicalFetchShapeSchema = zod.z.union([
|
|
290
|
+
zod.z.object({ shape: zod.z.literal("single") }).strict(),
|
|
291
|
+
zod.z.object({ shape: zod.z.literal("batch"), maxKeys: zod.z.number().int().min(2).max(500) }).strict()
|
|
292
|
+
]);
|
|
293
|
+
var canonicalResolvedSourceSchema = zod.z.object({
|
|
294
|
+
name: identifier,
|
|
295
|
+
kind: zod.z.enum(["manual", "etl", "prefetch"]),
|
|
296
|
+
/** Backing relation in this namespace: an internal table (manual/prefetch) or a synced table (etl). */
|
|
297
|
+
table: identifier,
|
|
298
|
+
/** Resolved columns (logical names) this source supplies — the primary key is implicit. */
|
|
299
|
+
columns: zod.z.array(zod.z.string().min(1).max(63)).min(1).max(500),
|
|
300
|
+
/** etl: logical timestamptz/date column on the synced table used as every cell's updated_at. */
|
|
301
|
+
updatedAt: zod.z.string().min(1).max(63).optional(),
|
|
302
|
+
/** prefetch: resolved column holding the source system's key (may be the primary key). */
|
|
303
|
+
key: zod.z.string().min(1).max(63).optional(),
|
|
304
|
+
fetch: canonicalFetchShapeSchema.optional(),
|
|
305
|
+
function: canonicalFunctionRefSchema.optional(),
|
|
306
|
+
prefetchDefaults: zod.z.object({
|
|
307
|
+
maxStalenessMs: durationMs,
|
|
308
|
+
timeoutMs: durationMs,
|
|
309
|
+
onError: zod.z.enum(["fallback", "fail"])
|
|
310
|
+
}).strict().optional(),
|
|
311
|
+
writeback: zod.z.object({
|
|
312
|
+
columns: zod.z.array(zod.z.string().min(1).max(63)).min(1).max(500),
|
|
313
|
+
defaultMode: zod.z.enum(["sync", "async"]),
|
|
314
|
+
timeoutMs: durationMs,
|
|
315
|
+
/** Remove confirmed cells from the manual source so a stale edit cannot shadow the source. */
|
|
316
|
+
clearManualOnConfirm: zod.z.boolean()
|
|
317
|
+
}).strict().optional()
|
|
318
|
+
}).strict();
|
|
319
|
+
var canonicalResolveRuleSchema = zod.z.object({
|
|
320
|
+
column: zod.z.string().min(1).max(63),
|
|
321
|
+
comparator: zod.z.enum(COMPARATORS),
|
|
322
|
+
/** Source names, in tie-break order. */
|
|
323
|
+
candidates: zod.z.array(identifier).min(1).max(16),
|
|
324
|
+
/** custom only: SQL ORDER BY key over value / updated_at / source / actor / ord. */
|
|
325
|
+
orderBy: zod.z.string().min(1).max(2e3).optional()
|
|
326
|
+
}).strict();
|
|
327
|
+
var canonicalResolvedSchema = zod.z.object({
|
|
328
|
+
sources: zod.z.array(canonicalResolvedSourceSchema).min(1).max(16),
|
|
329
|
+
/** One rule per resolved (non-pk) column; documents the generated view for tooling. */
|
|
330
|
+
resolve: zod.z.array(canonicalResolveRuleSchema).max(500),
|
|
331
|
+
/** Sources whose tombstone hides the resolved row (v1: the manual source only). */
|
|
332
|
+
deletedBy: zod.z.array(identifier).max(1).default([]),
|
|
333
|
+
/**
|
|
334
|
+
* Materialized: the view reads an indexed internal table (`table`) that the
|
|
335
|
+
* data plane keeps in step with `compute`, the internal view holding the
|
|
336
|
+
* resolution query. Omitted when the view computes on read.
|
|
337
|
+
*/
|
|
338
|
+
materialized: zod.z.object({ table: identifier, compute: identifier }).strict().optional(),
|
|
339
|
+
/** Write policies evaluated against the RESOLVED row (reads use the view's read policy). */
|
|
340
|
+
policies: zod.z.object({
|
|
341
|
+
write: policyExpressionSchema.optional(),
|
|
342
|
+
insert: policyExpressionSchema.optional(),
|
|
343
|
+
update: policyExpressionSchema.optional()
|
|
344
|
+
}).strict().default({})
|
|
345
|
+
}).strict();
|
|
346
|
+
function validateResolvedView({ view, tables, views = [] }) {
|
|
347
|
+
const resolved = view.resolved;
|
|
348
|
+
if (!resolved) return [];
|
|
349
|
+
const errors = [];
|
|
350
|
+
const fail = (message) => errors.push(`resolved table '${view.view}': ${message}`);
|
|
351
|
+
const pkColumn = view.primaryKey?.length === 1 ? view.columns.find((c) => c.columnName === view.primaryKey?.[0]) : void 0;
|
|
352
|
+
if (!pkColumn) {
|
|
353
|
+
fail("requires exactly one primary-key column");
|
|
354
|
+
return errors;
|
|
355
|
+
}
|
|
356
|
+
const systemNames = new Set(Object.values(RESOLVED_VIEW_SYSTEM_COLUMNS).map((c) => c.name));
|
|
357
|
+
const scope = {
|
|
358
|
+
view,
|
|
359
|
+
resolved,
|
|
360
|
+
pkColumn,
|
|
361
|
+
dataColumns: new Map(view.columns.filter((c) => !systemNames.has(c.name) && c.cell === void 0).map((c) => [c.name, c])),
|
|
362
|
+
sourceByName: /* @__PURE__ */ new Map(),
|
|
363
|
+
fail
|
|
364
|
+
};
|
|
365
|
+
if (resolved.sources.filter((source) => source.kind === "manual").length !== 1) fail("requires exactly one manual source");
|
|
366
|
+
const tableByName = new Map(tables.map((table) => [table.table, table]));
|
|
367
|
+
for (const source of resolved.sources) {
|
|
368
|
+
if (scope.sourceByName.has(source.name)) fail(`duplicate source '${source.name}'`);
|
|
369
|
+
scope.sourceByName.set(source.name, source);
|
|
370
|
+
const table = tableByName.get(source.table);
|
|
371
|
+
if (!table) fail(`source '${source.name}' references unknown table '${source.table}'`);
|
|
372
|
+
else validateSource({ scope, source, table });
|
|
373
|
+
}
|
|
374
|
+
validateRules(scope);
|
|
375
|
+
validateViewCells(scope);
|
|
376
|
+
if (resolved.materialized) validateMaterialized({ scope, tableByName, views });
|
|
377
|
+
for (const name of resolved.deletedBy) {
|
|
378
|
+
if (scope.sourceByName.get(name)?.kind !== "manual") fail(`deletedBy '${name}' must be the manual source`);
|
|
379
|
+
}
|
|
380
|
+
return errors;
|
|
381
|
+
}
|
|
382
|
+
function validateSource({ scope, source, table }) {
|
|
383
|
+
const { fail } = scope;
|
|
384
|
+
if (source.kind === "etl" && table.source === void 0) fail(`etl source '${source.name}' must reference a synced table`);
|
|
385
|
+
if (source.kind !== "etl" && (table.internal !== true || table.cellMetadata !== true)) {
|
|
386
|
+
fail(`${source.kind} source '${source.name}' must reference an internal table with cell metadata`);
|
|
387
|
+
}
|
|
388
|
+
const hasRev = table.columns.some((column) => column.cell?.part === "rev");
|
|
389
|
+
if (source.kind === "manual" && !hasRev) fail(`manual source '${source.name}' must carry '__rev' cell columns`);
|
|
390
|
+
if (source.kind === "prefetch" && hasRev) fail(`prefetch source '${source.name}' must not carry '__rev' cell columns`);
|
|
391
|
+
const byName = new Map(table.columns.map((c) => [c.name, c]));
|
|
392
|
+
validateSourceColumns({ scope, source, byName });
|
|
393
|
+
if (source.kind === "etl") {
|
|
394
|
+
const updatedAt = source.updatedAt === void 0 ? void 0 : byName.get(source.updatedAt);
|
|
395
|
+
if (source.updatedAt !== void 0 && updatedAt?.type !== "timestamptz" && updatedAt?.type !== "date") {
|
|
396
|
+
fail(`etl source '${source.name}' updatedAt must name a timestamptz/date column on '${source.table}'`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
validatePrefetchFields({ scope, source });
|
|
400
|
+
}
|
|
401
|
+
function validatePrefetchFields({ scope, source }) {
|
|
402
|
+
const { fail, pkColumn } = scope;
|
|
403
|
+
if (source.kind !== "prefetch") {
|
|
404
|
+
if (source.key || source.fetch || source.function || source.prefetchDefaults || source.writeback) {
|
|
405
|
+
fail(`only prefetch sources may declare key/fetch/function/prefetchDefaults/writeback ('${source.name}')`);
|
|
406
|
+
}
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (!source.key || !source.fetch || !source.function || !source.prefetchDefaults) {
|
|
410
|
+
fail(`prefetch source '${source.name}' requires key, fetch, function and prefetchDefaults`);
|
|
411
|
+
} else if (source.key !== pkColumn.name && !source.columns.includes(source.key)) {
|
|
412
|
+
fail(`prefetch source '${source.name}' key '${source.key}' must be the primary key or one of its columns`);
|
|
413
|
+
}
|
|
414
|
+
for (const column of source.writeback?.columns ?? []) {
|
|
415
|
+
if (!source.columns.includes(column)) fail(`prefetch source '${source.name}' writes back '${column}', which it does not supply`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function validateSourceColumns({ scope, source, byName }) {
|
|
419
|
+
const { fail, pkColumn, dataColumns } = scope;
|
|
420
|
+
for (const column of [pkColumn.name, ...source.columns]) {
|
|
421
|
+
const own = column === pkColumn.name ? pkColumn : dataColumns.get(column);
|
|
422
|
+
const theirs = byName.get(column);
|
|
423
|
+
if (!own) fail(`source '${source.name}' supplies unknown column '${column}'`);
|
|
424
|
+
else if (!theirs) fail(`source '${source.name}' table '${source.table}' has no column '${column}'`);
|
|
425
|
+
else if (JSON.stringify(theirs.type) !== JSON.stringify(own.type)) fail(`column '${column}' has a different type on source '${source.name}'`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function validateRules({ resolved, pkColumn, dataColumns, sourceByName, fail }) {
|
|
429
|
+
const ruled = /* @__PURE__ */ new Set();
|
|
430
|
+
for (const rule of resolved.resolve) {
|
|
431
|
+
if (!dataColumns.has(rule.column) || rule.column === pkColumn.name) {
|
|
432
|
+
fail(`resolve rule targets unknown column '${rule.column}'`);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (ruled.has(rule.column)) fail(`column '${rule.column}' has more than one resolve rule`);
|
|
436
|
+
ruled.add(rule.column);
|
|
437
|
+
if (rule.comparator === "custom" !== (rule.orderBy !== void 0)) {
|
|
438
|
+
fail(`column '${rule.column}': orderBy is required for custom comparators and only allowed there`);
|
|
439
|
+
}
|
|
440
|
+
if (new Set(rule.candidates).size !== rule.candidates.length) fail(`column '${rule.column}' repeats a candidate`);
|
|
441
|
+
for (const candidate of rule.candidates) {
|
|
442
|
+
const source = sourceByName.get(candidate);
|
|
443
|
+
if (!source) fail(`column '${rule.column}' names unknown candidate source '${candidate}'`);
|
|
444
|
+
else if (!source.columns.includes(rule.column)) fail(`column '${rule.column}' candidate '${candidate}' does not supply it`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
for (const column of dataColumns.keys()) {
|
|
448
|
+
if (column !== pkColumn.name && !ruled.has(column)) fail(`column '${column}' has no resolve rule`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
var VIEW_CELL_TYPES = {
|
|
452
|
+
source: "text",
|
|
453
|
+
at: "timestamptz",
|
|
454
|
+
by: "text",
|
|
455
|
+
conflicts: "jsonb"
|
|
456
|
+
};
|
|
457
|
+
function validateViewCells({ view, dataColumns, pkColumn, fail }) {
|
|
458
|
+
const expected = /* @__PURE__ */ new Map();
|
|
459
|
+
for (const column of dataColumns.values()) {
|
|
460
|
+
if (column.name === pkColumn.name) continue;
|
|
461
|
+
for (const part of RESOLVED_VIEW_CELL_PARTS) {
|
|
462
|
+
expected.set(cellColumnName(column.name, part), { part, columnName: cellColumnName(column.columnName, part) });
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
for (const column of view.columns) {
|
|
466
|
+
if (!column.cell) continue;
|
|
467
|
+
const want = expected.get(column.name);
|
|
468
|
+
if (!want || want.part !== column.cell.part || want.columnName !== column.columnName || cellColumnName(column.cell.of, column.cell.part) !== column.name) {
|
|
469
|
+
fail(`unexpected cell column '${column.name}'`);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (column.type !== VIEW_CELL_TYPES[column.cell.part]) fail(`cell column '${column.name}' must be ${VIEW_CELL_TYPES[column.cell.part]}`);
|
|
473
|
+
expected.delete(column.name);
|
|
474
|
+
}
|
|
475
|
+
for (const name of expected.keys()) fail(`missing cell column '${name}'`);
|
|
476
|
+
}
|
|
477
|
+
var columnSignature = (columns) => JSON.stringify(columns.map((column) => [column.name, column.columnName, column.type]));
|
|
478
|
+
function validateMaterialized({ scope, tableByName, views }) {
|
|
479
|
+
const { view, resolved, fail } = scope;
|
|
480
|
+
const { table: tableName, compute: computeName } = resolved.materialized;
|
|
481
|
+
const table = tableByName.get(tableName);
|
|
482
|
+
if (!table || table.internal !== true || table.materializes !== view.view) {
|
|
483
|
+
fail(`materialized table '${tableName}' must be an internal table that materializes '${view.view}'`);
|
|
484
|
+
} else {
|
|
485
|
+
if (columnSignature(table.columns) !== columnSignature(view.columns)) fail(`materialized table '${tableName}' must have exactly the view's columns`);
|
|
486
|
+
if (JSON.stringify(table.primaryKey ?? []) !== JSON.stringify(view.primaryKey ?? [])) fail(`materialized table '${tableName}' must share the view's primary key`);
|
|
487
|
+
}
|
|
488
|
+
const compute = views.find((candidate) => candidate.view === computeName);
|
|
489
|
+
if (!compute || compute.internal !== true) fail(`compute view '${computeName}' must be an internal view`);
|
|
490
|
+
else if (columnSignature(compute.columns) !== columnSignature(view.columns)) fail(`compute view '${computeName}' must have exactly the view's columns`);
|
|
491
|
+
for (const source of resolved.sources) {
|
|
492
|
+
if (source.kind !== "etl") continue;
|
|
493
|
+
const read = tableByName.get(source.table)?.policies?.read;
|
|
494
|
+
const unconditional = typeof read === "string" ? read.trim() === "true" : read !== void 0 && read.default.trim() === "true" && read.variants.length === 0;
|
|
495
|
+
if (!unconditional) {
|
|
496
|
+
fail(`etl source '${source.name}' must have read policy 'true' to be materialized (the copy is shared by every caller)`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/canonical/v2.ts
|
|
36
502
|
var CANONICAL_FORMAT = 2;
|
|
37
503
|
var IDENTIFIER_RE = /^[a-z][a-z0-9_]*$/;
|
|
38
|
-
var
|
|
504
|
+
var identifier2 = zod.z.string().min(1).max(63).regex(IDENTIFIER_RE);
|
|
39
505
|
var columnIdentifier = zod.z.string().min(1).max(63).regex(COLUMN_IDENTIFIER_PATTERN);
|
|
40
506
|
var COLUMN_TYPES = [
|
|
41
507
|
"uuid",
|
|
@@ -51,14 +517,14 @@ var COLUMN_TYPES = [
|
|
|
51
517
|
];
|
|
52
518
|
var columnTypeSchema = zod.z.enum(COLUMN_TYPES);
|
|
53
519
|
var canonicalEnumSchema = zod.z.object({
|
|
54
|
-
name:
|
|
520
|
+
name: identifier2,
|
|
55
521
|
values: zod.z.array(zod.z.string().min(1).max(63)).min(1).max(100)
|
|
56
522
|
});
|
|
57
523
|
var canonicalColumnSchema = zod.z.object({
|
|
58
524
|
/** Property name in TS (camelCase); columnName is the Postgres name. */
|
|
59
525
|
name: zod.z.string().min(1).max(63),
|
|
60
526
|
columnName: columnIdentifier,
|
|
61
|
-
type: zod.z.union([columnTypeSchema, zod.z.object({ enum:
|
|
527
|
+
type: zod.z.union([columnTypeSchema, zod.z.object({ enum: identifier2 })]),
|
|
62
528
|
nullable: zod.z.boolean().default(true),
|
|
63
529
|
unique: zod.z.boolean().default(false),
|
|
64
530
|
/** Literal default, or one of the allowlisted SQL functions. */
|
|
@@ -74,7 +540,14 @@ var canonicalColumnSchema = zod.z.object({
|
|
|
74
540
|
/** Migration hints — same semantics as v1. */
|
|
75
541
|
renamedFrom: columnIdentifier.optional(),
|
|
76
542
|
cast: zod.z.string().max(200).optional(),
|
|
77
|
-
deprecated: zod.z.boolean().default(false)
|
|
543
|
+
deprecated: zod.z.boolean().default(false),
|
|
544
|
+
/**
|
|
545
|
+
* Marks a system cell-metadata column (see CELL_PARTS in constants): part
|
|
546
|
+
* `part` of the cell of logical column `of`. Synthesized by authoring,
|
|
547
|
+
* never client-writable, absent from GraphQL scalar fields. Omitted on
|
|
548
|
+
* ordinary columns so their hashes are unchanged.
|
|
549
|
+
*/
|
|
550
|
+
cell: zod.z.object({ of: zod.z.string().min(1).max(63), part: zod.z.enum(CELL_PARTS) }).strict().optional()
|
|
78
551
|
}).superRefine((column, ctx) => {
|
|
79
552
|
if (column.onUpdateFn === void 0) return;
|
|
80
553
|
if (typeof column.type !== "string" || column.type !== "timestamptz" && column.type !== "date") {
|
|
@@ -89,36 +562,26 @@ var canonicalLinkSchema = zod.z.object({
|
|
|
89
562
|
name: zod.z.string().min(1).max(63),
|
|
90
563
|
kind: zod.z.enum(["toOne", "toMany", "toPlatformUser"]),
|
|
91
564
|
/** Target table name (namespace-local). Absent for toPlatformUser. */
|
|
92
|
-
target:
|
|
565
|
+
target: identifier2.optional(),
|
|
93
566
|
/** FK column on the owning side (toOne/toPlatformUser: on this table; toMany: on the target). */
|
|
94
567
|
on: columnIdentifier,
|
|
95
568
|
/** Referenced column on the target (default: target's PK / platform user `sub`). */
|
|
96
569
|
references: columnIdentifier.optional(),
|
|
97
570
|
/** enforce: true emits a real FK constraint (same-namespace toOne only). */
|
|
98
571
|
enforce: zod.z.boolean().default(false),
|
|
572
|
+
onDelete: zod.z.enum(["cascade", "restrict", "no action", "set null", "set default"]).optional(),
|
|
573
|
+
onUpdate: zod.z.enum(["cascade", "restrict", "no action", "set null", "set default"]).optional(),
|
|
99
574
|
description: zod.z.string().max(500).optional()
|
|
100
575
|
});
|
|
101
576
|
var canonicalIndexSchema = zod.z.object({
|
|
102
577
|
columns: zod.z.array(columnIdentifier).min(1),
|
|
103
578
|
unique: zod.z.boolean().default(false),
|
|
104
|
-
name:
|
|
105
|
-
});
|
|
106
|
-
var policyVariantSchema = zod.z.object({
|
|
107
|
-
/** Claims-only guard (capability()/role()/claim() comparisons — no columns). */
|
|
108
|
-
when: zod.z.string().min(1).max(1e3),
|
|
109
|
-
/** Exactly one of: allow-all bypass, or a row predicate. */
|
|
110
|
-
allow: zod.z.literal(true).optional(),
|
|
111
|
-
policy: zod.z.string().min(1).max(2e3).optional()
|
|
112
|
-
}).refine((v) => v.allow === true !== (typeof v.policy === "string"), {
|
|
113
|
-
message: "A variant declares exactly one of allow: true or policy"
|
|
579
|
+
name: identifier2.optional()
|
|
114
580
|
});
|
|
115
|
-
var
|
|
116
|
-
|
|
117
|
-
zod.z.
|
|
118
|
-
|
|
119
|
-
variants: zod.z.array(policyVariantSchema).max(10).default([])
|
|
120
|
-
})
|
|
121
|
-
]);
|
|
581
|
+
var canonicalCheckSchema = zod.z.object({
|
|
582
|
+
name: identifier2,
|
|
583
|
+
expression: zod.z.string().min(1).max(2e3)
|
|
584
|
+
}).strict();
|
|
122
585
|
var canonicalPoliciesSchema = zod.z.object({
|
|
123
586
|
read: policyExpressionSchema.optional(),
|
|
124
587
|
write: policyExpressionSchema.optional(),
|
|
@@ -149,7 +612,7 @@ function isSingleViewQuerySql(value) {
|
|
|
149
612
|
return state === "normal" && !/\$[a-z_0-9]*\$/i.test(query);
|
|
150
613
|
}
|
|
151
614
|
var canonicalActionSchema = zod.z.object({
|
|
152
|
-
name:
|
|
615
|
+
name: identifier2,
|
|
153
616
|
requires: zod.z.string().max(1e3).optional(),
|
|
154
617
|
params: zod.z.record(zod.z.string(), zod.z.object({
|
|
155
618
|
type: columnTypeSchema,
|
|
@@ -161,9 +624,9 @@ var canonicalActionSchema = zod.z.object({
|
|
|
161
624
|
});
|
|
162
625
|
var canonicalCowMutationSchema = zod.z.object({
|
|
163
626
|
kind: zod.z.enum(["override", "revert"]),
|
|
164
|
-
name:
|
|
627
|
+
name: identifier2,
|
|
165
628
|
/** Synced table (same namespace) the overlay copies from / falls back to. */
|
|
166
|
-
synced:
|
|
629
|
+
synced: identifier2,
|
|
167
630
|
/** Single-column primary key PROPERTY (TS camelCase, like column `name`) shared by overlay and synced. */
|
|
168
631
|
pk: zod.z.string().min(1).max(63),
|
|
169
632
|
/** Override: contract properties copied INSERT…SELECT from synced (pk included). Empty for revert. */
|
|
@@ -191,23 +654,27 @@ var canonicalCowMutationSchema = zod.z.object({
|
|
|
191
654
|
}
|
|
192
655
|
});
|
|
193
656
|
var canonicalForeignKeySchema = zod.z.object({
|
|
194
|
-
name:
|
|
657
|
+
name: identifier2,
|
|
195
658
|
columns: zod.z.array(columnIdentifier).min(1).max(8),
|
|
196
|
-
target:
|
|
659
|
+
target: identifier2,
|
|
197
660
|
references: zod.z.array(columnIdentifier).min(1).max(8),
|
|
198
|
-
deferrable: zod.z.enum(["initially-immediate", "initially-deferred"]).optional()
|
|
661
|
+
deferrable: zod.z.enum(["initially-immediate", "initially-deferred"]).optional(),
|
|
662
|
+
onDelete: zod.z.enum(["cascade", "restrict", "no action", "set null", "set default"]).optional(),
|
|
663
|
+
onUpdate: zod.z.enum(["cascade", "restrict", "no action", "set null", "set default"]).optional()
|
|
199
664
|
}).strict();
|
|
200
665
|
var canonicalTableSchema = zod.z.object({
|
|
201
|
-
table:
|
|
666
|
+
table: identifier2,
|
|
202
667
|
description: zod.z.string().max(2e3).optional(),
|
|
203
668
|
/** Table-level rename hint (new in v2). */
|
|
204
|
-
renamedFrom:
|
|
669
|
+
renamedFrom: identifier2.optional(),
|
|
205
670
|
/** Synced/reflected read-only tables (`source: gold.*`) — v1 semantics. */
|
|
206
671
|
source: zod.z.string().regex(/^gold\.[a-z][a-z0-9_]*$/).optional(),
|
|
207
672
|
columns: zod.z.array(canonicalColumnSchema).default([]),
|
|
208
673
|
primaryKey: zod.z.array(columnIdentifier).min(1).max(8).optional(),
|
|
209
674
|
links: zod.z.array(canonicalLinkSchema).default([]),
|
|
210
675
|
indexes: zod.z.array(canonicalIndexSchema).default([]),
|
|
676
|
+
/** Omitted by old definitions, preserving their existing content hashes. */
|
|
677
|
+
checks: zod.z.array(canonicalCheckSchema).max(100).optional(),
|
|
211
678
|
policies: canonicalPoliciesSchema.default({}),
|
|
212
679
|
/**
|
|
213
680
|
* v1-conversion carry-over only. v2 has no action surface — the runtime
|
|
@@ -218,22 +685,55 @@ var canonicalTableSchema = zod.z.object({
|
|
|
218
685
|
/** Optional (not defaulted) so pre-CoW canonical documents keep byte-identical hashes. */
|
|
219
686
|
cowMutations: zod.z.array(canonicalCowMutationSchema).optional(),
|
|
220
687
|
/** Omitted by old definitions, preserving their existing content hashes. */
|
|
221
|
-
foreignKeys: zod.z.array(canonicalForeignKeySchema).max(100).optional()
|
|
688
|
+
foreignKeys: zod.z.array(canonicalForeignKeySchema).max(100).optional(),
|
|
689
|
+
/**
|
|
690
|
+
* Per-cell edit metadata (`cellMetadata: true` in authoring): every tracked
|
|
691
|
+
* column `c` carries typed `c__at` / `c__by` system columns (plus `c__rev`
|
|
692
|
+
* on a resolved table's internal sources), listed explicitly in `columns`
|
|
693
|
+
* with a `cell` marker so DDL and migrations treat them as ordinary columns.
|
|
694
|
+
* Omitted when off so existing definitions hash unchanged.
|
|
695
|
+
*/
|
|
696
|
+
cellMetadata: zod.z.literal(true).optional(),
|
|
697
|
+
/**
|
|
698
|
+
* System-owned backing table of a resolved table (its manual or prefetch
|
|
699
|
+
* source). Internal tables get DDL and IR metadata but no GraphQL surface;
|
|
700
|
+
* only the data plane writes them. Omitted when false.
|
|
701
|
+
*/
|
|
702
|
+
internal: zod.z.literal(true).optional(),
|
|
703
|
+
/**
|
|
704
|
+
* The materialized copy of resolved table `materializes` (internal): the
|
|
705
|
+
* same columns as that view, including its cell parts, maintained by the
|
|
706
|
+
* data plane. Omitted otherwise.
|
|
707
|
+
*/
|
|
708
|
+
materializes: identifier2.optional()
|
|
222
709
|
});
|
|
710
|
+
var MAX_VIEW_QUERY = 1e5;
|
|
711
|
+
var MAX_RESOLVED_VIEW_QUERY = 5e6;
|
|
223
712
|
var canonicalViewSchema = zod.z.object({
|
|
224
|
-
view:
|
|
713
|
+
view: identifier2,
|
|
225
714
|
description: zod.z.string().max(2e3).optional(),
|
|
226
715
|
columns: zod.z.array(canonicalColumnSchema).min(1),
|
|
227
716
|
/** Logical identity for GraphQL only; PostgreSQL views have no PK constraint. */
|
|
228
717
|
primaryKey: zod.z.array(columnIdentifier).min(1).max(8).optional(),
|
|
229
718
|
links: zod.z.array(canonicalLinkSchema).default([]),
|
|
230
719
|
policies: zod.z.object({ read: policyExpressionSchema }).strict(),
|
|
231
|
-
query: zod.z.string().min(1).max(
|
|
720
|
+
query: zod.z.string().min(1).max(MAX_RESOLVED_VIEW_QUERY).refine(isSingleViewQuerySql, {
|
|
232
721
|
message: "view query must be one SELECT/WITH statement without comments or dollar-quoted bodies"
|
|
233
722
|
}),
|
|
234
723
|
/** Namespace-local logical relation names, sorted for deterministic hashing. */
|
|
235
|
-
dependencies: zod.z.array(
|
|
724
|
+
dependencies: zod.z.array(identifier2).max(500),
|
|
725
|
+
/**
|
|
726
|
+
* Present when this view is a resolved table (defineResolvedTable): the
|
|
727
|
+
* multi-source metadata behind its generated query. Omitted otherwise so
|
|
728
|
+
* existing views hash unchanged.
|
|
729
|
+
*/
|
|
730
|
+
resolved: canonicalResolvedSchema.optional(),
|
|
731
|
+
/** System-owned view (a materialized resolved table's compute view): no GraphQL surface. */
|
|
732
|
+
internal: zod.z.literal(true).optional()
|
|
236
733
|
}).superRefine((view, ctx) => {
|
|
734
|
+
if (view.resolved === void 0 && view.query.length > MAX_VIEW_QUERY) {
|
|
735
|
+
ctx.addIssue({ code: "custom", path: ["query"], message: `view query must be at most ${MAX_VIEW_QUERY} characters` });
|
|
736
|
+
}
|
|
237
737
|
for (const [index, column] of view.columns.entries()) {
|
|
238
738
|
if (column.default !== void 0 || column.defaultFn !== void 0) {
|
|
239
739
|
ctx.addIssue({ code: "custom", path: ["columns", index], message: `view column '${column.name}' cannot declare a default` });
|
|
@@ -248,15 +748,82 @@ var canonicalViewSchema = zod.z.object({
|
|
|
248
748
|
ctx.addIssue({ code: "custom", path: ["columns", index], message: `view column '${column.name}' cannot declare migration hints` });
|
|
249
749
|
}
|
|
250
750
|
}
|
|
751
|
+
if (view.resolved === void 0 && view.internal !== true) {
|
|
752
|
+
for (const [index, column] of view.columns.entries()) {
|
|
753
|
+
if (column.cell) ctx.addIssue({ code: "custom", path: ["columns", index], message: `only resolved tables carry cell columns ('${column.name}')` });
|
|
754
|
+
}
|
|
755
|
+
}
|
|
251
756
|
for (const [index, link] of view.links.entries()) {
|
|
252
757
|
if (link.enforce) {
|
|
253
758
|
ctx.addIssue({ code: "custom", path: ["links", index, "enforce"], message: `view link '${link.name}' cannot enforce a foreign key` });
|
|
254
759
|
}
|
|
760
|
+
if (link.onDelete !== void 0 || link.onUpdate !== void 0) {
|
|
761
|
+
ctx.addIssue({ code: "custom", path: ["links", index], message: `view link '${link.name}' cannot declare referential actions` });
|
|
762
|
+
}
|
|
255
763
|
}
|
|
256
764
|
});
|
|
765
|
+
var CELL_PART_TYPES = {
|
|
766
|
+
at: "timestamptz",
|
|
767
|
+
by: "text",
|
|
768
|
+
rev: "uuid",
|
|
769
|
+
source: "text",
|
|
770
|
+
conflicts: "jsonb"
|
|
771
|
+
};
|
|
772
|
+
function trackedCellColumns(table) {
|
|
773
|
+
const pk = new Set(table.primaryKey ?? []);
|
|
774
|
+
return table.columns.filter((column) => column.cell === void 0 && !pk.has(column.columnName) && column.onUpdateFn === void 0);
|
|
775
|
+
}
|
|
776
|
+
var SUFFIXES = Object.values(CELL_PART_SUFFIX);
|
|
777
|
+
function validateCellMetadata(table, index, ctx) {
|
|
778
|
+
const fail = (message, path = ["tables", index]) => ctx.addIssue({ code: "custom", path, message });
|
|
779
|
+
const cells = table.columns.flatMap((column, position) => column.cell ? [{ column, position, cell: column.cell }] : []);
|
|
780
|
+
if (table.materializes !== void 0) return;
|
|
781
|
+
if (table.cellMetadata !== true) {
|
|
782
|
+
for (const { position } of cells) fail(`cell-metadata columns require cellMetadata on table '${table.table}'`, ["tables", index, "columns", position]);
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
if (table.source !== void 0) {
|
|
786
|
+
fail(`synced table '${table.table}' cannot track cell metadata`);
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
const tracked = trackedCellColumns(table);
|
|
790
|
+
const byName = new Map(tracked.map((column) => [column.name, column]));
|
|
791
|
+
const withRev = cells.some(({ cell }) => cell.part === "rev");
|
|
792
|
+
if (withRev && table.internal !== true) fail(`only a resolved table's internal sources carry '__rev' cell columns ('${table.table}')`);
|
|
793
|
+
const seen = /* @__PURE__ */ new Set();
|
|
794
|
+
for (const { column, position, cell } of cells) {
|
|
795
|
+
const path = ["tables", index, "columns", position];
|
|
796
|
+
const owner = byName.get(cell.of);
|
|
797
|
+
if (!owner) {
|
|
798
|
+
fail(`cell column '${column.name}' belongs to '${cell.of}', which is not a tracked column`, path);
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
if (cell.part !== "at" && cell.part !== "by" && cell.part !== "rev") fail(`a table cell column must be part at/by/rev, not '${cell.part}'`, path);
|
|
802
|
+
if (column.name !== cellColumnName(owner.name, cell.part) || column.columnName !== cellColumnName(owner.columnName, cell.part)) {
|
|
803
|
+
fail(`cell column for '${owner.name}' ${cell.part} must be named '${cellColumnName(owner.name, cell.part)}' / '${cellColumnName(owner.columnName, cell.part)}'`, path);
|
|
804
|
+
}
|
|
805
|
+
if (column.type !== CELL_PART_TYPES[cell.part] || !column.nullable || column.unique || column.default !== void 0 || column.defaultFn !== void 0) {
|
|
806
|
+
fail(`cell column '${column.name}' must be a nullable, default-less ${CELL_PART_TYPES[cell.part]}`, path);
|
|
807
|
+
}
|
|
808
|
+
const key = `${cell.of}:${cell.part}`;
|
|
809
|
+
if (seen.has(key)) fail(`duplicate cell column '${column.name}'`, path);
|
|
810
|
+
seen.add(key);
|
|
811
|
+
}
|
|
812
|
+
for (const column of tracked) {
|
|
813
|
+
if (column.columnName.length > MAX_TRACKED_COLUMN_NAME) {
|
|
814
|
+
fail(`column '${column.columnName}' is too long to track cell metadata (max ${MAX_TRACKED_COLUMN_NAME} characters)`);
|
|
815
|
+
}
|
|
816
|
+
if (SUFFIXES.some((suffix) => column.columnName.endsWith(suffix))) {
|
|
817
|
+
fail(`column '${column.columnName}' ends in a reserved cell-metadata suffix (${SUFFIXES.join(", ")})`);
|
|
818
|
+
}
|
|
819
|
+
for (const part of withRev ? ["at", "by", "rev"] : ["at", "by"]) {
|
|
820
|
+
if (!seen.has(`${column.name}:${part}`)) fail(`tracked column '${column.name}' is missing its '${cellColumnName(column.columnName, part)}' cell column`);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
257
824
|
zod.z.object({
|
|
258
825
|
definition_format: zod.z.literal(CANONICAL_FORMAT),
|
|
259
|
-
namespace:
|
|
826
|
+
namespace: identifier2.max(41),
|
|
260
827
|
description: zod.z.string().max(2e3).optional(),
|
|
261
828
|
enums: zod.z.array(canonicalEnumSchema).default([]),
|
|
262
829
|
tables: zod.z.array(canonicalTableSchema).min(0).max(500),
|
|
@@ -274,6 +841,7 @@ zod.z.object({
|
|
|
274
841
|
ctx.addIssue({ code: "custom", path: ["tables", index, "table"], message: `duplicate relation '${table.table}'` });
|
|
275
842
|
}
|
|
276
843
|
seen.add(table.table);
|
|
844
|
+
validateCellMetadata(table, index, ctx);
|
|
277
845
|
if (table.source !== void 0) {
|
|
278
846
|
for (const [columnIndex, column] of table.columns.entries()) {
|
|
279
847
|
if (column.onUpdateFn !== void 0) {
|
|
@@ -293,6 +861,26 @@ zod.z.object({
|
|
|
293
861
|
...table.columns.filter((column) => column.unique && !(table.primaryKey?.length === 1 && table.primaryKey[0] === column.columnName)).map((column) => `${table.table}_${column.columnName}_unique`),
|
|
294
862
|
...table.links.filter((link) => link.enforce).map((link) => `fk_${table.table}_${link.name}`)
|
|
295
863
|
]);
|
|
864
|
+
for (const [index, link] of table.links.entries()) {
|
|
865
|
+
if ((link.onDelete !== void 0 || link.onUpdate !== void 0) && (!link.enforce || link.kind !== "toOne")) {
|
|
866
|
+
ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "links", index], message: "referential actions require an enforced toOne link" });
|
|
867
|
+
}
|
|
868
|
+
if (link.onDelete === "set null" || link.onUpdate === "set null") {
|
|
869
|
+
const source = table.columns.find((column) => column.columnName === link.on);
|
|
870
|
+
if (source?.nullable === false || table.primaryKey?.includes(link.on)) ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "links", index], message: "SET NULL requires a nullable source column" });
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
for (const [index, check] of (table.checks ?? []).entries()) {
|
|
874
|
+
const fail = (message) => ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "checks", index], message });
|
|
875
|
+
if (table.source) fail("synced tables cannot declare checks");
|
|
876
|
+
if (names.has(check.name)) fail(`duplicate constraint '${check.name}'`);
|
|
877
|
+
names.add(check.name);
|
|
878
|
+
try {
|
|
879
|
+
compileCheckExpression(check.expression, table.columns);
|
|
880
|
+
} catch (error) {
|
|
881
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
882
|
+
}
|
|
883
|
+
}
|
|
296
884
|
for (const [index, fk] of (table.foreignKeys ?? []).entries()) {
|
|
297
885
|
const fail = (message) => ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "foreignKeys", index], message });
|
|
298
886
|
if (names.has(fk.name)) fail(`duplicate foreign key '${fk.name}'`);
|
|
@@ -310,10 +898,18 @@ zod.z.object({
|
|
|
310
898
|
if (!sourceColumn || !targetColumn) fail("foreign key references an unknown column");
|
|
311
899
|
else if (JSON.stringify(sourceColumn.type) !== JSON.stringify(targetColumn.type)) fail("foreign key column types must match");
|
|
312
900
|
}
|
|
901
|
+
if (fk.onDelete === "set null" || fk.onUpdate === "set null") {
|
|
902
|
+
if (fk.columns.some((name) => table.columns.find((column) => column.columnName === name)?.nullable === false || table.primaryKey?.includes(name))) fail("SET NULL requires nullable source columns");
|
|
903
|
+
}
|
|
313
904
|
const uniqueKeys = [target.primaryKey ?? [], ...target.indexes.filter((index2) => index2.unique).map((index2) => index2.columns), ...target.columns.filter((column) => column.unique).map((column) => [column.columnName])];
|
|
314
905
|
if (!uniqueKeys.some((key) => key.length === fk.references.length && key.every((column) => fk.references.includes(column)))) fail("foreign key target columns must form a unique key");
|
|
315
906
|
}
|
|
316
907
|
}
|
|
908
|
+
for (const [viewIndex, view] of (definition.views ?? []).entries()) {
|
|
909
|
+
for (const message of validateResolvedView({ view, tables: definition.tables, views: definition.views ?? [] })) {
|
|
910
|
+
ctx.addIssue({ code: "custom", path: ["views", viewIndex, "resolved"], message });
|
|
911
|
+
}
|
|
912
|
+
}
|
|
317
913
|
const tableByName = new Map(definition.tables.map((table) => [table.table, table]));
|
|
318
914
|
for (const [tableIndex, table] of definition.tables.entries()) {
|
|
319
915
|
const cowMutations = table.cowMutations ?? [];
|
|
@@ -506,6 +1102,21 @@ var selectionIrSchema = zod.z.lazy(
|
|
|
506
1102
|
() => zod.z.object({
|
|
507
1103
|
typename: zod.z.string(),
|
|
508
1104
|
columns: zod.z.array(zod.z.object({ alias: zod.z.string(), column: zod.z.string() })),
|
|
1105
|
+
cells: zod.z.object({
|
|
1106
|
+
alias: zod.z.string(),
|
|
1107
|
+
typename: zod.z.string(),
|
|
1108
|
+
fields: zod.z.array(zod.z.object({
|
|
1109
|
+
alias: zod.z.string(),
|
|
1110
|
+
column: zod.z.string(),
|
|
1111
|
+
typename: zod.z.string(),
|
|
1112
|
+
parts: zod.z.array(zod.z.object({
|
|
1113
|
+
alias: zod.z.string(),
|
|
1114
|
+
part: zod.z.enum(["source", "at", "by", "conflicts"]),
|
|
1115
|
+
fields: zod.z.array(zod.z.object({ alias: zod.z.string(), field: zod.z.enum(["source", "at", "value"]) })).optional(),
|
|
1116
|
+
typename: zod.z.string().optional()
|
|
1117
|
+
}))
|
|
1118
|
+
}))
|
|
1119
|
+
}).optional(),
|
|
509
1120
|
relations: zod.z.array(zod.z.object({
|
|
510
1121
|
alias: zod.z.string(),
|
|
511
1122
|
link: zod.z.string(),
|
|
@@ -527,6 +1138,11 @@ var selectionIrSchema = zod.z.lazy(
|
|
|
527
1138
|
})
|
|
528
1139
|
);
|
|
529
1140
|
var includeIfSchema = zod.z.object({ variable: zod.z.string(), negated: zod.z.boolean() });
|
|
1141
|
+
var prefetchPlanIrSchema = zod.z.object({
|
|
1142
|
+
source: zod.z.string(),
|
|
1143
|
+
maxStalenessMs: zod.z.number().int().positive().optional(),
|
|
1144
|
+
key: zod.z.union([zod.z.object({ one: valueIrSchema }), zod.z.object({ many: valueIrSchema })])
|
|
1145
|
+
});
|
|
530
1146
|
var selectRootSchema = zod.z.object({
|
|
531
1147
|
kind: zod.z.literal("select"),
|
|
532
1148
|
alias: zod.z.string(),
|
|
@@ -538,7 +1154,8 @@ var selectRootSchema = zod.z.object({
|
|
|
538
1154
|
distinctOn: zod.z.union([zod.z.array(zod.z.string()), zod.z.object({ slot: zod.z.string() })]).optional(),
|
|
539
1155
|
limit: limitIrSchema,
|
|
540
1156
|
offset: zod.z.object({ variable: zod.z.string().optional(), value: zod.z.number().int().optional() }).optional(),
|
|
541
|
-
includeIf: includeIfSchema.optional()
|
|
1157
|
+
includeIf: includeIfSchema.optional(),
|
|
1158
|
+
prefetch: zod.z.array(prefetchPlanIrSchema).optional()
|
|
542
1159
|
});
|
|
543
1160
|
var byPkRootSchema = zod.z.object({
|
|
544
1161
|
kind: zod.z.literal("byPk"),
|
|
@@ -548,7 +1165,8 @@ var byPkRootSchema = zod.z.object({
|
|
|
548
1165
|
pk: zod.z.array(zod.z.object({ column: zod.z.string(), value: valueIrSchema })).min(1),
|
|
549
1166
|
/** Policy predicate — ANDed with the PK match. */
|
|
550
1167
|
where: predIrSchema,
|
|
551
|
-
includeIf: includeIfSchema.optional()
|
|
1168
|
+
includeIf: includeIfSchema.optional(),
|
|
1169
|
+
prefetch: zod.z.array(prefetchPlanIrSchema).optional()
|
|
552
1170
|
});
|
|
553
1171
|
var aggregateRootSchema = zod.z.object({
|
|
554
1172
|
kind: zod.z.literal("aggregate"),
|
|
@@ -606,7 +1224,20 @@ var mutationRootSchema = zod.z.object({
|
|
|
606
1224
|
* coalesce view falls back to synced. Both are gated on a matching synced
|
|
607
1225
|
* row and carry the write policy in `where` like update/delete.
|
|
608
1226
|
*/
|
|
609
|
-
op: zod.z.enum(["insert", "insertOne", "update", "updateByPk", "delete", "deleteByPk", "cowOverride", "cowRevert"]),
|
|
1227
|
+
op: zod.z.enum(["insert", "insertOne", "update", "updateByPk", "delete", "deleteByPk", "cowOverride", "cowRevert", "resolvedWrite"]),
|
|
1228
|
+
/**
|
|
1229
|
+
* resolvedWrite: a write to a resolved table (`table` is its view). The
|
|
1230
|
+
* executor gates on the resolved row (write policy in `where`), writes only
|
|
1231
|
+
* the touched cells into the manual source, then re-checks `insertCheck`
|
|
1232
|
+
* against the resolved row. `revert` clears `revertColumns` from the manual
|
|
1233
|
+
* source; `delete` tombstones the row there.
|
|
1234
|
+
*/
|
|
1235
|
+
resolvedAction: zod.z.enum(["insert", "update", "delete", "revert"]).optional(),
|
|
1236
|
+
revertColumns: zod.z.array(zod.z.string()).optional(),
|
|
1237
|
+
/** `@writeback(source:, mode:)` directives on this root. */
|
|
1238
|
+
writeback: zod.z.array(zod.z.object({ source: zod.z.string(), mode: zod.z.enum(["sync", "async"]) })).optional(),
|
|
1239
|
+
/** `@localOnly(source:)` — writeback columns deliberately kept local for these sources. */
|
|
1240
|
+
localOnly: zod.z.array(zod.z.string()).optional(),
|
|
610
1241
|
alias: zod.z.string(),
|
|
611
1242
|
table: zod.z.string(),
|
|
612
1243
|
/** CoW ops: logical name of the synced twin (read through its policy-filtered view). */
|
|
@@ -691,6 +1322,43 @@ var compiledOperationSchema = zod.z.object({
|
|
|
691
1322
|
}
|
|
692
1323
|
}
|
|
693
1324
|
});
|
|
1325
|
+
var resolvedSourceIrSchema = zod.z.object({
|
|
1326
|
+
name: zod.z.string(),
|
|
1327
|
+
kind: zod.z.enum(["manual", "etl", "prefetch"]),
|
|
1328
|
+
/** Logical table key of the backing relation. */
|
|
1329
|
+
table: zod.z.string(),
|
|
1330
|
+
columns: zod.z.array(zod.z.string()),
|
|
1331
|
+
key: zod.z.string().optional(),
|
|
1332
|
+
fetch: zod.z.union([
|
|
1333
|
+
zod.z.object({ shape: zod.z.literal("single") }),
|
|
1334
|
+
zod.z.object({ shape: zod.z.literal("batch"), maxKeys: zod.z.number().int().positive() })
|
|
1335
|
+
]).optional(),
|
|
1336
|
+
function: zod.z.object({ kind: zod.z.enum(["managed", "native"]), id: zod.z.string() }).optional(),
|
|
1337
|
+
prefetchDefaults: zod.z.object({
|
|
1338
|
+
maxStalenessMs: zod.z.number().int().positive(),
|
|
1339
|
+
timeoutMs: zod.z.number().int().positive(),
|
|
1340
|
+
onError: zod.z.enum(["fallback", "fail"])
|
|
1341
|
+
}).optional(),
|
|
1342
|
+
writeback: zod.z.object({
|
|
1343
|
+
columns: zod.z.array(zod.z.string()),
|
|
1344
|
+
defaultMode: zod.z.enum(["sync", "async"]),
|
|
1345
|
+
timeoutMs: zod.z.number().int().positive(),
|
|
1346
|
+
clearManualOnConfirm: zod.z.boolean()
|
|
1347
|
+
}).optional()
|
|
1348
|
+
});
|
|
1349
|
+
var resolvedMetaIrSchema = zod.z.object({
|
|
1350
|
+
/** Logical pk column of the resolved view (single-column). */
|
|
1351
|
+
pk: zod.z.string(),
|
|
1352
|
+
/** Source name of the manual source. */
|
|
1353
|
+
manual: zod.z.string(),
|
|
1354
|
+
sources: zod.z.array(resolvedSourceIrSchema),
|
|
1355
|
+
deletedBy: zod.z.array(zod.z.string()),
|
|
1356
|
+
/**
|
|
1357
|
+
* Materialized: the view reads `table`, which the data plane refreshes from
|
|
1358
|
+
* the `compute` view (claims-gated) whenever it writes a source row.
|
|
1359
|
+
*/
|
|
1360
|
+
materialized: zod.z.object({ table: zod.z.string(), compute: zod.z.string() }).optional()
|
|
1361
|
+
});
|
|
694
1362
|
var tableMetaIrSchema = zod.z.object({
|
|
695
1363
|
table: zod.z.string(),
|
|
696
1364
|
schema: zod.z.string(),
|
|
@@ -731,6 +1399,22 @@ var tableMetaIrSchema = zod.z.object({
|
|
|
731
1399
|
* `onUpdateFn`). Omitted when empty so pre-onUpdate artifacts still parse.
|
|
732
1400
|
*/
|
|
733
1401
|
touchOnUpdate: zod.z.array(zod.z.string()).optional(),
|
|
1402
|
+
/**
|
|
1403
|
+
* Per-cell metadata: logical column → the logical names of its cell-part
|
|
1404
|
+
* columns (which are listed in `columns`, but never filterable, orderable or
|
|
1405
|
+
* writable). Tables stamp `at`/`by` (and a manual source `rev`) on every
|
|
1406
|
+
* write; resolved views expose `source`/`at`/`by`/`conflicts`. Omitted when
|
|
1407
|
+
* the relation tracks no cells.
|
|
1408
|
+
*/
|
|
1409
|
+
cells: zod.z.record(zod.z.string(), zod.z.object({
|
|
1410
|
+
at: zod.z.string().optional(),
|
|
1411
|
+
by: zod.z.string().optional(),
|
|
1412
|
+
rev: zod.z.string().optional(),
|
|
1413
|
+
source: zod.z.string().optional(),
|
|
1414
|
+
conflicts: zod.z.string().optional()
|
|
1415
|
+
}).strict()).optional(),
|
|
1416
|
+
/** Resolved-table view metadata (sources, prefetch/writeback config) for the data plane. */
|
|
1417
|
+
resolved: resolvedMetaIrSchema.optional(),
|
|
734
1418
|
constraints: zod.z.array(zod.z.object({ name: zod.z.string(), columns: zod.z.array(zod.z.string()) })),
|
|
735
1419
|
/** Depth/node caps for dynamic filter trees on this table. */
|
|
736
1420
|
dynamicFilterMaxDepth: zod.z.number().int().positive().default(6),
|
|
@@ -759,7 +1443,7 @@ var IDENTIFIER_CHAR = /[a-zA-Z0-9_]/;
|
|
|
759
1443
|
var NUMBER_PATTERN = /^-?\d+(\.\d+)?/;
|
|
760
1444
|
var SCALAR_CLAIM_KEYS = Object.entries(CLAIMS_CONTRACT).filter(([, type]) => !type.endsWith("[]")).map(([key]) => key);
|
|
761
1445
|
var ARRAY_CLAIM_KEYS = Object.entries(CLAIMS_CONTRACT).filter(([, type]) => type.endsWith("[]")).map(([key]) => key);
|
|
762
|
-
function
|
|
1446
|
+
function tokenize2(source) {
|
|
763
1447
|
const tokens = [];
|
|
764
1448
|
let position = 0;
|
|
765
1449
|
while (position < source.length) {
|
|
@@ -838,7 +1522,7 @@ function readString(source, start) {
|
|
|
838
1522
|
}
|
|
839
1523
|
throw new PolicyParseError("unterminated string literal", start);
|
|
840
1524
|
}
|
|
841
|
-
var
|
|
1525
|
+
var Parser2 = class {
|
|
842
1526
|
constructor(tokens) {
|
|
843
1527
|
this.tokens = tokens;
|
|
844
1528
|
}
|
|
@@ -1012,7 +1696,7 @@ var Parser = class {
|
|
|
1012
1696
|
function parsePolicy(source) {
|
|
1013
1697
|
const trimmed = source.trim();
|
|
1014
1698
|
if (trimmed.length === 0) throw new PolicyParseError("empty policy expression", 0);
|
|
1015
|
-
return { source: trimmed, ast: new
|
|
1699
|
+
return { source: trimmed, ast: new Parser2(tokenize2(trimmed)).parse() };
|
|
1016
1700
|
}
|
|
1017
1701
|
|
|
1018
1702
|
// src/predicate-v2/compile.ts
|