@sequenceholdings/orm 2.10.0 → 2.11.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 +74 -4
- package/dist/authoring.cjs +796 -7
- package/dist/authoring.cjs.map +1 -1
- package/dist/authoring.d.cts +10 -1
- package/dist/authoring.d.ts +10 -1
- package/dist/authoring.js +796 -7
- package/dist/authoring.js.map +1 -1
- package/dist/canonical.cjs +777 -6
- package/dist/canonical.cjs.map +1 -1
- package/dist/canonical.js +777 -6
- package/dist/canonical.js.map +1 -1
- package/dist/cli.cjs +7697 -7686
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +7698 -7687
- package/dist/cli.js.map +1 -1
- package/dist/client.cjs +5 -10
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -13
- package/dist/client.d.ts +2 -13
- package/dist/client.js +6 -10
- package/dist/client.js.map +1 -1
- package/dist/compiler.cjs +815 -100
- package/dist/compiler.cjs.map +1 -1
- package/dist/compiler.d.cts +5 -16
- package/dist/compiler.d.ts +5 -16
- package/dist/compiler.js +817 -99
- package/dist/compiler.js.map +1 -1
- package/dist/index.cjs +3611 -3287
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +577 -7
- package/dist/index.d.ts +577 -7
- package/dist/index.js +3600 -3285
- package/dist/index.js.map +1 -1
- package/dist/ir.cjs +784 -18
- package/dist/ir.cjs.map +1 -1
- package/dist/ir.d.cts +3 -6
- package/dist/ir.d.ts +3 -6
- package/dist/ir.js +784 -18
- package/dist/ir.js.map +1 -1
- package/dist/predicate-v2.cjs +796 -31
- package/dist/predicate-v2.cjs.map +1 -1
- package/dist/predicate-v2.js +796 -31
- package/dist/predicate-v2.js.map +1 -1
- package/dist/sdl.cjs +790 -28
- package/dist/sdl.cjs.map +1 -1
- package/dist/sdl.d.cts +1 -5
- package/dist/sdl.d.ts +1 -5
- package/dist/sdl.js +790 -28
- package/dist/sdl.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen-gql/server-fns.cjs +1 -1
package/dist/ir.cjs
CHANGED
|
@@ -204,10 +204,16 @@ function compileCheckExpression(source, columns) {
|
|
|
204
204
|
|
|
205
205
|
// src/constants.ts
|
|
206
206
|
var NAMESPACE_PATTERN = /^(?!(?:events|native_sources)$)[a-z][a-z0-9_]{0,40}$/;
|
|
207
|
+
var IDENTIFIER_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
207
208
|
var COLUMN_IDENTIFIER_PATTERN = /^_?[a-z][a-z0-9_]*$/;
|
|
208
209
|
var MAX_IDENTIFIER_LENGTH = 63;
|
|
210
|
+
var CAPABILITY_KEY_PATTERN = /^(?:org\/|app\/[a-z][a-z0-9-]{0,63}\/)?[a-z0-9-]{1,64}$/;
|
|
209
211
|
var MAX_PERSISTED_READ_ROWS = 50001;
|
|
212
|
+
var CLAIM_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
210
213
|
var QUALIFIED_TABLE_PATTERN = /^[a-z][a-z0-9_]{0,62}\.[a-z][a-z0-9_]{0,62}$/;
|
|
214
|
+
var SYNCED_SOURCE_PATTERN = QUALIFIED_TABLE_PATTERN;
|
|
215
|
+
var RESERVED_TABLE_NAMES = /* @__PURE__ */ new Set(["actions", "query"]);
|
|
216
|
+
var RESERVED_COLUMN_NAMES = /* @__PURE__ */ new Set(["select", "order", "limit", "offset"]);
|
|
211
217
|
var CELL_PARTS = ["at", "by", "rev", "source", "conflicts"];
|
|
212
218
|
var CELL_PART_SUFFIX = {
|
|
213
219
|
at: "__at",
|
|
@@ -223,6 +229,741 @@ function cellColumnName(column, part) {
|
|
|
223
229
|
}
|
|
224
230
|
var MATERIALIZE_ACTOR = "orm:materialize";
|
|
225
231
|
JSON.stringify({ sub: MATERIALIZE_ACTOR, org: null, roles: [], capabilities: [] });
|
|
232
|
+
|
|
233
|
+
// src/predicate/ast.ts
|
|
234
|
+
var COMPARE_OPS = ["=", "!=", "<", "<=", ">", ">="];
|
|
235
|
+
function walkPredicate(predicate, visit) {
|
|
236
|
+
visit(predicate);
|
|
237
|
+
if (predicate.kind === "and" || predicate.kind === "or") {
|
|
238
|
+
for (const operand of predicate.operands) walkPredicate(operand, visit);
|
|
239
|
+
}
|
|
240
|
+
if (predicate.kind === "not") walkPredicate(predicate.operand, visit);
|
|
241
|
+
}
|
|
242
|
+
function predicateOperands(predicate) {
|
|
243
|
+
const operands = [];
|
|
244
|
+
walkPredicate(predicate, (node) => {
|
|
245
|
+
if (node.kind === "compare") operands.push(node.left, node.right);
|
|
246
|
+
});
|
|
247
|
+
return operands;
|
|
248
|
+
}
|
|
249
|
+
function referencedColumns(predicate) {
|
|
250
|
+
const names = predicateOperands(predicate).flatMap(
|
|
251
|
+
(operand) => operand.kind === "column" ? [operand.name] : []
|
|
252
|
+
);
|
|
253
|
+
return [...new Set(names)];
|
|
254
|
+
}
|
|
255
|
+
var identifierSchema = zod.z.string().max(MAX_IDENTIFIER_LENGTH).regex(IDENTIFIER_PATTERN, "must be a lowercase identifier (a-z, 0-9, _)");
|
|
256
|
+
var namespaceSchema = zod.z.string().regex(NAMESPACE_PATTERN, "must be lowercase, start with a letter, and be at most 41 characters");
|
|
257
|
+
var tableReferenceSchema = zod.z.string().refine(
|
|
258
|
+
(value) => IDENTIFIER_PATTERN.test(value) || QUALIFIED_TABLE_PATTERN.test(value),
|
|
259
|
+
"must be a table name or a schema-qualified table reference"
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// src/schema/columns.ts
|
|
263
|
+
var COLUMN_TYPES = [
|
|
264
|
+
"uuid",
|
|
265
|
+
"text",
|
|
266
|
+
"integer",
|
|
267
|
+
"bigint",
|
|
268
|
+
"numeric",
|
|
269
|
+
"double",
|
|
270
|
+
"boolean",
|
|
271
|
+
"timestamptz",
|
|
272
|
+
"date",
|
|
273
|
+
"jsonb"
|
|
274
|
+
];
|
|
275
|
+
var columnTypeSchema = zod.z.enum(COLUMN_TYPES);
|
|
276
|
+
var FUNCTION_DEFAULTS = {
|
|
277
|
+
"gen_random_uuid()": ["uuid"],
|
|
278
|
+
"now()": ["timestamptz", "date"]
|
|
279
|
+
};
|
|
280
|
+
var CAST_PATTERN = /^([a-z][a-z0-9_]*)::([a-z]+)$/;
|
|
281
|
+
function defaultIssue(value, type) {
|
|
282
|
+
if (typeof value === "boolean") return type === "boolean" ? null : `boolean default is invalid for type '${type}'`;
|
|
283
|
+
if (typeof value === "number") {
|
|
284
|
+
if (!["integer", "bigint", "numeric", "double"].includes(type)) {
|
|
285
|
+
return `numeric default is invalid for type '${type}'`;
|
|
286
|
+
}
|
|
287
|
+
if (["integer", "bigint"].includes(type)) {
|
|
288
|
+
if (!Number.isInteger(value)) return `default for type '${type}' must be an integer`;
|
|
289
|
+
if (!Number.isSafeInteger(value)) {
|
|
290
|
+
return `default for type '${type}' exceeds the safe integer range \u2014 YAML numbers lose precision past 2^53`;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (type === "numeric" && Math.abs(value) > Number.MAX_SAFE_INTEGER) {
|
|
294
|
+
return "numeric default exceeds the safe float range \u2014 write it as a string (e.g. '9007199254740993') to keep exact precision";
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
const allowedTypes = FUNCTION_DEFAULTS[value];
|
|
299
|
+
if (allowedTypes) return allowedTypes.includes(type) ? null : `default '${value}' is invalid for type '${type}'`;
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
var columnSchema = zod.z.strictObject({
|
|
303
|
+
type: columnTypeSchema,
|
|
304
|
+
primary_key: zod.z.boolean().optional(),
|
|
305
|
+
nullable: zod.z.boolean().optional(),
|
|
306
|
+
unique: zod.z.boolean().optional(),
|
|
307
|
+
default: zod.z.union([zod.z.string(), zod.z.number(), zod.z.boolean()]).optional(),
|
|
308
|
+
description: zod.z.string().optional(),
|
|
309
|
+
renamed_from: identifierSchema.optional(),
|
|
310
|
+
cast: zod.z.string().optional(),
|
|
311
|
+
deprecated: zod.z.boolean().optional()
|
|
312
|
+
}).superRefine((column, ctx) => {
|
|
313
|
+
if (column.default !== void 0) {
|
|
314
|
+
const issue = defaultIssue(column.default, column.type);
|
|
315
|
+
if (issue) ctx.addIssue({ code: "custom", message: issue, path: ["default"] });
|
|
316
|
+
}
|
|
317
|
+
if (column.primary_key && column.nullable) {
|
|
318
|
+
ctx.addIssue({ code: "custom", message: "a primary key column cannot be nullable", path: ["nullable"] });
|
|
319
|
+
}
|
|
320
|
+
if (column.primary_key && column.deprecated) {
|
|
321
|
+
ctx.addIssue({ code: "custom", message: "a primary key column cannot be deprecated", path: ["deprecated"] });
|
|
322
|
+
}
|
|
323
|
+
if (column.cast && !CAST_PATTERN.test(column.cast)) {
|
|
324
|
+
ctx.addIssue({ code: "custom", message: "cast must have the shape '<column>::<type>'", path: ["cast"] });
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
function castParts(cast) {
|
|
328
|
+
const match = CAST_PATTERN.exec(cast);
|
|
329
|
+
if (!match) return null;
|
|
330
|
+
return { column: match[1], type: match[2] };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/errors.ts
|
|
334
|
+
var PredicateParseError = class extends Error {
|
|
335
|
+
constructor(message, position) {
|
|
336
|
+
super(`${message} (at position ${position})`);
|
|
337
|
+
this.position = position;
|
|
338
|
+
}
|
|
339
|
+
name = "PredicateParseError";
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
// src/predicate/parse.ts
|
|
343
|
+
var KEYWORDS = /* @__PURE__ */ new Set(["and", "or", "not", "true", "false", "null", "claim", "capability"]);
|
|
344
|
+
var OPERATOR_PATTERN = /^(!=|<=|>=|<>|=|<|>)/;
|
|
345
|
+
var IDENTIFIER_START = /[a-z_]/;
|
|
346
|
+
var IDENTIFIER_CHAR = /[a-z0-9_.-]/;
|
|
347
|
+
var NUMBER_PATTERN = /^-?\d+(\.\d+)?/;
|
|
348
|
+
function tokenize2(source) {
|
|
349
|
+
const tokens = [];
|
|
350
|
+
let position = 0;
|
|
351
|
+
while (position < source.length) {
|
|
352
|
+
const char = source[position];
|
|
353
|
+
if (/\s/.test(char)) {
|
|
354
|
+
position += 1;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (char === "(") {
|
|
358
|
+
tokens.push({ kind: "lparen", value: char, position });
|
|
359
|
+
position += 1;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (char === ")") {
|
|
363
|
+
tokens.push({ kind: "rparen", value: char, position });
|
|
364
|
+
position += 1;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (char === ",") {
|
|
368
|
+
tokens.push({ kind: "comma", value: char, position });
|
|
369
|
+
position += 1;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (char === "'") {
|
|
373
|
+
const { value, end } = readString(source, position);
|
|
374
|
+
tokens.push({ kind: "string", value, position });
|
|
375
|
+
position = end;
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
const operator = OPERATOR_PATTERN.exec(source.slice(position));
|
|
379
|
+
if (operator) {
|
|
380
|
+
const value = operator[0] === "<>" ? "!=" : operator[0];
|
|
381
|
+
tokens.push({ kind: "operator", value, position });
|
|
382
|
+
position += operator[0].length;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const number = NUMBER_PATTERN.exec(source.slice(position));
|
|
386
|
+
if (number && !IDENTIFIER_START.test(char)) {
|
|
387
|
+
tokens.push({ kind: "number", value: number[0], position });
|
|
388
|
+
position += number[0].length;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (IDENTIFIER_START.test(char)) {
|
|
392
|
+
let end = position;
|
|
393
|
+
while (end < source.length && IDENTIFIER_CHAR.test(source[end])) end += 1;
|
|
394
|
+
tokens.push({ kind: "identifier", value: source.slice(position, end), position });
|
|
395
|
+
position = end;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
throw new PredicateParseError(`unexpected character '${char}'`, position);
|
|
399
|
+
}
|
|
400
|
+
tokens.push({ kind: "end", value: "", position: source.length });
|
|
401
|
+
return tokens;
|
|
402
|
+
}
|
|
403
|
+
function readString(source, start) {
|
|
404
|
+
let value = "";
|
|
405
|
+
let position = start + 1;
|
|
406
|
+
while (position < source.length) {
|
|
407
|
+
const char = source[position];
|
|
408
|
+
if (char !== "'") {
|
|
409
|
+
value += char;
|
|
410
|
+
position += 1;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (source[position + 1] === "'") {
|
|
414
|
+
value += "'";
|
|
415
|
+
position += 2;
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
return { value, end: position + 1 };
|
|
419
|
+
}
|
|
420
|
+
throw new PredicateParseError("unterminated string literal", start);
|
|
421
|
+
}
|
|
422
|
+
var Parser2 = class {
|
|
423
|
+
constructor(tokens) {
|
|
424
|
+
this.tokens = tokens;
|
|
425
|
+
}
|
|
426
|
+
index = 0;
|
|
427
|
+
parse() {
|
|
428
|
+
const predicate = this.parseOr();
|
|
429
|
+
this.expect("end", "end of expression");
|
|
430
|
+
return predicate;
|
|
431
|
+
}
|
|
432
|
+
parseOr() {
|
|
433
|
+
const operands = [this.parseAnd()];
|
|
434
|
+
while (this.matchKeyword("or")) operands.push(this.parseAnd());
|
|
435
|
+
return operands.length === 1 ? operands[0] : { kind: "or", operands };
|
|
436
|
+
}
|
|
437
|
+
parseAnd() {
|
|
438
|
+
const operands = [this.parseNot()];
|
|
439
|
+
while (this.matchKeyword("and")) operands.push(this.parseNot());
|
|
440
|
+
return operands.length === 1 ? operands[0] : { kind: "and", operands };
|
|
441
|
+
}
|
|
442
|
+
parseNot() {
|
|
443
|
+
if (this.matchKeyword("not")) return { kind: "not", operand: this.parseNot() };
|
|
444
|
+
return this.parsePrimary();
|
|
445
|
+
}
|
|
446
|
+
parsePrimary() {
|
|
447
|
+
if (this.peek().kind === "lparen") {
|
|
448
|
+
this.advance();
|
|
449
|
+
const inner = this.parseOr();
|
|
450
|
+
this.expect("rparen", "')'");
|
|
451
|
+
return inner;
|
|
452
|
+
}
|
|
453
|
+
if (this.peekKeyword("capability")) {
|
|
454
|
+
const capability = this.parseCapability();
|
|
455
|
+
const next = this.peek();
|
|
456
|
+
if (next.kind === "operator") {
|
|
457
|
+
throw new PredicateParseError("capability() is boolean-valued and cannot appear in a comparison", next.position);
|
|
458
|
+
}
|
|
459
|
+
return capability;
|
|
460
|
+
}
|
|
461
|
+
return this.parseComparison();
|
|
462
|
+
}
|
|
463
|
+
parseCapability() {
|
|
464
|
+
this.advance();
|
|
465
|
+
const key = this.parseCallArgument("capability");
|
|
466
|
+
if (!CAPABILITY_KEY_PATTERN.test(key)) {
|
|
467
|
+
throw new PredicateParseError(`invalid capability key '${key}'`, this.peek().position);
|
|
468
|
+
}
|
|
469
|
+
return { kind: "capability", key };
|
|
470
|
+
}
|
|
471
|
+
parseComparison() {
|
|
472
|
+
const left = this.parseOperand();
|
|
473
|
+
const operator = this.expect("operator", "a comparison operator");
|
|
474
|
+
if (!COMPARE_OPS.includes(operator.value)) {
|
|
475
|
+
throw new PredicateParseError(`unsupported operator '${operator.value}'`, operator.position);
|
|
476
|
+
}
|
|
477
|
+
const right = this.parseOperand();
|
|
478
|
+
return { kind: "compare", op: operator.value, left, right };
|
|
479
|
+
}
|
|
480
|
+
parseOperand() {
|
|
481
|
+
const token = this.peek();
|
|
482
|
+
if (token.kind === "string") {
|
|
483
|
+
this.advance();
|
|
484
|
+
return { kind: "literal", value: token.value };
|
|
485
|
+
}
|
|
486
|
+
if (token.kind === "number") {
|
|
487
|
+
this.advance();
|
|
488
|
+
const value = Number(token.value);
|
|
489
|
+
if (!Number.isFinite(value) || Number.isInteger(value) && !Number.isSafeInteger(value)) {
|
|
490
|
+
throw new PredicateParseError(
|
|
491
|
+
`numeric literal '${token.value}' exceeds safe integer precision \u2014 quote it as a string`,
|
|
492
|
+
token.position
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
return { kind: "literal", value };
|
|
496
|
+
}
|
|
497
|
+
if (token.kind !== "identifier") {
|
|
498
|
+
throw new PredicateParseError(`expected an operand, got '${token.value || "end"}'`, token.position);
|
|
499
|
+
}
|
|
500
|
+
this.advance();
|
|
501
|
+
if (token.value === "true" || token.value === "false") {
|
|
502
|
+
return { kind: "literal", value: token.value === "true" };
|
|
503
|
+
}
|
|
504
|
+
if (token.value === "null") return { kind: "literal", value: null };
|
|
505
|
+
if (token.value === "claim") {
|
|
506
|
+
const key = this.parseCallArgument("claim");
|
|
507
|
+
if (!CLAIM_KEY_PATTERN.test(key)) {
|
|
508
|
+
throw new PredicateParseError(`invalid claim key '${key}'`, token.position);
|
|
509
|
+
}
|
|
510
|
+
return { kind: "claim", key };
|
|
511
|
+
}
|
|
512
|
+
if (token.value === "capability") {
|
|
513
|
+
throw new PredicateParseError("capability() is boolean-valued and cannot appear in a comparison", token.position);
|
|
514
|
+
}
|
|
515
|
+
if (KEYWORDS.has(token.value) || !IDENTIFIER_PATTERN.test(token.value)) {
|
|
516
|
+
throw new PredicateParseError(`invalid column reference '${token.value}'`, token.position);
|
|
517
|
+
}
|
|
518
|
+
return { kind: "column", name: token.value };
|
|
519
|
+
}
|
|
520
|
+
parseCallArgument(fn) {
|
|
521
|
+
this.expect("lparen", `'(' after ${fn}`);
|
|
522
|
+
const argument = this.expect("string", `a quoted key in ${fn}(...)`);
|
|
523
|
+
this.expect("rparen", `')' after ${fn} key`);
|
|
524
|
+
return argument.value;
|
|
525
|
+
}
|
|
526
|
+
peek() {
|
|
527
|
+
return this.tokens[this.index];
|
|
528
|
+
}
|
|
529
|
+
peekKeyword(keyword) {
|
|
530
|
+
const token = this.peek();
|
|
531
|
+
return token.kind === "identifier" && token.value === keyword;
|
|
532
|
+
}
|
|
533
|
+
matchKeyword(keyword) {
|
|
534
|
+
if (!this.peekKeyword(keyword)) return false;
|
|
535
|
+
this.advance();
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
advance() {
|
|
539
|
+
const token = this.peek();
|
|
540
|
+
if (token.kind !== "end") this.index += 1;
|
|
541
|
+
return token;
|
|
542
|
+
}
|
|
543
|
+
expect(kind, description) {
|
|
544
|
+
const token = this.peek();
|
|
545
|
+
if (token.kind !== kind) {
|
|
546
|
+
throw new PredicateParseError(`expected ${description}, got '${token.value || "end"}'`, token.position);
|
|
547
|
+
}
|
|
548
|
+
return this.advance();
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
function parsePredicate(source) {
|
|
552
|
+
const trimmed = source.trim();
|
|
553
|
+
if (trimmed.length === 0) throw new PredicateParseError("empty predicate", 0);
|
|
554
|
+
return { source: trimmed, ast: new Parser2(tokenize2(trimmed)).parse() };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/schema/predicates.ts
|
|
558
|
+
var parsedPredicateSchema = zod.z.string().transform((source, ctx) => {
|
|
559
|
+
try {
|
|
560
|
+
return parsePredicate(source);
|
|
561
|
+
} catch (error) {
|
|
562
|
+
ctx.addIssue({ code: "custom", message: error instanceof Error ? error.message : String(error) });
|
|
563
|
+
return zod.z.NEVER;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// src/schema/actions.ts
|
|
568
|
+
var EXECUTES_VERB_PATTERN = /^\s*(insert|update|delete)\b/i;
|
|
569
|
+
var DOLLAR_QUOTE_PATTERN = /\$[a-zA-Z0-9_]*\$/;
|
|
570
|
+
var RETURNING_PATTERN = /\breturning\b/i;
|
|
571
|
+
var ESCAPE_STRING_PATTERN = /(?<![a-zA-Z0-9_])[Ee]'/;
|
|
572
|
+
var MASKED_CLAIM_CALL_PATTERN = /\b(?:claim|capability)\(\s*''\s*\)/g;
|
|
573
|
+
var BARE_CLAIM_CALL_PATTERN = /\b(?:claim|capability)\s*\(/;
|
|
574
|
+
var EXISTS_ENTRY_PATTERN = /^exists\(\s*'([^']+)'\s*,\s*([a-z][a-z0-9_]*)\s*\)$/;
|
|
575
|
+
var NAMED_PARAM_PATTERN = /(?<!:):([a-z][a-z0-9_]*)/g;
|
|
576
|
+
var QUOTED_STRING_PATTERN = /'(?:[^']|'')*'/g;
|
|
577
|
+
var SQL_COMMENT_PATTERN = /--|\/\*/;
|
|
578
|
+
var actionParamSchema = zod.z.strictObject({
|
|
579
|
+
type: columnTypeSchema,
|
|
580
|
+
optional: zod.z.boolean().optional(),
|
|
581
|
+
description: zod.z.string().optional()
|
|
582
|
+
});
|
|
583
|
+
var validatesEntrySchema = zod.z.string().transform((source, ctx) => {
|
|
584
|
+
const match = EXISTS_ENTRY_PATTERN.exec(source.trim());
|
|
585
|
+
if (!match) {
|
|
586
|
+
ctx.addIssue({ code: "custom", message: "validates entries must have the shape exists('<table>', <param>)" });
|
|
587
|
+
return zod.z.NEVER;
|
|
588
|
+
}
|
|
589
|
+
const table = tableReferenceSchema.safeParse(match[1]);
|
|
590
|
+
if (!table.success) {
|
|
591
|
+
ctx.addIssue({ code: "custom", message: `invalid table reference '${match[1]}'` });
|
|
592
|
+
return zod.z.NEVER;
|
|
593
|
+
}
|
|
594
|
+
return { source: source.trim(), table: table.data, param: match[2] };
|
|
595
|
+
});
|
|
596
|
+
var AUDIT_ACTIONS = ["create", "update", "delete"];
|
|
597
|
+
var auditSchema = zod.z.strictObject({
|
|
598
|
+
action: zod.z.enum(AUDIT_ACTIONS),
|
|
599
|
+
resource: identifierSchema.optional()
|
|
600
|
+
});
|
|
601
|
+
var actionSchema = zod.z.strictObject({
|
|
602
|
+
description: zod.z.string().optional(),
|
|
603
|
+
requires: parsedPredicateSchema.optional(),
|
|
604
|
+
params: zod.z.record(identifierSchema, actionParamSchema).optional(),
|
|
605
|
+
validates: zod.z.array(validatesEntrySchema).optional(),
|
|
606
|
+
// One statement, or an ordered list run atomically in the action function.
|
|
607
|
+
executes: zod.z.union([zod.z.string().min(1), zod.z.array(zod.z.string().min(1)).min(1)]),
|
|
608
|
+
// Declares the columns the action's RETURNING yields. Present → the
|
|
609
|
+
// function returns those rows (typed in the client); absent → the action
|
|
610
|
+
// reports rows_affected only. The column names/types must match the
|
|
611
|
+
// RETURNING list, which Postgres enforces at CREATE FUNCTION.
|
|
612
|
+
returns: zod.z.record(identifierSchema, columnTypeSchema).optional(),
|
|
613
|
+
audit: auditSchema.optional()
|
|
614
|
+
}).superRefine((action, ctx) => {
|
|
615
|
+
const params = Object.keys(action.params ?? {});
|
|
616
|
+
if (action.requires && referencedColumns(action.requires.ast).length > 0) {
|
|
617
|
+
ctx.addIssue({
|
|
618
|
+
code: "custom",
|
|
619
|
+
message: "requires may reference claims and capabilities only, not columns",
|
|
620
|
+
path: ["requires"]
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
for (const issue of executesIssues(action.executes, new Set(params), action.returns !== void 0)) {
|
|
624
|
+
ctx.addIssue({ code: "custom", message: issue, path: ["executes"] });
|
|
625
|
+
}
|
|
626
|
+
if (action.returns && Object.keys(action.returns).length === 0) {
|
|
627
|
+
ctx.addIssue({ code: "custom", message: "returns must declare at least one column", path: ["returns"] });
|
|
628
|
+
}
|
|
629
|
+
for (const [index, entry] of (action.validates ?? []).entries()) {
|
|
630
|
+
if (!params.includes(entry.param)) {
|
|
631
|
+
ctx.addIssue({
|
|
632
|
+
code: "custom",
|
|
633
|
+
message: `validates references undeclared param '${entry.param}'`,
|
|
634
|
+
path: ["validates", index]
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
function extractNamedParams(sql) {
|
|
640
|
+
const withoutStrings = sql.replace(QUOTED_STRING_PATTERN, "''");
|
|
641
|
+
const names = [...withoutStrings.matchAll(NAMED_PARAM_PATTERN)].map((match) => match[1]);
|
|
642
|
+
return [...new Set(names)];
|
|
643
|
+
}
|
|
644
|
+
function actionStatementSources(executes) {
|
|
645
|
+
return (Array.isArray(executes) ? executes : [executes]).map((statement) => statement.trim());
|
|
646
|
+
}
|
|
647
|
+
function executesIssues(executes, declaredParams, hasReturns) {
|
|
648
|
+
const issues = [];
|
|
649
|
+
const statements = actionStatementSources(executes);
|
|
650
|
+
statements.forEach((statement, index) => {
|
|
651
|
+
const withoutStrings = statement.replace(QUOTED_STRING_PATTERN, "''");
|
|
652
|
+
const at = statements.length > 1 ? ` (statement ${index + 1})` : "";
|
|
653
|
+
if (!EXECUTES_VERB_PATTERN.test(statement)) issues.push(`executes must be INSERT, UPDATE, or DELETE${at}`);
|
|
654
|
+
if (withoutStrings.replace(/;\s*$/, "").includes(";")) issues.push(`each executes entry must be a single statement${at}`);
|
|
655
|
+
if (SQL_COMMENT_PATTERN.test(withoutStrings)) issues.push(`executes must not contain SQL comments${at}`);
|
|
656
|
+
if (DOLLAR_QUOTE_PATTERN.test(withoutStrings)) issues.push(`executes must not use dollar-quoted strings${at}`);
|
|
657
|
+
if (ESCAPE_STRING_PATTERN.test(withoutStrings)) issues.push(`executes must not use escape strings (E'\u2026')${at}`);
|
|
658
|
+
if (withoutStrings.includes('"')) issues.push(`executes must not use double-quoted identifiers${at}`);
|
|
659
|
+
if (BARE_CLAIM_CALL_PATTERN.test(withoutStrings.replace(MASKED_CLAIM_CALL_PATTERN, ""))) {
|
|
660
|
+
issues.push(`executes has a malformed claim()/capability() call \u2014 use e.g. claim('sub')${at}`);
|
|
661
|
+
}
|
|
662
|
+
if (RETURNING_PATTERN.test(withoutStrings) && !(hasReturns && index === statements.length - 1)) {
|
|
663
|
+
issues.push(
|
|
664
|
+
hasReturns ? `only the last executes statement may use RETURNING${at}` : `executes must not use RETURNING unless the action declares returns${at}`
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
const last = statements.at(-1);
|
|
669
|
+
if (hasReturns && last && !RETURNING_PATTERN.test(last.replace(QUOTED_STRING_PATTERN, "''"))) {
|
|
670
|
+
issues.push("the last executes statement must use RETURNING to populate the declared returns columns");
|
|
671
|
+
}
|
|
672
|
+
const undeclared = extractNamedParams(statements.join("\n")).filter((name) => !declaredParams.has(name));
|
|
673
|
+
if (undeclared.length > 0) issues.push(`executes references undeclared params: ${undeclared.join(", ")}`);
|
|
674
|
+
return issues;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// src/schema/table.ts
|
|
678
|
+
var linkSchema = zod.z.strictObject({
|
|
679
|
+
to: tableReferenceSchema,
|
|
680
|
+
on: identifierSchema,
|
|
681
|
+
enforce: zod.z.boolean().optional(),
|
|
682
|
+
description: zod.z.string().optional()
|
|
683
|
+
});
|
|
684
|
+
var indexSchema = zod.z.union([
|
|
685
|
+
zod.z.array(identifierSchema).min(1),
|
|
686
|
+
zod.z.strictObject({
|
|
687
|
+
columns: zod.z.array(identifierSchema).min(1),
|
|
688
|
+
unique: zod.z.boolean().optional(),
|
|
689
|
+
name: identifierSchema.optional()
|
|
690
|
+
})
|
|
691
|
+
]).transform((index) => Array.isArray(index) ? { columns: index } : index);
|
|
692
|
+
var policiesSchema = zod.z.strictObject({
|
|
693
|
+
read: parsedPredicateSchema.optional(),
|
|
694
|
+
write: parsedPredicateSchema.optional(),
|
|
695
|
+
insert: parsedPredicateSchema.optional(),
|
|
696
|
+
update: parsedPredicateSchema.optional()
|
|
697
|
+
});
|
|
698
|
+
zod.z.strictObject({
|
|
699
|
+
namespace: namespaceSchema,
|
|
700
|
+
table: identifierSchema,
|
|
701
|
+
description: zod.z.string().optional(),
|
|
702
|
+
columns: zod.z.record(identifierSchema, columnSchema).optional(),
|
|
703
|
+
source: zod.z.string().regex(SYNCED_SOURCE_PATTERN, "source must be a qualified lowercase schema.table identifier").optional(),
|
|
704
|
+
links: zod.z.record(identifierSchema, linkSchema).optional(),
|
|
705
|
+
indexes: zod.z.array(indexSchema).optional(),
|
|
706
|
+
policies: policiesSchema.optional(),
|
|
707
|
+
actions: zod.z.record(identifierSchema, actionSchema).optional()
|
|
708
|
+
}).superRefine((table, ctx) => {
|
|
709
|
+
if (RESERVED_TABLE_NAMES.has(table.table)) {
|
|
710
|
+
ctx.addIssue({ code: "custom", message: `'${table.table}' is a reserved table name`, path: ["table"] });
|
|
711
|
+
}
|
|
712
|
+
for (const name of Object.keys(table.columns ?? {})) {
|
|
713
|
+
if (RESERVED_COLUMN_NAMES.has(name)) {
|
|
714
|
+
ctx.addIssue({
|
|
715
|
+
code: "custom",
|
|
716
|
+
message: `column '${name}' collides with the Data API's reserved query parameters (select, order, limit, offset) \u2014 it could never be filtered`,
|
|
717
|
+
path: ["columns", name]
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (table.source) validateSyncedTable(table, ctx);
|
|
722
|
+
else validateColumnTable(table, ctx);
|
|
723
|
+
validateLinks(table, ctx);
|
|
724
|
+
validatePolicyColumns(table, ctx);
|
|
725
|
+
validateDerivedNameLengths(table, ctx);
|
|
726
|
+
});
|
|
727
|
+
var indexName = (table, definition) => definition.name ?? `ix_${table}_${definition.columns.join("_")}`;
|
|
728
|
+
var policyName = (table, suffix) => `${table}_${suffix}`;
|
|
729
|
+
var foreignKeyName = (table, link) => `fk_${table}_${link}`;
|
|
730
|
+
function validateSyncedTable(table, ctx) {
|
|
731
|
+
const forbidden = [
|
|
732
|
+
["actions", "synced tables are read-only; actions are not allowed"],
|
|
733
|
+
["indexes", "v1 synced tables cannot declare indexes; declare them with defineSyncedTable({ indexes }) in a v2 definition"]
|
|
734
|
+
];
|
|
735
|
+
for (const [field, message] of forbidden) {
|
|
736
|
+
if (table[field] !== void 0) ctx.addIssue({ code: "custom", message, path: [field] });
|
|
737
|
+
}
|
|
738
|
+
if (table.policies?.write) {
|
|
739
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "write"] });
|
|
740
|
+
}
|
|
741
|
+
if (table.policies?.insert) {
|
|
742
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "insert"] });
|
|
743
|
+
}
|
|
744
|
+
if (table.policies?.update) {
|
|
745
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "update"] });
|
|
746
|
+
}
|
|
747
|
+
const columns = table.columns ?? {};
|
|
748
|
+
const names = Object.keys(columns);
|
|
749
|
+
if (names.length === 0) return;
|
|
750
|
+
const primaryKeys = names.filter((name) => columns[name]?.primary_key);
|
|
751
|
+
if (primaryKeys.length !== 1) {
|
|
752
|
+
ctx.addIssue({ code: "custom", message: "exactly one column must be primary_key", path: ["columns"] });
|
|
753
|
+
}
|
|
754
|
+
validateRenames(columns, names, ctx);
|
|
755
|
+
for (const name of names) {
|
|
756
|
+
const column = columns[name];
|
|
757
|
+
if (!column) continue;
|
|
758
|
+
const cast = column.cast ? castParts(column.cast) : null;
|
|
759
|
+
if (column.cast && cast?.column !== name) {
|
|
760
|
+
ctx.addIssue({
|
|
761
|
+
code: "custom",
|
|
762
|
+
message: "cast must reference the column it is declared on",
|
|
763
|
+
path: ["columns", name, "cast"]
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
if (cast && cast.type !== column.type) {
|
|
767
|
+
ctx.addIssue({
|
|
768
|
+
code: "custom",
|
|
769
|
+
message: `cast target '${cast.type}' must match the declared type '${column.type}'`,
|
|
770
|
+
path: ["columns", name, "cast"]
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
function validateRenames(columns, names, ctx) {
|
|
776
|
+
const renameSources = /* @__PURE__ */ new Map();
|
|
777
|
+
for (const name of names) {
|
|
778
|
+
const renamedFrom = columns[name]?.renamed_from;
|
|
779
|
+
if (!renamedFrom) continue;
|
|
780
|
+
if (renamedFrom === name || names.includes(renamedFrom)) {
|
|
781
|
+
ctx.addIssue({
|
|
782
|
+
code: "custom",
|
|
783
|
+
message: "renamed_from must name a column that no longer exists",
|
|
784
|
+
path: ["columns", name, "renamed_from"]
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
const claimed = renameSources.get(renamedFrom);
|
|
788
|
+
if (claimed) {
|
|
789
|
+
ctx.addIssue({
|
|
790
|
+
code: "custom",
|
|
791
|
+
message: `renamed_from '${renamedFrom}' is already claimed by '${claimed}'`,
|
|
792
|
+
path: ["columns", name, "renamed_from"]
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
renameSources.set(renamedFrom, name);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function validateColumnTable(table, ctx) {
|
|
799
|
+
const columns = table.columns ?? {};
|
|
800
|
+
const names = Object.keys(columns);
|
|
801
|
+
if (names.length === 0) {
|
|
802
|
+
ctx.addIssue({ code: "custom", message: "a table must declare columns or a source", path: ["columns"] });
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const primaryKeys = names.filter((name) => columns[name]?.primary_key);
|
|
806
|
+
if (primaryKeys.length !== 1) {
|
|
807
|
+
ctx.addIssue({ code: "custom", message: "exactly one column must be primary_key", path: ["columns"] });
|
|
808
|
+
}
|
|
809
|
+
validateRenames(columns, names, ctx);
|
|
810
|
+
for (const name of names) {
|
|
811
|
+
const column = columns[name];
|
|
812
|
+
if (!column) continue;
|
|
813
|
+
const cast = column.cast ? castParts(column.cast) : null;
|
|
814
|
+
if (column.cast && cast?.column !== name) {
|
|
815
|
+
ctx.addIssue({
|
|
816
|
+
code: "custom",
|
|
817
|
+
message: "cast must reference the column it is declared on",
|
|
818
|
+
path: ["columns", name, "cast"]
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
if (cast && cast.type !== column.type) {
|
|
822
|
+
ctx.addIssue({
|
|
823
|
+
code: "custom",
|
|
824
|
+
message: `cast target '${cast.type}' must match the declared type '${column.type}'`,
|
|
825
|
+
path: ["columns", name, "cast"]
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
for (const [position, index] of (table.indexes ?? []).entries()) {
|
|
830
|
+
if (new Set(index.columns).size !== index.columns.length) {
|
|
831
|
+
ctx.addIssue({
|
|
832
|
+
code: "custom",
|
|
833
|
+
message: "index names a column more than once",
|
|
834
|
+
path: ["indexes", position]
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
for (const column of index.columns) {
|
|
838
|
+
if (!names.includes(column)) {
|
|
839
|
+
ctx.addIssue({
|
|
840
|
+
code: "custom",
|
|
841
|
+
message: `index references unknown column '${column}'`,
|
|
842
|
+
path: ["indexes", position]
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
function validateLinks(table, ctx) {
|
|
849
|
+
const columnNames = Object.keys(table.columns ?? {});
|
|
850
|
+
for (const [name, link] of Object.entries(table.links ?? {})) {
|
|
851
|
+
if (columnNames.length > 0 && !columnNames.includes(link.on)) {
|
|
852
|
+
ctx.addIssue({ code: "custom", message: `link references unknown column '${link.on}'`, path: ["links", name, "on"] });
|
|
853
|
+
}
|
|
854
|
+
if (link.enforce && link.to.includes(".")) {
|
|
855
|
+
ctx.addIssue({
|
|
856
|
+
code: "custom",
|
|
857
|
+
message: "enforce is only valid for links inside the namespace; cross-boundary links stay soft",
|
|
858
|
+
path: ["links", name, "enforce"]
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
if (link.enforce && table.source) {
|
|
862
|
+
ctx.addIssue({
|
|
863
|
+
code: "custom",
|
|
864
|
+
message: "a synced table cannot enforce foreign keys \u2014 its DDL is owned by the sync",
|
|
865
|
+
path: ["links", name, "enforce"]
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
function validateDerivedNameLengths(table, ctx) {
|
|
871
|
+
const worstPolicy = policyName(table.table, "insert");
|
|
872
|
+
if (!table.source && worstPolicy.length > MAX_IDENTIFIER_LENGTH) {
|
|
873
|
+
ctx.addIssue({
|
|
874
|
+
code: "custom",
|
|
875
|
+
message: `table name is too long \u2014 the derived policy name '${worstPolicy}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit`,
|
|
876
|
+
path: ["table"]
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
for (const [position, index] of (table.indexes ?? []).entries()) {
|
|
880
|
+
const name = indexName(table.table, index);
|
|
881
|
+
if (name.length > MAX_IDENTIFIER_LENGTH) {
|
|
882
|
+
ctx.addIssue({
|
|
883
|
+
code: "custom",
|
|
884
|
+
message: `derived index name '${name}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 set a shorter explicit name`,
|
|
885
|
+
path: ["indexes", position]
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
for (const [name, link] of Object.entries(table.links ?? {})) {
|
|
890
|
+
const constraint = foreignKeyName(table.table, name);
|
|
891
|
+
if (link.enforce && constraint.length > MAX_IDENTIFIER_LENGTH) {
|
|
892
|
+
ctx.addIssue({
|
|
893
|
+
code: "custom",
|
|
894
|
+
message: `derived foreign-key name '${constraint}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 shorten the link or table name`,
|
|
895
|
+
path: ["links", name]
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
for (const name of Object.keys(table.actions ?? {})) {
|
|
900
|
+
const fnName = `action_${name}`;
|
|
901
|
+
if (fnName.length > MAX_IDENTIFIER_LENGTH) {
|
|
902
|
+
ctx.addIssue({
|
|
903
|
+
code: "custom",
|
|
904
|
+
message: `derived action function name '${fnName}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 shorten the action name`,
|
|
905
|
+
path: ["actions", name]
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
var TYPE_GROUPS = {
|
|
911
|
+
integer: "numeric",
|
|
912
|
+
bigint: "numeric",
|
|
913
|
+
numeric: "numeric",
|
|
914
|
+
double: "numeric",
|
|
915
|
+
text: "text",
|
|
916
|
+
uuid: "uuid",
|
|
917
|
+
date: "temporal",
|
|
918
|
+
timestamptz: "temporal",
|
|
919
|
+
boolean: "boolean",
|
|
920
|
+
jsonb: "jsonb"
|
|
921
|
+
};
|
|
922
|
+
function validatePolicyColumns(table, ctx) {
|
|
923
|
+
const columns = table.columns ?? {};
|
|
924
|
+
const columnNames = Object.keys(columns);
|
|
925
|
+
if (columnNames.length === 0) return;
|
|
926
|
+
for (const kind of ["read", "write", "insert", "update"]) {
|
|
927
|
+
const predicate = table.policies?.[kind];
|
|
928
|
+
if (!predicate) continue;
|
|
929
|
+
for (const column of referencedColumns(predicate.ast)) {
|
|
930
|
+
if (!columnNames.includes(column)) {
|
|
931
|
+
ctx.addIssue({
|
|
932
|
+
code: "custom",
|
|
933
|
+
message: `policy references unknown column '${column}'`,
|
|
934
|
+
path: ["policies", kind]
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
walkPredicate(predicate.ast, (node) => {
|
|
939
|
+
if (node.kind !== "compare") return;
|
|
940
|
+
const issue = comparisonTypeIssue(node.left, node.right, columns);
|
|
941
|
+
if (issue) ctx.addIssue({ code: "custom", message: issue, path: ["policies", kind] });
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
function comparisonTypeIssue(left, right, columns) {
|
|
946
|
+
const groupOf = (operand) => operand.kind === "column" ? columns[operand.name] ? TYPE_GROUPS[columns[operand.name].type] : null : null;
|
|
947
|
+
if (left.kind === "column" && right.kind === "column") {
|
|
948
|
+
const leftGroup = groupOf(left);
|
|
949
|
+
const rightGroup = groupOf(right);
|
|
950
|
+
if (leftGroup && rightGroup && leftGroup !== rightGroup) {
|
|
951
|
+
return `policy compares '${left.name}' (${columns[left.name].type}) with '${right.name}' (${columns[right.name].type}) \u2014 incomparable types`;
|
|
952
|
+
}
|
|
953
|
+
return null;
|
|
954
|
+
}
|
|
955
|
+
const column = left.kind === "column" ? left : right.kind === "column" ? right : null;
|
|
956
|
+
const literal = left.kind === "literal" ? left : right.kind === "literal" ? right : null;
|
|
957
|
+
if (!column || !literal || literal.value === null) return null;
|
|
958
|
+
const columnGroup = groupOf(column);
|
|
959
|
+
if (!columnGroup) return null;
|
|
960
|
+
if (typeof literal.value === "string") return null;
|
|
961
|
+
const literalGroup = typeof literal.value === "number" ? "numeric" : "boolean";
|
|
962
|
+
if (literalGroup !== columnGroup) {
|
|
963
|
+
return `policy compares '${column.name}' (${columns[column.name].type}) with a ${typeof literal.value} literal \u2014 incomparable types`;
|
|
964
|
+
}
|
|
965
|
+
return null;
|
|
966
|
+
}
|
|
226
967
|
var policyVariantSchema = zod.z.object({
|
|
227
968
|
/** Claims-only guard (capability()/role()/claim() comparisons — no columns). */
|
|
228
969
|
when: zod.z.string().min(1).max(1e3),
|
|
@@ -531,7 +1272,7 @@ var CANONICAL_FORMAT = 2;
|
|
|
531
1272
|
var IDENTIFIER_RE = /^[a-z][a-z0-9_]*$/;
|
|
532
1273
|
var identifier2 = zod.z.string().min(1).max(63).regex(IDENTIFIER_RE);
|
|
533
1274
|
var columnIdentifier = zod.z.string().min(1).max(63).regex(COLUMN_IDENTIFIER_PATTERN);
|
|
534
|
-
var
|
|
1275
|
+
var COLUMN_TYPES2 = [
|
|
535
1276
|
"uuid",
|
|
536
1277
|
"text",
|
|
537
1278
|
"integer",
|
|
@@ -543,7 +1284,7 @@ var COLUMN_TYPES = [
|
|
|
543
1284
|
"date",
|
|
544
1285
|
"jsonb"
|
|
545
1286
|
];
|
|
546
|
-
var
|
|
1287
|
+
var columnTypeSchema2 = zod.z.enum(COLUMN_TYPES2);
|
|
547
1288
|
var canonicalEnumSchema = zod.z.object({
|
|
548
1289
|
name: identifier2,
|
|
549
1290
|
values: zod.z.array(zod.z.string().min(1).max(63)).min(1).max(100)
|
|
@@ -552,7 +1293,7 @@ var canonicalColumnSchema = zod.z.object({
|
|
|
552
1293
|
/** Property name in TS (camelCase); columnName is the Postgres name. */
|
|
553
1294
|
name: zod.z.string().min(1).max(63),
|
|
554
1295
|
columnName: columnIdentifier,
|
|
555
|
-
type: zod.z.union([
|
|
1296
|
+
type: zod.z.union([columnTypeSchema2, zod.z.object({ enum: identifier2 })]),
|
|
556
1297
|
nullable: zod.z.boolean().default(true),
|
|
557
1298
|
unique: zod.z.boolean().default(false),
|
|
558
1299
|
/** Literal default, or one of the allowlisted SQL functions. */
|
|
@@ -645,7 +1386,7 @@ var canonicalActionSchema = zod.z.object({
|
|
|
645
1386
|
name: identifier2,
|
|
646
1387
|
requires: zod.z.string().max(1e3).optional(),
|
|
647
1388
|
params: zod.z.record(zod.z.string(), zod.z.object({
|
|
648
|
-
type:
|
|
1389
|
+
type: columnTypeSchema2,
|
|
649
1390
|
nullable: zod.z.boolean().default(false)
|
|
650
1391
|
})).default({}),
|
|
651
1392
|
executes: zod.z.string().min(1).max(1e4),
|
|
@@ -746,7 +1487,8 @@ var canonicalTableSchema = zod.z.object({
|
|
|
746
1487
|
if ([table.policies.write, table.policies.insert, table.policies.update].some((policy) => policy !== void 0)) {
|
|
747
1488
|
fail("is read-only and cannot declare write policies", ["policies"]);
|
|
748
1489
|
}
|
|
749
|
-
const
|
|
1490
|
+
const ownedIndexes = table.source === void 0 ? table.indexes : table.indexes.filter((index) => index.unique);
|
|
1491
|
+
const ownedCollections = [ownedIndexes, table.actions, table.checks, table.foreignKeys, table.cowMutations];
|
|
750
1492
|
const ownedMetadata = [table.cellMetadata, table.internal, table.materializes, table.renamedFrom];
|
|
751
1493
|
if (ownedCollections.some((items) => items !== void 0 && items.length > 0) || ownedMetadata.some((value) => value !== void 0)) {
|
|
752
1494
|
fail("cannot declare owned storage, constraints or identity operations");
|
|
@@ -848,6 +1590,23 @@ function trackedCellColumns(table) {
|
|
|
848
1590
|
return table.columns.filter((column) => column.cell === void 0 && !pk.has(column.columnName) && column.onUpdateFn === void 0);
|
|
849
1591
|
}
|
|
850
1592
|
var SUFFIXES = Object.values(CELL_PART_SUFFIX);
|
|
1593
|
+
function validateSyncedIndexes(table, tableIndex, ctx) {
|
|
1594
|
+
if (table.source === void 0) return;
|
|
1595
|
+
const columns = new Set(table.columns.map((column) => column.columnName));
|
|
1596
|
+
for (const [position, index] of table.indexes.entries()) {
|
|
1597
|
+
const fail = (message) => ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "indexes", position], message: `synced table '${table.table}' ${message}` });
|
|
1598
|
+
if (columns.size === 0) fail("must declare columns to declare indexes");
|
|
1599
|
+
if (index.unique) fail("cannot declare a unique index");
|
|
1600
|
+
if (new Set(index.columns).size !== index.columns.length) fail("index names a column more than once");
|
|
1601
|
+
for (const column of index.columns) {
|
|
1602
|
+
if (columns.size > 0 && !columns.has(column)) fail(`index references unknown column '${column}'`);
|
|
1603
|
+
}
|
|
1604
|
+
const name = indexName(table.table, index);
|
|
1605
|
+
if (name.length > MAX_IDENTIFIER_LENGTH) {
|
|
1606
|
+
fail(`derived index name '${name}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 set a shorter explicit name`);
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
851
1610
|
function validateCellMetadata(table, index, ctx) {
|
|
852
1611
|
const fail = (message, path = ["tables", index]) => ctx.addIssue({ code: "custom", path, message });
|
|
853
1612
|
const cells = table.columns.flatMap((column, position) => column.cell ? [{ column, position, cell: column.cell }] : []);
|
|
@@ -914,12 +1673,24 @@ var localDefinitionSchema = zod.z.object({
|
|
|
914
1673
|
}).default({ uses: [] })
|
|
915
1674
|
}).superRefine((definition, ctx) => {
|
|
916
1675
|
const seen = /* @__PURE__ */ new Set();
|
|
1676
|
+
const syncedIndexOwners = /* @__PURE__ */ new Map();
|
|
917
1677
|
for (const [index, table] of definition.tables.entries()) {
|
|
918
1678
|
if (seen.has(table.table)) {
|
|
919
1679
|
ctx.addIssue({ code: "custom", path: ["tables", index, "table"], message: `duplicate relation '${table.table}'` });
|
|
920
1680
|
}
|
|
921
1681
|
seen.add(table.table);
|
|
922
1682
|
validateCellMetadata(table, index, ctx);
|
|
1683
|
+
validateSyncedIndexes(table, index, ctx);
|
|
1684
|
+
if (table.source !== void 0) {
|
|
1685
|
+
for (const [position, entry] of table.indexes.entries()) {
|
|
1686
|
+
const name = indexName(table.table, entry);
|
|
1687
|
+
const owner = syncedIndexOwners.get(name);
|
|
1688
|
+
if (owner !== void 0) {
|
|
1689
|
+
ctx.addIssue({ code: "custom", path: ["tables", index, "indexes", position], message: `synced index '${name}' is also declared on '${owner}'` });
|
|
1690
|
+
}
|
|
1691
|
+
syncedIndexOwners.set(name, table.table);
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
923
1694
|
if (table.source !== void 0) {
|
|
924
1695
|
for (const [columnIndex, column] of table.columns.entries()) {
|
|
925
1696
|
if (column.onUpdateFn !== void 0) {
|
|
@@ -1118,9 +1889,6 @@ localDefinitionSchema.safeExtend({
|
|
|
1118
1889
|
imports: zod.z.array(localDefinitionSchema).max(32).optional()
|
|
1119
1890
|
});
|
|
1120
1891
|
|
|
1121
|
-
// src/client/sql.ts
|
|
1122
|
-
var BUILTIN_SQL_READ_HASH = "sha256:a50c477c952a5e0fe68ed158a4530e4aab573c09bcbb1a8ac454dc88a39a6258";
|
|
1123
|
-
|
|
1124
1892
|
// src/ir/index.ts
|
|
1125
1893
|
var IR_VERSION = 6;
|
|
1126
1894
|
var LEGACY_IR_VERSION = 1;
|
|
@@ -1411,7 +2179,7 @@ var variableDefaultIrSchema = zod.z.object({
|
|
|
1411
2179
|
var variableIrSchema = zod.z.object({
|
|
1412
2180
|
name: zod.z.string(),
|
|
1413
2181
|
type: zod.z.union([
|
|
1414
|
-
zod.z.object({ kind: zod.z.literal("scalar"), scalar: zod.z.enum(
|
|
2182
|
+
zod.z.object({ kind: zod.z.literal("scalar"), scalar: zod.z.enum(COLUMN_TYPES2) }),
|
|
1415
2183
|
/** `values` is the declared membership whitelist — the executor validates against it. */
|
|
1416
2184
|
zod.z.object({ kind: zod.z.literal("enum"), name: zod.z.string(), values: zod.z.array(zod.z.string()).min(1) }),
|
|
1417
2185
|
/** §4f dynamic slot: validated against `tables[table]` whitelists at runtime. */
|
|
@@ -1449,8 +2217,7 @@ var compiledOperationSchema = zod.z.object({
|
|
|
1449
2217
|
maxRows: zod.z.number().int().min(1).max(1e4)
|
|
1450
2218
|
}).strict().optional(),
|
|
1451
2219
|
irVersion: irVersionSchema,
|
|
1452
|
-
name: zod.z.string(),
|
|
1453
|
-
opHash: zod.z.string().regex(/^sha256:[0-9a-f]{64}$/),
|
|
2220
|
+
name: zod.z.string().regex(/^[_A-Za-z][_0-9A-Za-z]{0,254}$/),
|
|
1454
2221
|
kind: zod.z.enum(["query", "mutation"]),
|
|
1455
2222
|
requires: zod.z.object({
|
|
1456
2223
|
capability: zod.z.string().optional(),
|
|
@@ -1466,7 +2233,7 @@ var compiledOperationSchema = zod.z.object({
|
|
|
1466
2233
|
}
|
|
1467
2234
|
if (operation.language === "sql") {
|
|
1468
2235
|
const validPlan = operation.sqlQuery ? !operation.sql && operation.irVersion === RUNTIME_SQL_IR_VERSION : !!operation.sql && operation.irVersion === SQL_IR_VERSION;
|
|
1469
|
-
const builtin = operation.name === "BuiltinSqlRead" && operation.
|
|
2236
|
+
const builtin = operation.name === "BuiltinSqlRead" && operation.requires === null;
|
|
1470
2237
|
if (!validPlan || operation.kind !== "query" || operation.roots.length !== 0 || !builtin && !operation.requires?.capability) {
|
|
1471
2238
|
ctx.addIssue({ code: "custom", message: "SQL operations require a query-only v4 plan or v5 runtime contract, capability, and no GraphQL roots" });
|
|
1472
2239
|
}
|
|
@@ -1481,7 +2248,7 @@ var compiledOperationSchema = zod.z.object({
|
|
|
1481
2248
|
}
|
|
1482
2249
|
});
|
|
1483
2250
|
var resolvedSourceIrSchema = zod.z.object({
|
|
1484
|
-
name: zod.z.string(),
|
|
2251
|
+
name: zod.z.string().regex(/^[_A-Za-z][_0-9A-Za-z]{0,254}$/),
|
|
1485
2252
|
kind: zod.z.enum(["manual", "etl", "prefetch"]),
|
|
1486
2253
|
/** Logical table key of the backing relation. */
|
|
1487
2254
|
table: zod.z.string(),
|
|
@@ -1544,7 +2311,7 @@ var tableMetaIrSchema = zod.z.object({
|
|
|
1544
2311
|
columns: zod.z.array(zod.z.object({
|
|
1545
2312
|
name: zod.z.string(),
|
|
1546
2313
|
columnName: zod.z.string(),
|
|
1547
|
-
type: zod.z.union([zod.z.enum(
|
|
2314
|
+
type: zod.z.union([zod.z.enum(COLUMN_TYPES2), zod.z.object({ enum: zod.z.string(), values: zod.z.array(zod.z.string()) })]),
|
|
1548
2315
|
nullable: zod.z.boolean()
|
|
1549
2316
|
})),
|
|
1550
2317
|
/** Empty for keyless read-only views; keyed tables and views list logical columns. */
|
|
@@ -1595,7 +2362,6 @@ var compiledArtifactSchema = zod.z.object({
|
|
|
1595
2362
|
ownSchemaHash: zod.z.string().optional(),
|
|
1596
2363
|
dependencies: zod.z.record(zod.z.string(), zod.z.string()).optional(),
|
|
1597
2364
|
readPolicies: zod.z.record(zod.z.string(), predIrSchema).optional(),
|
|
1598
|
-
opsHash: zod.z.string(),
|
|
1599
2365
|
/** Apply-observed synced objects without a serving backing; not part of the authored schema hash. */
|
|
1600
2366
|
unbackedTables: zod.z.array(zod.z.string()).optional(),
|
|
1601
2367
|
tables: zod.z.record(zod.z.string(), tableMetaIrSchema),
|
|
@@ -1625,9 +2391,9 @@ function normalizeIr(raw) {
|
|
|
1625
2391
|
if (!irVersionSchema.safeParse(version).success) throw new IrVersionError(version);
|
|
1626
2392
|
return compiledArtifactSchema.parse(raw);
|
|
1627
2393
|
}
|
|
1628
|
-
function
|
|
2394
|
+
function indexByName(artifact) {
|
|
1629
2395
|
const map = /* @__PURE__ */ new Map();
|
|
1630
|
-
for (const op of Object.values(artifact.operations)) map.set(op.
|
|
2396
|
+
for (const op of Object.values(artifact.operations)) map.set(op.name, op);
|
|
1631
2397
|
return map;
|
|
1632
2398
|
}
|
|
1633
2399
|
|
|
@@ -1649,7 +2415,7 @@ exports.SQL_IR_VERSION = SQL_IR_VERSION;
|
|
|
1649
2415
|
exports.cellColumnName = cellColumnName;
|
|
1650
2416
|
exports.compiledArtifactSchema = compiledArtifactSchema;
|
|
1651
2417
|
exports.compiledOperationSchema = compiledOperationSchema;
|
|
1652
|
-
exports.
|
|
2418
|
+
exports.indexByName = indexByName;
|
|
1653
2419
|
exports.isExternalPolicyClaimKey = isExternalPolicyClaimKey;
|
|
1654
2420
|
exports.limitIrSchema = limitIrSchema;
|
|
1655
2421
|
exports.normalizeIr = normalizeIr;
|