@sequenceholdings/orm 2.10.0 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/authoring.cjs +796 -7
- package/dist/authoring.cjs.map +1 -1
- package/dist/authoring.d.cts +11 -2
- package/dist/authoring.d.ts +11 -2
- 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.d.cts +43 -43
- package/dist/canonical.d.ts +43 -43
- package/dist/canonical.js +777 -6
- package/dist/canonical.js.map +1 -1
- package/dist/cli.cjs +7561 -7502
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +7561 -7502
- package/dist/cli.js.map +1 -1
- package/dist/compiler.cjs +801 -33
- package/dist/compiler.cjs.map +1 -1
- package/dist/compiler.d.cts +1 -1
- package/dist/compiler.d.ts +1 -1
- package/dist/compiler.js +801 -33
- package/dist/compiler.js.map +1 -1
- package/dist/index.cjs +3716 -3445
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +95 -30
- package/dist/index.d.ts +95 -30
- package/dist/index.js +3711 -3444
- package/dist/index.js.map +1 -1
- package/dist/ir.cjs +778 -7
- package/dist/ir.cjs.map +1 -1
- package/dist/ir.d.cts +58 -58
- package/dist/ir.d.ts +58 -58
- package/dist/ir.js +778 -7
- package/dist/ir.js.map +1 -1
- package/dist/predicate-v2.cjs +793 -23
- package/dist/predicate-v2.cjs.map +1 -1
- package/dist/predicate-v2.js +793 -23
- package/dist/predicate-v2.js.map +1 -1
- package/dist/sdl.cjs +786 -17
- package/dist/sdl.cjs.map +1 -1
- package/dist/sdl.js +786 -17
- package/dist/sdl.js.map +1 -1
- package/package.json +1 -1
- package/dist/{definition-B99BsOAO.d.cts → definition-CevES7lM.d.cts} +2 -2
- package/dist/{definition-B99BsOAO.d.ts → definition-CevES7lM.d.ts} +2 -2
package/dist/canonical.cjs
CHANGED
|
@@ -221,9 +221,15 @@ function compileCheckExpression(source, columns) {
|
|
|
221
221
|
|
|
222
222
|
// src/constants.ts
|
|
223
223
|
var NAMESPACE_PATTERN = /^(?!(?:events|native_sources)$)[a-z][a-z0-9_]{0,40}$/;
|
|
224
|
+
var IDENTIFIER_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
224
225
|
var COLUMN_IDENTIFIER_PATTERN = /^_?[a-z][a-z0-9_]*$/;
|
|
225
226
|
var MAX_IDENTIFIER_LENGTH = 63;
|
|
227
|
+
var CAPABILITY_KEY_PATTERN = /^(?:org\/|app\/[a-z][a-z0-9-]{0,63}\/)?[a-z0-9-]{1,64}$/;
|
|
228
|
+
var CLAIM_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
226
229
|
var QUALIFIED_TABLE_PATTERN = /^[a-z][a-z0-9_]{0,62}\.[a-z][a-z0-9_]{0,62}$/;
|
|
230
|
+
var SYNCED_SOURCE_PATTERN = QUALIFIED_TABLE_PATTERN;
|
|
231
|
+
var RESERVED_TABLE_NAMES = /* @__PURE__ */ new Set(["actions", "query"]);
|
|
232
|
+
var RESERVED_COLUMN_NAMES = /* @__PURE__ */ new Set(["select", "order", "limit", "offset"]);
|
|
227
233
|
var CELL_PARTS = ["at", "by", "rev", "source", "conflicts"];
|
|
228
234
|
var CELL_PART_SUFFIX = {
|
|
229
235
|
at: "__at",
|
|
@@ -238,6 +244,741 @@ function cellColumnName(column, part) {
|
|
|
238
244
|
}
|
|
239
245
|
var MATERIALIZE_ACTOR = "orm:materialize";
|
|
240
246
|
JSON.stringify({ sub: MATERIALIZE_ACTOR, org: null, roles: [], capabilities: [] });
|
|
247
|
+
|
|
248
|
+
// src/predicate/ast.ts
|
|
249
|
+
var COMPARE_OPS = ["=", "!=", "<", "<=", ">", ">="];
|
|
250
|
+
function walkPredicate(predicate, visit) {
|
|
251
|
+
visit(predicate);
|
|
252
|
+
if (predicate.kind === "and" || predicate.kind === "or") {
|
|
253
|
+
for (const operand of predicate.operands) walkPredicate(operand, visit);
|
|
254
|
+
}
|
|
255
|
+
if (predicate.kind === "not") walkPredicate(predicate.operand, visit);
|
|
256
|
+
}
|
|
257
|
+
function predicateOperands(predicate) {
|
|
258
|
+
const operands = [];
|
|
259
|
+
walkPredicate(predicate, (node) => {
|
|
260
|
+
if (node.kind === "compare") operands.push(node.left, node.right);
|
|
261
|
+
});
|
|
262
|
+
return operands;
|
|
263
|
+
}
|
|
264
|
+
function referencedColumns(predicate) {
|
|
265
|
+
const names = predicateOperands(predicate).flatMap(
|
|
266
|
+
(operand) => operand.kind === "column" ? [operand.name] : []
|
|
267
|
+
);
|
|
268
|
+
return [...new Set(names)];
|
|
269
|
+
}
|
|
270
|
+
var identifierSchema = zod.z.string().max(MAX_IDENTIFIER_LENGTH).regex(IDENTIFIER_PATTERN, "must be a lowercase identifier (a-z, 0-9, _)");
|
|
271
|
+
var namespaceSchema = zod.z.string().regex(NAMESPACE_PATTERN, "must be lowercase, start with a letter, and be at most 41 characters");
|
|
272
|
+
var tableReferenceSchema = zod.z.string().refine(
|
|
273
|
+
(value) => IDENTIFIER_PATTERN.test(value) || QUALIFIED_TABLE_PATTERN.test(value),
|
|
274
|
+
"must be a table name or a schema-qualified table reference"
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
// src/schema/columns.ts
|
|
278
|
+
var COLUMN_TYPES = [
|
|
279
|
+
"uuid",
|
|
280
|
+
"text",
|
|
281
|
+
"integer",
|
|
282
|
+
"bigint",
|
|
283
|
+
"numeric",
|
|
284
|
+
"double",
|
|
285
|
+
"boolean",
|
|
286
|
+
"timestamptz",
|
|
287
|
+
"date",
|
|
288
|
+
"jsonb"
|
|
289
|
+
];
|
|
290
|
+
var columnTypeSchema = zod.z.enum(COLUMN_TYPES);
|
|
291
|
+
var FUNCTION_DEFAULTS = {
|
|
292
|
+
"gen_random_uuid()": ["uuid"],
|
|
293
|
+
"now()": ["timestamptz", "date"]
|
|
294
|
+
};
|
|
295
|
+
var CAST_PATTERN = /^([a-z][a-z0-9_]*)::([a-z]+)$/;
|
|
296
|
+
function defaultIssue(value, type) {
|
|
297
|
+
if (typeof value === "boolean") return type === "boolean" ? null : `boolean default is invalid for type '${type}'`;
|
|
298
|
+
if (typeof value === "number") {
|
|
299
|
+
if (!["integer", "bigint", "numeric", "double"].includes(type)) {
|
|
300
|
+
return `numeric default is invalid for type '${type}'`;
|
|
301
|
+
}
|
|
302
|
+
if (["integer", "bigint"].includes(type)) {
|
|
303
|
+
if (!Number.isInteger(value)) return `default for type '${type}' must be an integer`;
|
|
304
|
+
if (!Number.isSafeInteger(value)) {
|
|
305
|
+
return `default for type '${type}' exceeds the safe integer range \u2014 YAML numbers lose precision past 2^53`;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (type === "numeric" && Math.abs(value) > Number.MAX_SAFE_INTEGER) {
|
|
309
|
+
return "numeric default exceeds the safe float range \u2014 write it as a string (e.g. '9007199254740993') to keep exact precision";
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
const allowedTypes = FUNCTION_DEFAULTS[value];
|
|
314
|
+
if (allowedTypes) return allowedTypes.includes(type) ? null : `default '${value}' is invalid for type '${type}'`;
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
var columnSchema = zod.z.strictObject({
|
|
318
|
+
type: columnTypeSchema,
|
|
319
|
+
primary_key: zod.z.boolean().optional(),
|
|
320
|
+
nullable: zod.z.boolean().optional(),
|
|
321
|
+
unique: zod.z.boolean().optional(),
|
|
322
|
+
default: zod.z.union([zod.z.string(), zod.z.number(), zod.z.boolean()]).optional(),
|
|
323
|
+
description: zod.z.string().optional(),
|
|
324
|
+
renamed_from: identifierSchema.optional(),
|
|
325
|
+
cast: zod.z.string().optional(),
|
|
326
|
+
deprecated: zod.z.boolean().optional()
|
|
327
|
+
}).superRefine((column, ctx) => {
|
|
328
|
+
if (column.default !== void 0) {
|
|
329
|
+
const issue = defaultIssue(column.default, column.type);
|
|
330
|
+
if (issue) ctx.addIssue({ code: "custom", message: issue, path: ["default"] });
|
|
331
|
+
}
|
|
332
|
+
if (column.primary_key && column.nullable) {
|
|
333
|
+
ctx.addIssue({ code: "custom", message: "a primary key column cannot be nullable", path: ["nullable"] });
|
|
334
|
+
}
|
|
335
|
+
if (column.primary_key && column.deprecated) {
|
|
336
|
+
ctx.addIssue({ code: "custom", message: "a primary key column cannot be deprecated", path: ["deprecated"] });
|
|
337
|
+
}
|
|
338
|
+
if (column.cast && !CAST_PATTERN.test(column.cast)) {
|
|
339
|
+
ctx.addIssue({ code: "custom", message: "cast must have the shape '<column>::<type>'", path: ["cast"] });
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
function castParts(cast) {
|
|
343
|
+
const match = CAST_PATTERN.exec(cast);
|
|
344
|
+
if (!match) return null;
|
|
345
|
+
return { column: match[1], type: match[2] };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/errors.ts
|
|
349
|
+
var PredicateParseError = class extends Error {
|
|
350
|
+
constructor(message, position) {
|
|
351
|
+
super(`${message} (at position ${position})`);
|
|
352
|
+
this.position = position;
|
|
353
|
+
}
|
|
354
|
+
name = "PredicateParseError";
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// src/predicate/parse.ts
|
|
358
|
+
var KEYWORDS = /* @__PURE__ */ new Set(["and", "or", "not", "true", "false", "null", "claim", "capability"]);
|
|
359
|
+
var OPERATOR_PATTERN = /^(!=|<=|>=|<>|=|<|>)/;
|
|
360
|
+
var IDENTIFIER_START = /[a-z_]/;
|
|
361
|
+
var IDENTIFIER_CHAR = /[a-z0-9_.-]/;
|
|
362
|
+
var NUMBER_PATTERN = /^-?\d+(\.\d+)?/;
|
|
363
|
+
function tokenize2(source) {
|
|
364
|
+
const tokens = [];
|
|
365
|
+
let position = 0;
|
|
366
|
+
while (position < source.length) {
|
|
367
|
+
const char = source[position];
|
|
368
|
+
if (/\s/.test(char)) {
|
|
369
|
+
position += 1;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (char === "(") {
|
|
373
|
+
tokens.push({ kind: "lparen", value: char, position });
|
|
374
|
+
position += 1;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (char === ")") {
|
|
378
|
+
tokens.push({ kind: "rparen", value: char, position });
|
|
379
|
+
position += 1;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (char === ",") {
|
|
383
|
+
tokens.push({ kind: "comma", value: char, position });
|
|
384
|
+
position += 1;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (char === "'") {
|
|
388
|
+
const { value, end } = readString(source, position);
|
|
389
|
+
tokens.push({ kind: "string", value, position });
|
|
390
|
+
position = end;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
const operator = OPERATOR_PATTERN.exec(source.slice(position));
|
|
394
|
+
if (operator) {
|
|
395
|
+
const value = operator[0] === "<>" ? "!=" : operator[0];
|
|
396
|
+
tokens.push({ kind: "operator", value, position });
|
|
397
|
+
position += operator[0].length;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const number = NUMBER_PATTERN.exec(source.slice(position));
|
|
401
|
+
if (number && !IDENTIFIER_START.test(char)) {
|
|
402
|
+
tokens.push({ kind: "number", value: number[0], position });
|
|
403
|
+
position += number[0].length;
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (IDENTIFIER_START.test(char)) {
|
|
407
|
+
let end = position;
|
|
408
|
+
while (end < source.length && IDENTIFIER_CHAR.test(source[end])) end += 1;
|
|
409
|
+
tokens.push({ kind: "identifier", value: source.slice(position, end), position });
|
|
410
|
+
position = end;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
throw new PredicateParseError(`unexpected character '${char}'`, position);
|
|
414
|
+
}
|
|
415
|
+
tokens.push({ kind: "end", value: "", position: source.length });
|
|
416
|
+
return tokens;
|
|
417
|
+
}
|
|
418
|
+
function readString(source, start) {
|
|
419
|
+
let value = "";
|
|
420
|
+
let position = start + 1;
|
|
421
|
+
while (position < source.length) {
|
|
422
|
+
const char = source[position];
|
|
423
|
+
if (char !== "'") {
|
|
424
|
+
value += char;
|
|
425
|
+
position += 1;
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (source[position + 1] === "'") {
|
|
429
|
+
value += "'";
|
|
430
|
+
position += 2;
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
return { value, end: position + 1 };
|
|
434
|
+
}
|
|
435
|
+
throw new PredicateParseError("unterminated string literal", start);
|
|
436
|
+
}
|
|
437
|
+
var Parser2 = class {
|
|
438
|
+
constructor(tokens) {
|
|
439
|
+
this.tokens = tokens;
|
|
440
|
+
}
|
|
441
|
+
index = 0;
|
|
442
|
+
parse() {
|
|
443
|
+
const predicate = this.parseOr();
|
|
444
|
+
this.expect("end", "end of expression");
|
|
445
|
+
return predicate;
|
|
446
|
+
}
|
|
447
|
+
parseOr() {
|
|
448
|
+
const operands = [this.parseAnd()];
|
|
449
|
+
while (this.matchKeyword("or")) operands.push(this.parseAnd());
|
|
450
|
+
return operands.length === 1 ? operands[0] : { kind: "or", operands };
|
|
451
|
+
}
|
|
452
|
+
parseAnd() {
|
|
453
|
+
const operands = [this.parseNot()];
|
|
454
|
+
while (this.matchKeyword("and")) operands.push(this.parseNot());
|
|
455
|
+
return operands.length === 1 ? operands[0] : { kind: "and", operands };
|
|
456
|
+
}
|
|
457
|
+
parseNot() {
|
|
458
|
+
if (this.matchKeyword("not")) return { kind: "not", operand: this.parseNot() };
|
|
459
|
+
return this.parsePrimary();
|
|
460
|
+
}
|
|
461
|
+
parsePrimary() {
|
|
462
|
+
if (this.peek().kind === "lparen") {
|
|
463
|
+
this.advance();
|
|
464
|
+
const inner = this.parseOr();
|
|
465
|
+
this.expect("rparen", "')'");
|
|
466
|
+
return inner;
|
|
467
|
+
}
|
|
468
|
+
if (this.peekKeyword("capability")) {
|
|
469
|
+
const capability = this.parseCapability();
|
|
470
|
+
const next = this.peek();
|
|
471
|
+
if (next.kind === "operator") {
|
|
472
|
+
throw new PredicateParseError("capability() is boolean-valued and cannot appear in a comparison", next.position);
|
|
473
|
+
}
|
|
474
|
+
return capability;
|
|
475
|
+
}
|
|
476
|
+
return this.parseComparison();
|
|
477
|
+
}
|
|
478
|
+
parseCapability() {
|
|
479
|
+
this.advance();
|
|
480
|
+
const key = this.parseCallArgument("capability");
|
|
481
|
+
if (!CAPABILITY_KEY_PATTERN.test(key)) {
|
|
482
|
+
throw new PredicateParseError(`invalid capability key '${key}'`, this.peek().position);
|
|
483
|
+
}
|
|
484
|
+
return { kind: "capability", key };
|
|
485
|
+
}
|
|
486
|
+
parseComparison() {
|
|
487
|
+
const left = this.parseOperand();
|
|
488
|
+
const operator = this.expect("operator", "a comparison operator");
|
|
489
|
+
if (!COMPARE_OPS.includes(operator.value)) {
|
|
490
|
+
throw new PredicateParseError(`unsupported operator '${operator.value}'`, operator.position);
|
|
491
|
+
}
|
|
492
|
+
const right = this.parseOperand();
|
|
493
|
+
return { kind: "compare", op: operator.value, left, right };
|
|
494
|
+
}
|
|
495
|
+
parseOperand() {
|
|
496
|
+
const token = this.peek();
|
|
497
|
+
if (token.kind === "string") {
|
|
498
|
+
this.advance();
|
|
499
|
+
return { kind: "literal", value: token.value };
|
|
500
|
+
}
|
|
501
|
+
if (token.kind === "number") {
|
|
502
|
+
this.advance();
|
|
503
|
+
const value = Number(token.value);
|
|
504
|
+
if (!Number.isFinite(value) || Number.isInteger(value) && !Number.isSafeInteger(value)) {
|
|
505
|
+
throw new PredicateParseError(
|
|
506
|
+
`numeric literal '${token.value}' exceeds safe integer precision \u2014 quote it as a string`,
|
|
507
|
+
token.position
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
return { kind: "literal", value };
|
|
511
|
+
}
|
|
512
|
+
if (token.kind !== "identifier") {
|
|
513
|
+
throw new PredicateParseError(`expected an operand, got '${token.value || "end"}'`, token.position);
|
|
514
|
+
}
|
|
515
|
+
this.advance();
|
|
516
|
+
if (token.value === "true" || token.value === "false") {
|
|
517
|
+
return { kind: "literal", value: token.value === "true" };
|
|
518
|
+
}
|
|
519
|
+
if (token.value === "null") return { kind: "literal", value: null };
|
|
520
|
+
if (token.value === "claim") {
|
|
521
|
+
const key = this.parseCallArgument("claim");
|
|
522
|
+
if (!CLAIM_KEY_PATTERN.test(key)) {
|
|
523
|
+
throw new PredicateParseError(`invalid claim key '${key}'`, token.position);
|
|
524
|
+
}
|
|
525
|
+
return { kind: "claim", key };
|
|
526
|
+
}
|
|
527
|
+
if (token.value === "capability") {
|
|
528
|
+
throw new PredicateParseError("capability() is boolean-valued and cannot appear in a comparison", token.position);
|
|
529
|
+
}
|
|
530
|
+
if (KEYWORDS.has(token.value) || !IDENTIFIER_PATTERN.test(token.value)) {
|
|
531
|
+
throw new PredicateParseError(`invalid column reference '${token.value}'`, token.position);
|
|
532
|
+
}
|
|
533
|
+
return { kind: "column", name: token.value };
|
|
534
|
+
}
|
|
535
|
+
parseCallArgument(fn) {
|
|
536
|
+
this.expect("lparen", `'(' after ${fn}`);
|
|
537
|
+
const argument = this.expect("string", `a quoted key in ${fn}(...)`);
|
|
538
|
+
this.expect("rparen", `')' after ${fn} key`);
|
|
539
|
+
return argument.value;
|
|
540
|
+
}
|
|
541
|
+
peek() {
|
|
542
|
+
return this.tokens[this.index];
|
|
543
|
+
}
|
|
544
|
+
peekKeyword(keyword) {
|
|
545
|
+
const token = this.peek();
|
|
546
|
+
return token.kind === "identifier" && token.value === keyword;
|
|
547
|
+
}
|
|
548
|
+
matchKeyword(keyword) {
|
|
549
|
+
if (!this.peekKeyword(keyword)) return false;
|
|
550
|
+
this.advance();
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
advance() {
|
|
554
|
+
const token = this.peek();
|
|
555
|
+
if (token.kind !== "end") this.index += 1;
|
|
556
|
+
return token;
|
|
557
|
+
}
|
|
558
|
+
expect(kind, description) {
|
|
559
|
+
const token = this.peek();
|
|
560
|
+
if (token.kind !== kind) {
|
|
561
|
+
throw new PredicateParseError(`expected ${description}, got '${token.value || "end"}'`, token.position);
|
|
562
|
+
}
|
|
563
|
+
return this.advance();
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
function parsePredicate(source) {
|
|
567
|
+
const trimmed = source.trim();
|
|
568
|
+
if (trimmed.length === 0) throw new PredicateParseError("empty predicate", 0);
|
|
569
|
+
return { source: trimmed, ast: new Parser2(tokenize2(trimmed)).parse() };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// src/schema/predicates.ts
|
|
573
|
+
var parsedPredicateSchema = zod.z.string().transform((source, ctx) => {
|
|
574
|
+
try {
|
|
575
|
+
return parsePredicate(source);
|
|
576
|
+
} catch (error) {
|
|
577
|
+
ctx.addIssue({ code: "custom", message: error instanceof Error ? error.message : String(error) });
|
|
578
|
+
return zod.z.NEVER;
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// src/schema/actions.ts
|
|
583
|
+
var EXECUTES_VERB_PATTERN = /^\s*(insert|update|delete)\b/i;
|
|
584
|
+
var DOLLAR_QUOTE_PATTERN = /\$[a-zA-Z0-9_]*\$/;
|
|
585
|
+
var RETURNING_PATTERN = /\breturning\b/i;
|
|
586
|
+
var ESCAPE_STRING_PATTERN = /(?<![a-zA-Z0-9_])[Ee]'/;
|
|
587
|
+
var MASKED_CLAIM_CALL_PATTERN = /\b(?:claim|capability)\(\s*''\s*\)/g;
|
|
588
|
+
var BARE_CLAIM_CALL_PATTERN = /\b(?:claim|capability)\s*\(/;
|
|
589
|
+
var EXISTS_ENTRY_PATTERN = /^exists\(\s*'([^']+)'\s*,\s*([a-z][a-z0-9_]*)\s*\)$/;
|
|
590
|
+
var NAMED_PARAM_PATTERN = /(?<!:):([a-z][a-z0-9_]*)/g;
|
|
591
|
+
var QUOTED_STRING_PATTERN = /'(?:[^']|'')*'/g;
|
|
592
|
+
var SQL_COMMENT_PATTERN = /--|\/\*/;
|
|
593
|
+
var actionParamSchema = zod.z.strictObject({
|
|
594
|
+
type: columnTypeSchema,
|
|
595
|
+
optional: zod.z.boolean().optional(),
|
|
596
|
+
description: zod.z.string().optional()
|
|
597
|
+
});
|
|
598
|
+
var validatesEntrySchema = zod.z.string().transform((source, ctx) => {
|
|
599
|
+
const match = EXISTS_ENTRY_PATTERN.exec(source.trim());
|
|
600
|
+
if (!match) {
|
|
601
|
+
ctx.addIssue({ code: "custom", message: "validates entries must have the shape exists('<table>', <param>)" });
|
|
602
|
+
return zod.z.NEVER;
|
|
603
|
+
}
|
|
604
|
+
const table = tableReferenceSchema.safeParse(match[1]);
|
|
605
|
+
if (!table.success) {
|
|
606
|
+
ctx.addIssue({ code: "custom", message: `invalid table reference '${match[1]}'` });
|
|
607
|
+
return zod.z.NEVER;
|
|
608
|
+
}
|
|
609
|
+
return { source: source.trim(), table: table.data, param: match[2] };
|
|
610
|
+
});
|
|
611
|
+
var AUDIT_ACTIONS = ["create", "update", "delete"];
|
|
612
|
+
var auditSchema = zod.z.strictObject({
|
|
613
|
+
action: zod.z.enum(AUDIT_ACTIONS),
|
|
614
|
+
resource: identifierSchema.optional()
|
|
615
|
+
});
|
|
616
|
+
var actionSchema = zod.z.strictObject({
|
|
617
|
+
description: zod.z.string().optional(),
|
|
618
|
+
requires: parsedPredicateSchema.optional(),
|
|
619
|
+
params: zod.z.record(identifierSchema, actionParamSchema).optional(),
|
|
620
|
+
validates: zod.z.array(validatesEntrySchema).optional(),
|
|
621
|
+
// One statement, or an ordered list run atomically in the action function.
|
|
622
|
+
executes: zod.z.union([zod.z.string().min(1), zod.z.array(zod.z.string().min(1)).min(1)]),
|
|
623
|
+
// Declares the columns the action's RETURNING yields. Present → the
|
|
624
|
+
// function returns those rows (typed in the client); absent → the action
|
|
625
|
+
// reports rows_affected only. The column names/types must match the
|
|
626
|
+
// RETURNING list, which Postgres enforces at CREATE FUNCTION.
|
|
627
|
+
returns: zod.z.record(identifierSchema, columnTypeSchema).optional(),
|
|
628
|
+
audit: auditSchema.optional()
|
|
629
|
+
}).superRefine((action, ctx) => {
|
|
630
|
+
const params = Object.keys(action.params ?? {});
|
|
631
|
+
if (action.requires && referencedColumns(action.requires.ast).length > 0) {
|
|
632
|
+
ctx.addIssue({
|
|
633
|
+
code: "custom",
|
|
634
|
+
message: "requires may reference claims and capabilities only, not columns",
|
|
635
|
+
path: ["requires"]
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
for (const issue of executesIssues(action.executes, new Set(params), action.returns !== void 0)) {
|
|
639
|
+
ctx.addIssue({ code: "custom", message: issue, path: ["executes"] });
|
|
640
|
+
}
|
|
641
|
+
if (action.returns && Object.keys(action.returns).length === 0) {
|
|
642
|
+
ctx.addIssue({ code: "custom", message: "returns must declare at least one column", path: ["returns"] });
|
|
643
|
+
}
|
|
644
|
+
for (const [index, entry] of (action.validates ?? []).entries()) {
|
|
645
|
+
if (!params.includes(entry.param)) {
|
|
646
|
+
ctx.addIssue({
|
|
647
|
+
code: "custom",
|
|
648
|
+
message: `validates references undeclared param '${entry.param}'`,
|
|
649
|
+
path: ["validates", index]
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
function extractNamedParams(sql) {
|
|
655
|
+
const withoutStrings = sql.replace(QUOTED_STRING_PATTERN, "''");
|
|
656
|
+
const names = [...withoutStrings.matchAll(NAMED_PARAM_PATTERN)].map((match) => match[1]);
|
|
657
|
+
return [...new Set(names)];
|
|
658
|
+
}
|
|
659
|
+
function actionStatementSources(executes) {
|
|
660
|
+
return (Array.isArray(executes) ? executes : [executes]).map((statement) => statement.trim());
|
|
661
|
+
}
|
|
662
|
+
function executesIssues(executes, declaredParams, hasReturns) {
|
|
663
|
+
const issues = [];
|
|
664
|
+
const statements = actionStatementSources(executes);
|
|
665
|
+
statements.forEach((statement, index) => {
|
|
666
|
+
const withoutStrings = statement.replace(QUOTED_STRING_PATTERN, "''");
|
|
667
|
+
const at = statements.length > 1 ? ` (statement ${index + 1})` : "";
|
|
668
|
+
if (!EXECUTES_VERB_PATTERN.test(statement)) issues.push(`executes must be INSERT, UPDATE, or DELETE${at}`);
|
|
669
|
+
if (withoutStrings.replace(/;\s*$/, "").includes(";")) issues.push(`each executes entry must be a single statement${at}`);
|
|
670
|
+
if (SQL_COMMENT_PATTERN.test(withoutStrings)) issues.push(`executes must not contain SQL comments${at}`);
|
|
671
|
+
if (DOLLAR_QUOTE_PATTERN.test(withoutStrings)) issues.push(`executes must not use dollar-quoted strings${at}`);
|
|
672
|
+
if (ESCAPE_STRING_PATTERN.test(withoutStrings)) issues.push(`executes must not use escape strings (E'\u2026')${at}`);
|
|
673
|
+
if (withoutStrings.includes('"')) issues.push(`executes must not use double-quoted identifiers${at}`);
|
|
674
|
+
if (BARE_CLAIM_CALL_PATTERN.test(withoutStrings.replace(MASKED_CLAIM_CALL_PATTERN, ""))) {
|
|
675
|
+
issues.push(`executes has a malformed claim()/capability() call \u2014 use e.g. claim('sub')${at}`);
|
|
676
|
+
}
|
|
677
|
+
if (RETURNING_PATTERN.test(withoutStrings) && !(hasReturns && index === statements.length - 1)) {
|
|
678
|
+
issues.push(
|
|
679
|
+
hasReturns ? `only the last executes statement may use RETURNING${at}` : `executes must not use RETURNING unless the action declares returns${at}`
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
const last = statements.at(-1);
|
|
684
|
+
if (hasReturns && last && !RETURNING_PATTERN.test(last.replace(QUOTED_STRING_PATTERN, "''"))) {
|
|
685
|
+
issues.push("the last executes statement must use RETURNING to populate the declared returns columns");
|
|
686
|
+
}
|
|
687
|
+
const undeclared = extractNamedParams(statements.join("\n")).filter((name) => !declaredParams.has(name));
|
|
688
|
+
if (undeclared.length > 0) issues.push(`executes references undeclared params: ${undeclared.join(", ")}`);
|
|
689
|
+
return issues;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// src/schema/table.ts
|
|
693
|
+
var linkSchema = zod.z.strictObject({
|
|
694
|
+
to: tableReferenceSchema,
|
|
695
|
+
on: identifierSchema,
|
|
696
|
+
enforce: zod.z.boolean().optional(),
|
|
697
|
+
description: zod.z.string().optional()
|
|
698
|
+
});
|
|
699
|
+
var indexSchema = zod.z.union([
|
|
700
|
+
zod.z.array(identifierSchema).min(1),
|
|
701
|
+
zod.z.strictObject({
|
|
702
|
+
columns: zod.z.array(identifierSchema).min(1),
|
|
703
|
+
unique: zod.z.boolean().optional(),
|
|
704
|
+
name: identifierSchema.optional()
|
|
705
|
+
})
|
|
706
|
+
]).transform((index) => Array.isArray(index) ? { columns: index } : index);
|
|
707
|
+
var policiesSchema = zod.z.strictObject({
|
|
708
|
+
read: parsedPredicateSchema.optional(),
|
|
709
|
+
write: parsedPredicateSchema.optional(),
|
|
710
|
+
insert: parsedPredicateSchema.optional(),
|
|
711
|
+
update: parsedPredicateSchema.optional()
|
|
712
|
+
});
|
|
713
|
+
zod.z.strictObject({
|
|
714
|
+
namespace: namespaceSchema,
|
|
715
|
+
table: identifierSchema,
|
|
716
|
+
description: zod.z.string().optional(),
|
|
717
|
+
columns: zod.z.record(identifierSchema, columnSchema).optional(),
|
|
718
|
+
source: zod.z.string().regex(SYNCED_SOURCE_PATTERN, "source must be a qualified lowercase schema.table identifier").optional(),
|
|
719
|
+
links: zod.z.record(identifierSchema, linkSchema).optional(),
|
|
720
|
+
indexes: zod.z.array(indexSchema).optional(),
|
|
721
|
+
policies: policiesSchema.optional(),
|
|
722
|
+
actions: zod.z.record(identifierSchema, actionSchema).optional()
|
|
723
|
+
}).superRefine((table, ctx) => {
|
|
724
|
+
if (RESERVED_TABLE_NAMES.has(table.table)) {
|
|
725
|
+
ctx.addIssue({ code: "custom", message: `'${table.table}' is a reserved table name`, path: ["table"] });
|
|
726
|
+
}
|
|
727
|
+
for (const name of Object.keys(table.columns ?? {})) {
|
|
728
|
+
if (RESERVED_COLUMN_NAMES.has(name)) {
|
|
729
|
+
ctx.addIssue({
|
|
730
|
+
code: "custom",
|
|
731
|
+
message: `column '${name}' collides with the Data API's reserved query parameters (select, order, limit, offset) \u2014 it could never be filtered`,
|
|
732
|
+
path: ["columns", name]
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
if (table.source) validateSyncedTable(table, ctx);
|
|
737
|
+
else validateColumnTable(table, ctx);
|
|
738
|
+
validateLinks(table, ctx);
|
|
739
|
+
validatePolicyColumns(table, ctx);
|
|
740
|
+
validateDerivedNameLengths(table, ctx);
|
|
741
|
+
});
|
|
742
|
+
var indexName = (table, definition) => definition.name ?? `ix_${table}_${definition.columns.join("_")}`;
|
|
743
|
+
var policyName = (table, suffix) => `${table}_${suffix}`;
|
|
744
|
+
var foreignKeyName = (table, link) => `fk_${table}_${link}`;
|
|
745
|
+
function validateSyncedTable(table, ctx) {
|
|
746
|
+
const forbidden = [
|
|
747
|
+
["actions", "synced tables are read-only; actions are not allowed"],
|
|
748
|
+
["indexes", "v1 synced tables cannot declare indexes; declare them with defineSyncedTable({ indexes }) in a v2 definition"]
|
|
749
|
+
];
|
|
750
|
+
for (const [field, message] of forbidden) {
|
|
751
|
+
if (table[field] !== void 0) ctx.addIssue({ code: "custom", message, path: [field] });
|
|
752
|
+
}
|
|
753
|
+
if (table.policies?.write) {
|
|
754
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "write"] });
|
|
755
|
+
}
|
|
756
|
+
if (table.policies?.insert) {
|
|
757
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "insert"] });
|
|
758
|
+
}
|
|
759
|
+
if (table.policies?.update) {
|
|
760
|
+
ctx.addIssue({ code: "custom", message: "synced tables are read-only", path: ["policies", "update"] });
|
|
761
|
+
}
|
|
762
|
+
const columns = table.columns ?? {};
|
|
763
|
+
const names = Object.keys(columns);
|
|
764
|
+
if (names.length === 0) return;
|
|
765
|
+
const primaryKeys = names.filter((name) => columns[name]?.primary_key);
|
|
766
|
+
if (primaryKeys.length !== 1) {
|
|
767
|
+
ctx.addIssue({ code: "custom", message: "exactly one column must be primary_key", path: ["columns"] });
|
|
768
|
+
}
|
|
769
|
+
validateRenames(columns, names, ctx);
|
|
770
|
+
for (const name of names) {
|
|
771
|
+
const column = columns[name];
|
|
772
|
+
if (!column) continue;
|
|
773
|
+
const cast = column.cast ? castParts(column.cast) : null;
|
|
774
|
+
if (column.cast && cast?.column !== name) {
|
|
775
|
+
ctx.addIssue({
|
|
776
|
+
code: "custom",
|
|
777
|
+
message: "cast must reference the column it is declared on",
|
|
778
|
+
path: ["columns", name, "cast"]
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
if (cast && cast.type !== column.type) {
|
|
782
|
+
ctx.addIssue({
|
|
783
|
+
code: "custom",
|
|
784
|
+
message: `cast target '${cast.type}' must match the declared type '${column.type}'`,
|
|
785
|
+
path: ["columns", name, "cast"]
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function validateRenames(columns, names, ctx) {
|
|
791
|
+
const renameSources = /* @__PURE__ */ new Map();
|
|
792
|
+
for (const name of names) {
|
|
793
|
+
const renamedFrom = columns[name]?.renamed_from;
|
|
794
|
+
if (!renamedFrom) continue;
|
|
795
|
+
if (renamedFrom === name || names.includes(renamedFrom)) {
|
|
796
|
+
ctx.addIssue({
|
|
797
|
+
code: "custom",
|
|
798
|
+
message: "renamed_from must name a column that no longer exists",
|
|
799
|
+
path: ["columns", name, "renamed_from"]
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
const claimed = renameSources.get(renamedFrom);
|
|
803
|
+
if (claimed) {
|
|
804
|
+
ctx.addIssue({
|
|
805
|
+
code: "custom",
|
|
806
|
+
message: `renamed_from '${renamedFrom}' is already claimed by '${claimed}'`,
|
|
807
|
+
path: ["columns", name, "renamed_from"]
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
renameSources.set(renamedFrom, name);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
function validateColumnTable(table, ctx) {
|
|
814
|
+
const columns = table.columns ?? {};
|
|
815
|
+
const names = Object.keys(columns);
|
|
816
|
+
if (names.length === 0) {
|
|
817
|
+
ctx.addIssue({ code: "custom", message: "a table must declare columns or a source", path: ["columns"] });
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const primaryKeys = names.filter((name) => columns[name]?.primary_key);
|
|
821
|
+
if (primaryKeys.length !== 1) {
|
|
822
|
+
ctx.addIssue({ code: "custom", message: "exactly one column must be primary_key", path: ["columns"] });
|
|
823
|
+
}
|
|
824
|
+
validateRenames(columns, names, ctx);
|
|
825
|
+
for (const name of names) {
|
|
826
|
+
const column = columns[name];
|
|
827
|
+
if (!column) continue;
|
|
828
|
+
const cast = column.cast ? castParts(column.cast) : null;
|
|
829
|
+
if (column.cast && cast?.column !== name) {
|
|
830
|
+
ctx.addIssue({
|
|
831
|
+
code: "custom",
|
|
832
|
+
message: "cast must reference the column it is declared on",
|
|
833
|
+
path: ["columns", name, "cast"]
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
if (cast && cast.type !== column.type) {
|
|
837
|
+
ctx.addIssue({
|
|
838
|
+
code: "custom",
|
|
839
|
+
message: `cast target '${cast.type}' must match the declared type '${column.type}'`,
|
|
840
|
+
path: ["columns", name, "cast"]
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
for (const [position, index] of (table.indexes ?? []).entries()) {
|
|
845
|
+
if (new Set(index.columns).size !== index.columns.length) {
|
|
846
|
+
ctx.addIssue({
|
|
847
|
+
code: "custom",
|
|
848
|
+
message: "index names a column more than once",
|
|
849
|
+
path: ["indexes", position]
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
for (const column of index.columns) {
|
|
853
|
+
if (!names.includes(column)) {
|
|
854
|
+
ctx.addIssue({
|
|
855
|
+
code: "custom",
|
|
856
|
+
message: `index references unknown column '${column}'`,
|
|
857
|
+
path: ["indexes", position]
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
function validateLinks(table, ctx) {
|
|
864
|
+
const columnNames = Object.keys(table.columns ?? {});
|
|
865
|
+
for (const [name, link] of Object.entries(table.links ?? {})) {
|
|
866
|
+
if (columnNames.length > 0 && !columnNames.includes(link.on)) {
|
|
867
|
+
ctx.addIssue({ code: "custom", message: `link references unknown column '${link.on}'`, path: ["links", name, "on"] });
|
|
868
|
+
}
|
|
869
|
+
if (link.enforce && link.to.includes(".")) {
|
|
870
|
+
ctx.addIssue({
|
|
871
|
+
code: "custom",
|
|
872
|
+
message: "enforce is only valid for links inside the namespace; cross-boundary links stay soft",
|
|
873
|
+
path: ["links", name, "enforce"]
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
if (link.enforce && table.source) {
|
|
877
|
+
ctx.addIssue({
|
|
878
|
+
code: "custom",
|
|
879
|
+
message: "a synced table cannot enforce foreign keys \u2014 its DDL is owned by the sync",
|
|
880
|
+
path: ["links", name, "enforce"]
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
function validateDerivedNameLengths(table, ctx) {
|
|
886
|
+
const worstPolicy = policyName(table.table, "insert");
|
|
887
|
+
if (!table.source && worstPolicy.length > MAX_IDENTIFIER_LENGTH) {
|
|
888
|
+
ctx.addIssue({
|
|
889
|
+
code: "custom",
|
|
890
|
+
message: `table name is too long \u2014 the derived policy name '${worstPolicy}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit`,
|
|
891
|
+
path: ["table"]
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
for (const [position, index] of (table.indexes ?? []).entries()) {
|
|
895
|
+
const name = indexName(table.table, index);
|
|
896
|
+
if (name.length > MAX_IDENTIFIER_LENGTH) {
|
|
897
|
+
ctx.addIssue({
|
|
898
|
+
code: "custom",
|
|
899
|
+
message: `derived index name '${name}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 set a shorter explicit name`,
|
|
900
|
+
path: ["indexes", position]
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
for (const [name, link] of Object.entries(table.links ?? {})) {
|
|
905
|
+
const constraint = foreignKeyName(table.table, name);
|
|
906
|
+
if (link.enforce && constraint.length > MAX_IDENTIFIER_LENGTH) {
|
|
907
|
+
ctx.addIssue({
|
|
908
|
+
code: "custom",
|
|
909
|
+
message: `derived foreign-key name '${constraint}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 shorten the link or table name`,
|
|
910
|
+
path: ["links", name]
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
for (const name of Object.keys(table.actions ?? {})) {
|
|
915
|
+
const fnName = `action_${name}`;
|
|
916
|
+
if (fnName.length > MAX_IDENTIFIER_LENGTH) {
|
|
917
|
+
ctx.addIssue({
|
|
918
|
+
code: "custom",
|
|
919
|
+
message: `derived action function name '${fnName}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 shorten the action name`,
|
|
920
|
+
path: ["actions", name]
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
var TYPE_GROUPS = {
|
|
926
|
+
integer: "numeric",
|
|
927
|
+
bigint: "numeric",
|
|
928
|
+
numeric: "numeric",
|
|
929
|
+
double: "numeric",
|
|
930
|
+
text: "text",
|
|
931
|
+
uuid: "uuid",
|
|
932
|
+
date: "temporal",
|
|
933
|
+
timestamptz: "temporal",
|
|
934
|
+
boolean: "boolean",
|
|
935
|
+
jsonb: "jsonb"
|
|
936
|
+
};
|
|
937
|
+
function validatePolicyColumns(table, ctx) {
|
|
938
|
+
const columns = table.columns ?? {};
|
|
939
|
+
const columnNames = Object.keys(columns);
|
|
940
|
+
if (columnNames.length === 0) return;
|
|
941
|
+
for (const kind of ["read", "write", "insert", "update"]) {
|
|
942
|
+
const predicate = table.policies?.[kind];
|
|
943
|
+
if (!predicate) continue;
|
|
944
|
+
for (const column of referencedColumns(predicate.ast)) {
|
|
945
|
+
if (!columnNames.includes(column)) {
|
|
946
|
+
ctx.addIssue({
|
|
947
|
+
code: "custom",
|
|
948
|
+
message: `policy references unknown column '${column}'`,
|
|
949
|
+
path: ["policies", kind]
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
walkPredicate(predicate.ast, (node) => {
|
|
954
|
+
if (node.kind !== "compare") return;
|
|
955
|
+
const issue = comparisonTypeIssue(node.left, node.right, columns);
|
|
956
|
+
if (issue) ctx.addIssue({ code: "custom", message: issue, path: ["policies", kind] });
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
function comparisonTypeIssue(left, right, columns) {
|
|
961
|
+
const groupOf = (operand) => operand.kind === "column" ? columns[operand.name] ? TYPE_GROUPS[columns[operand.name].type] : null : null;
|
|
962
|
+
if (left.kind === "column" && right.kind === "column") {
|
|
963
|
+
const leftGroup = groupOf(left);
|
|
964
|
+
const rightGroup = groupOf(right);
|
|
965
|
+
if (leftGroup && rightGroup && leftGroup !== rightGroup) {
|
|
966
|
+
return `policy compares '${left.name}' (${columns[left.name].type}) with '${right.name}' (${columns[right.name].type}) \u2014 incomparable types`;
|
|
967
|
+
}
|
|
968
|
+
return null;
|
|
969
|
+
}
|
|
970
|
+
const column = left.kind === "column" ? left : right.kind === "column" ? right : null;
|
|
971
|
+
const literal = left.kind === "literal" ? left : right.kind === "literal" ? right : null;
|
|
972
|
+
if (!column || !literal || literal.value === null) return null;
|
|
973
|
+
const columnGroup = groupOf(column);
|
|
974
|
+
if (!columnGroup) return null;
|
|
975
|
+
if (typeof literal.value === "string") return null;
|
|
976
|
+
const literalGroup = typeof literal.value === "number" ? "numeric" : "boolean";
|
|
977
|
+
if (literalGroup !== columnGroup) {
|
|
978
|
+
return `policy compares '${column.name}' (${columns[column.name].type}) with a ${typeof literal.value} literal \u2014 incomparable types`;
|
|
979
|
+
}
|
|
980
|
+
return null;
|
|
981
|
+
}
|
|
241
982
|
var policyVariantSchema = zod.z.object({
|
|
242
983
|
/** Claims-only guard (capability()/role()/claim() comparisons — no columns). */
|
|
243
984
|
when: zod.z.string().min(1).max(1e3),
|
|
@@ -555,7 +1296,7 @@ var CANONICAL_FORMAT = 2;
|
|
|
555
1296
|
var IDENTIFIER_RE = /^[a-z][a-z0-9_]*$/;
|
|
556
1297
|
var identifier2 = zod.z.string().min(1).max(63).regex(IDENTIFIER_RE);
|
|
557
1298
|
var columnIdentifier = zod.z.string().min(1).max(63).regex(COLUMN_IDENTIFIER_PATTERN);
|
|
558
|
-
var
|
|
1299
|
+
var COLUMN_TYPES2 = [
|
|
559
1300
|
"uuid",
|
|
560
1301
|
"text",
|
|
561
1302
|
"integer",
|
|
@@ -567,7 +1308,7 @@ var COLUMN_TYPES = [
|
|
|
567
1308
|
"date",
|
|
568
1309
|
"jsonb"
|
|
569
1310
|
];
|
|
570
|
-
var
|
|
1311
|
+
var columnTypeSchema2 = zod.z.enum(COLUMN_TYPES2);
|
|
571
1312
|
var canonicalEnumSchema = zod.z.object({
|
|
572
1313
|
name: identifier2,
|
|
573
1314
|
values: zod.z.array(zod.z.string().min(1).max(63)).min(1).max(100)
|
|
@@ -576,7 +1317,7 @@ var canonicalColumnSchema = zod.z.object({
|
|
|
576
1317
|
/** Property name in TS (camelCase); columnName is the Postgres name. */
|
|
577
1318
|
name: zod.z.string().min(1).max(63),
|
|
578
1319
|
columnName: columnIdentifier,
|
|
579
|
-
type: zod.z.union([
|
|
1320
|
+
type: zod.z.union([columnTypeSchema2, zod.z.object({ enum: identifier2 })]),
|
|
580
1321
|
nullable: zod.z.boolean().default(true),
|
|
581
1322
|
unique: zod.z.boolean().default(false),
|
|
582
1323
|
/** Literal default, or one of the allowlisted SQL functions. */
|
|
@@ -826,7 +1567,7 @@ var canonicalActionSchema = zod.z.object({
|
|
|
826
1567
|
name: identifier2,
|
|
827
1568
|
requires: zod.z.string().max(1e3).optional(),
|
|
828
1569
|
params: zod.z.record(zod.z.string(), zod.z.object({
|
|
829
|
-
type:
|
|
1570
|
+
type: columnTypeSchema2,
|
|
830
1571
|
nullable: zod.z.boolean().default(false)
|
|
831
1572
|
})).default({}),
|
|
832
1573
|
executes: zod.z.string().min(1).max(1e4),
|
|
@@ -927,7 +1668,8 @@ var canonicalTableSchema = zod.z.object({
|
|
|
927
1668
|
if ([table.policies.write, table.policies.insert, table.policies.update].some((policy) => policy !== void 0)) {
|
|
928
1669
|
fail("is read-only and cannot declare write policies", ["policies"]);
|
|
929
1670
|
}
|
|
930
|
-
const
|
|
1671
|
+
const ownedIndexes = table.source === void 0 ? table.indexes : table.indexes.filter((index) => index.unique);
|
|
1672
|
+
const ownedCollections = [ownedIndexes, table.actions, table.checks, table.foreignKeys, table.cowMutations];
|
|
931
1673
|
const ownedMetadata = [table.cellMetadata, table.internal, table.materializes, table.renamedFrom];
|
|
932
1674
|
if (ownedCollections.some((items) => items !== void 0 && items.length > 0) || ownedMetadata.some((value) => value !== void 0)) {
|
|
933
1675
|
fail("cannot declare owned storage, constraints or identity operations");
|
|
@@ -1029,6 +1771,23 @@ function trackedCellColumns(table) {
|
|
|
1029
1771
|
return table.columns.filter((column) => column.cell === void 0 && !pk.has(column.columnName) && column.onUpdateFn === void 0);
|
|
1030
1772
|
}
|
|
1031
1773
|
var SUFFIXES = Object.values(CELL_PART_SUFFIX);
|
|
1774
|
+
function validateSyncedIndexes(table, tableIndex, ctx) {
|
|
1775
|
+
if (table.source === void 0) return;
|
|
1776
|
+
const columns = new Set(table.columns.map((column) => column.columnName));
|
|
1777
|
+
for (const [position, index] of table.indexes.entries()) {
|
|
1778
|
+
const fail = (message) => ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "indexes", position], message: `synced table '${table.table}' ${message}` });
|
|
1779
|
+
if (columns.size === 0) fail("must declare columns to declare indexes");
|
|
1780
|
+
if (index.unique) fail("cannot declare a unique index");
|
|
1781
|
+
if (new Set(index.columns).size !== index.columns.length) fail("index names a column more than once");
|
|
1782
|
+
for (const column of index.columns) {
|
|
1783
|
+
if (columns.size > 0 && !columns.has(column)) fail(`index references unknown column '${column}'`);
|
|
1784
|
+
}
|
|
1785
|
+
const name = indexName(table.table, index);
|
|
1786
|
+
if (name.length > MAX_IDENTIFIER_LENGTH) {
|
|
1787
|
+
fail(`derived index name '${name}' exceeds Postgres's ${MAX_IDENTIFIER_LENGTH}-byte identifier limit \u2014 set a shorter explicit name`);
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1032
1791
|
function validateCellMetadata(table, index, ctx) {
|
|
1033
1792
|
const fail = (message, path = ["tables", index]) => ctx.addIssue({ code: "custom", path, message });
|
|
1034
1793
|
const cells = table.columns.flatMap((column, position) => column.cell ? [{ column, position, cell: column.cell }] : []);
|
|
@@ -1095,12 +1854,24 @@ var localDefinitionSchema = zod.z.object({
|
|
|
1095
1854
|
}).default({ uses: [] })
|
|
1096
1855
|
}).superRefine((definition, ctx) => {
|
|
1097
1856
|
const seen = /* @__PURE__ */ new Set();
|
|
1857
|
+
const syncedIndexOwners = /* @__PURE__ */ new Map();
|
|
1098
1858
|
for (const [index, table] of definition.tables.entries()) {
|
|
1099
1859
|
if (seen.has(table.table)) {
|
|
1100
1860
|
ctx.addIssue({ code: "custom", path: ["tables", index, "table"], message: `duplicate relation '${table.table}'` });
|
|
1101
1861
|
}
|
|
1102
1862
|
seen.add(table.table);
|
|
1103
1863
|
validateCellMetadata(table, index, ctx);
|
|
1864
|
+
validateSyncedIndexes(table, index, ctx);
|
|
1865
|
+
if (table.source !== void 0) {
|
|
1866
|
+
for (const [position, entry] of table.indexes.entries()) {
|
|
1867
|
+
const name = indexName(table.table, entry);
|
|
1868
|
+
const owner = syncedIndexOwners.get(name);
|
|
1869
|
+
if (owner !== void 0) {
|
|
1870
|
+
ctx.addIssue({ code: "custom", path: ["tables", index, "indexes", position], message: `synced index '${name}' is also declared on '${owner}'` });
|
|
1871
|
+
}
|
|
1872
|
+
syncedIndexOwners.set(name, table.table);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1104
1875
|
if (table.source !== void 0) {
|
|
1105
1876
|
for (const [columnIndex, column] of table.columns.entries()) {
|
|
1106
1877
|
if (column.onUpdateFn !== void 0) {
|
|
@@ -1351,7 +2122,7 @@ function normalizeCanonical(definition) {
|
|
|
1351
2122
|
|
|
1352
2123
|
exports.CANONICAL_FORMAT = CANONICAL_FORMAT;
|
|
1353
2124
|
exports.CELL_PART_TYPES = CELL_PART_TYPES;
|
|
1354
|
-
exports.COLUMN_TYPES =
|
|
2125
|
+
exports.COLUMN_TYPES = COLUMN_TYPES2;
|
|
1355
2126
|
exports.COMPARATORS = COMPARATORS;
|
|
1356
2127
|
exports.IDENTIFIER_RE = IDENTIFIER_RE;
|
|
1357
2128
|
exports.RESERVED_RESOLVED_COLUMN_NAMES = RESERVED_RESOLVED_COLUMN_NAMES;
|