@sequenceholdings/orm 2.2.0 → 2.4.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/dist/cli.js CHANGED
@@ -1426,14 +1426,20 @@ function requiredArtifactIrVersion(operations) {
1426
1426
  var CLAIMS_CONTRACT = {
1427
1427
  sub: "string",
1428
1428
  org: "string?",
1429
+ ext_subject: "string?",
1430
+ ext_email: "string?",
1429
1431
  roles: "string[]",
1430
1432
  capabilities: "string[]"
1431
1433
  };
1434
+ var EXTERNAL_POLICY_CLAIM_KEYS = ["ext_subject", "ext_email"];
1435
+ function isExternalPolicyClaimKey(key) {
1436
+ return EXTERNAL_POLICY_CLAIM_KEYS.includes(key);
1437
+ }
1432
1438
  var scalarLiteral = z.union([z.string(), z.number(), z.boolean(), z.null()]);
1433
1439
  var valueIrSchema = z.discriminatedUnion("kind", [
1434
1440
  z.object({ kind: z.literal("literal"), value: z.union([scalarLiteral, z.array(scalarLiteral)]) }),
1435
1441
  z.object({ kind: z.literal("variable"), name: z.string() }),
1436
- z.object({ kind: z.literal("claim"), key: z.enum(["sub", "org"]) })
1442
+ z.object({ kind: z.literal("claim"), key: z.enum(["sub", "org", ...EXTERNAL_POLICY_CLAIM_KEYS]) })
1437
1443
  ]);
1438
1444
  var CMP_OPS = [
1439
1445
  "eq",
@@ -2171,6 +2177,7 @@ var Parser2 = class {
2171
2177
  return node;
2172
2178
  }
2173
2179
  if (this.peekKeyword("exists")) return this.parseExists();
2180
+ if (this.peekKeyword("visible") && this.tokens[this.index + 1]?.kind === "lparen") return this.parseVisible();
2174
2181
  return this.parseComparison();
2175
2182
  }
2176
2183
  parseBooleanCall(fn) {
@@ -2190,6 +2197,13 @@ var Parser2 = class {
2190
2197
  this.expect("rparen", "')' closing exists(...)");
2191
2198
  return { kind: "exists", path, where };
2192
2199
  }
2200
+ parseVisible() {
2201
+ this.advance();
2202
+ this.expect("lparen", "'(' after visible");
2203
+ const path = this.parsePath("a link path in visible(...)");
2204
+ this.expect("rparen", "')' closing visible(...)");
2205
+ return { kind: "visible", path };
2206
+ }
2193
2207
  parseComparison() {
2194
2208
  const left = this.parseOperand();
2195
2209
  const operator = this.expect("operator", "a comparison operator");
@@ -2305,6 +2319,24 @@ function parsePolicy(source) {
2305
2319
 
2306
2320
  // src/predicate-v2/compile.ts
2307
2321
  var POLICY_MAX_LINK_DEPTH = 3;
2322
+ function scopeToNamespace(pred, namespace) {
2323
+ switch (pred.kind) {
2324
+ case "capability":
2325
+ case "role":
2326
+ return pred.namespace ? pred : { ...pred, namespace };
2327
+ case "and":
2328
+ case "or":
2329
+ return { ...pred, operands: pred.operands.map((operand) => scopeToNamespace(operand, namespace)) };
2330
+ case "not":
2331
+ return { ...pred, operand: scopeToNamespace(pred.operand, namespace) };
2332
+ case "exists":
2333
+ return { ...pred, where: scopeToNamespace(pred.where, namespace) };
2334
+ case "aggPredicate":
2335
+ return pred.where ? { ...pred, where: scopeToNamespace(pred.where, namespace) } : pred;
2336
+ default:
2337
+ return pred;
2338
+ }
2339
+ }
2308
2340
  var CMP_BY_OP = {
2309
2341
  "=": "eq",
2310
2342
  "!=": "neq",
@@ -2338,7 +2370,7 @@ function resolveLink(info, ref) {
2338
2370
  function columnCandidates(info) {
2339
2371
  return [...info.columns.map((c) => c.name), ...info.links.map((l) => l.name)];
2340
2372
  }
2341
- var PolicyCompiler = class {
2373
+ var PolicyCompiler = class _PolicyCompiler {
2342
2374
  constructor(ctx) {
2343
2375
  this.ctx = ctx;
2344
2376
  }
@@ -2350,13 +2382,16 @@ var PolicyCompiler = class {
2350
2382
  case "or":
2351
2383
  return { kind: node.kind, operands: node.operands.map((operand) => this.compile(operand, table, depth)) };
2352
2384
  case "not":
2353
- return { kind: "not", operand: this.compile(node.operand, table, depth) };
2385
+ assertNoNegatedExternalClaim(node.operand);
2386
+ return { kind: "not", operand: new _PolicyCompiler({ ...this.ctx, negated: true }).compile(node.operand, table, depth) };
2354
2387
  case "capability":
2355
2388
  return { kind: "capability", key: node.key };
2356
2389
  case "role":
2357
2390
  return { kind: "role", key: node.key };
2358
2391
  case "exists":
2359
2392
  return this.compileExists(node.path, node.where, table, depth);
2393
+ case "visible":
2394
+ return this.compileVisible(node.path, table, depth);
2360
2395
  case "compare":
2361
2396
  return this.compileCompare(node, table, depth);
2362
2397
  }
@@ -2370,6 +2405,36 @@ var PolicyCompiler = class {
2370
2405
  }
2371
2406
  return pred;
2372
2407
  }
2408
+ /** `visible(path)` → `exists(path, <final target's read policy>)`. */
2409
+ compileVisible(path, table, depth) {
2410
+ const { links, finalTable } = this.resolveLinkPath(path, table, depth, { toOneOnly: false });
2411
+ const readPolicy = this.ctx.resolver.readPolicy;
2412
+ if (!readPolicy) {
2413
+ throw new PolicyCompileError(
2414
+ `visible(${path.join(".")}) needs the target's read policy, which this compile context does not provide`,
2415
+ "visible-unresolvable"
2416
+ );
2417
+ }
2418
+ if (this.ctx.visiting.includes(finalTable)) {
2419
+ throw new PolicyCompileError(
2420
+ `visible(${path.join(".")}) on '${table}' reaches '${finalTable}' whose read policy is already being expanded (${[...this.ctx.visiting, finalTable].join(" \u2192 ")})`,
2421
+ "visible-cycle"
2422
+ );
2423
+ }
2424
+ const expression = readPolicy(finalTable);
2425
+ let pred = expression === void 0 ? { kind: "const", value: true } : compilePolicyAt({
2426
+ expression,
2427
+ table: finalTable,
2428
+ ctx: { ...this.ctx, visiting: [...this.ctx.visiting, finalTable] },
2429
+ depth: depth + links.length
2430
+ });
2431
+ const owner = this.ctx.resolver.policyNamespace?.(finalTable);
2432
+ if (owner) pred = scopeToNamespace(pred, owner);
2433
+ for (let i = links.length - 1; i >= 0; i -= 1) {
2434
+ pred = { kind: "exists", link: links[i].name, where: pred };
2435
+ }
2436
+ return pred;
2437
+ }
2373
2438
  resolveLinkPath(path, table, depth, { toOneOnly }) {
2374
2439
  const links = [];
2375
2440
  let current = table;
@@ -2429,6 +2494,10 @@ var PolicyCompiler = class {
2429
2494
  columnCandidates(info)
2430
2495
  );
2431
2496
  }
2497
+ if (valueSide.kind === "claim" && isExternalPolicyClaimKey(valueSide.key)) {
2498
+ if (this.ctx.negated) throw negatedExternalClaim(valueSide.key);
2499
+ assertExternalClaimComparison({ op, key: valueSide.key, column: info.columns.find((c) => c.name === column) });
2500
+ }
2432
2501
  let cmp = this.buildCmp(op, column, valueSide);
2433
2502
  for (let i = links.length - 1; i >= 0; i -= 1) {
2434
2503
  cmp = { kind: "exists", link: links[i].name, where: cmp };
@@ -2449,10 +2518,37 @@ var PolicyCompiler = class {
2449
2518
  return { kind: "cmp", op: CMP_BY_OP[op], column, value };
2450
2519
  }
2451
2520
  };
2452
- function compilePredicateV2({ expression, table, resolver, maxLinkDepth }) {
2521
+ function assertExternalClaimComparison({ op, key, column }) {
2522
+ if (op !== "=") {
2523
+ throw new PolicyCompileError(
2524
+ `claim('${key}') supports = comparisons only, got '${op}'`,
2525
+ "unsupported-comparison"
2526
+ );
2527
+ }
2528
+ if (column?.type !== "text") {
2529
+ const actual = column?.type === void 0 ? "of unknown type" : typeof column.type === "string" ? column.type : "an enum";
2530
+ throw new PolicyCompileError(
2531
+ `claim('${key}') is text and compares to a text column only \u2014 '${column?.name ?? "?"}' is ${actual}`,
2532
+ "claim-type-mismatch"
2533
+ );
2534
+ }
2535
+ }
2536
+ function negatedExternalClaim(key) {
2537
+ return new PolicyCompileError(
2538
+ `claim('${key}') cannot appear under not(): a negated borrower comparison admits other borrowers' rows`,
2539
+ "unsupported-comparison"
2540
+ );
2541
+ }
2542
+ function assertNoNegatedExternalClaim(operand) {
2543
+ walkPolicy(operand, (node) => {
2544
+ if (node.kind !== "compare") return;
2545
+ const claim = [node.left, node.right].find((side) => side.kind === "claim" && isExternalPolicyClaimKey(side.key));
2546
+ if (claim?.kind === "claim") throw negatedExternalClaim(claim.key);
2547
+ });
2548
+ }
2549
+ function compilePredicateAt({ expression, table, ctx, depth }) {
2453
2550
  const parsed = typeof expression === "string" ? parsePolicy(expression) : expression;
2454
- const compiler = new PolicyCompiler({ resolver, maxLinkDepth: maxLinkDepth ?? POLICY_MAX_LINK_DEPTH });
2455
- return compiler.compile(parsed.ast, table, 0);
2551
+ return new PolicyCompiler(ctx).compile(parsed.ast, table, depth);
2456
2552
  }
2457
2553
  function assertClaimsOnlyWhen(parsed) {
2458
2554
  walkPolicy(parsed.ast, (node) => {
@@ -2469,25 +2565,34 @@ function assertClaimsOnlyWhen(parsed) {
2469
2565
  "when-not-claims-only"
2470
2566
  );
2471
2567
  }
2472
- if (node.kind === "exists") {
2568
+ if (node.kind === "exists" || node.kind === "visible") {
2473
2569
  throw new PolicyCompileError(
2474
- "variant 'when' guards are claims-only \u2014 exists() is not allowed",
2570
+ `variant 'when' guards are claims-only \u2014 ${node.kind}() is not allowed`,
2475
2571
  "when-not-claims-only"
2476
2572
  );
2477
2573
  }
2478
2574
  });
2479
2575
  }
2480
- function compilePolicy({ expression, table, resolver, maxLinkDepth }) {
2481
- if (typeof expression === "string") {
2482
- return compilePredicateV2({ expression, table, resolver, maxLinkDepth });
2483
- }
2484
- const defaultPred = compilePredicateV2({ expression: expression.default, table, resolver, maxLinkDepth });
2576
+ function initialVisiting({ table, access }) {
2577
+ return (access ?? "read") === "read" ? [table] : [];
2578
+ }
2579
+ function compilePolicy({ expression, table, resolver, maxLinkDepth, access }) {
2580
+ return compilePolicyAt({
2581
+ expression,
2582
+ table,
2583
+ depth: 0,
2584
+ ctx: { resolver, maxLinkDepth: maxLinkDepth ?? POLICY_MAX_LINK_DEPTH, visiting: initialVisiting({ table, access }) }
2585
+ });
2586
+ }
2587
+ function compilePolicyAt({ expression, table, ctx, depth }) {
2588
+ if (typeof expression === "string") return compilePredicateAt({ expression, table, ctx, depth });
2589
+ const defaultPred = compilePredicateAt({ expression: expression.default, table, ctx, depth });
2485
2590
  if (expression.variants.length === 0) return defaultPred;
2486
2591
  const branches = expression.variants.map((variant) => {
2487
2592
  const parsedWhen = parsePolicy(variant.when);
2488
2593
  assertClaimsOnlyWhen(parsedWhen);
2489
- const when = compilePredicateV2({ expression: parsedWhen, table, resolver, maxLinkDepth });
2490
- const grant = variant.allow === true ? { kind: "const", value: true } : compilePredicateV2({ expression: variant.policy, table, resolver, maxLinkDepth });
2594
+ const when = compilePredicateAt({ expression: parsedWhen, table, ctx, depth });
2595
+ const grant = variant.allow === true ? { kind: "const", value: true } : compilePredicateAt({ expression: variant.policy, table, ctx, depth });
2491
2596
  return { kind: "and", operands: [when, grant] };
2492
2597
  });
2493
2598
  return { kind: "or", operands: [defaultPred, ...branches] };
@@ -2525,33 +2630,6 @@ function andPred(...preds) {
2525
2630
  if (operands.length === 1) return operands[0];
2526
2631
  return { kind: "and", operands };
2527
2632
  }
2528
- function walkPred(pred, visit2) {
2529
- visit2(pred);
2530
- switch (pred.kind) {
2531
- case "and":
2532
- case "or":
2533
- for (const operand of pred.operands) walkPred(operand, visit2);
2534
- return;
2535
- case "not":
2536
- walkPred(pred.operand, visit2);
2537
- return;
2538
- case "exists":
2539
- walkPred(pred.where, visit2);
2540
- return;
2541
- case "aggPredicate":
2542
- if (pred.where) walkPred(pred.where, visit2);
2543
- return;
2544
- default:
2545
- return;
2546
- }
2547
- }
2548
- function predContainsExists(pred) {
2549
- let found = false;
2550
- walkPred(pred, (node) => {
2551
- if (node.kind === "exists" || node.kind === "aggPredicate") found = true;
2552
- });
2553
- return found;
2554
- }
2555
2633
 
2556
2634
  // src/compiler-gql/inject.ts
2557
2635
  function collectPinnedColumns(writePolicy) {
@@ -2695,12 +2773,6 @@ var PolicyInjector = class {
2695
2773
  if (root.op === "resolvedWrite") return this.injectResolvedWrite(root, returning);
2696
2774
  if (root.op === "insert" || root.op === "insertOne") {
2697
2775
  const insertPolicy = this.insert(table);
2698
- if (predContainsExists(insertPolicy)) {
2699
- throw new DocumentCompileError(
2700
- `table '${table}' cannot be inserted into: v2.0 insert checks are claims + inserted-values only, but its insert policy traverses a link (exists)`,
2701
- "insert-check-unrepresentable"
2702
- );
2703
- }
2704
2776
  const pins = (this.ctx.pins.get(table) ?? []).map(
2705
2777
  (pin) => ({ column: pin.column, value: { kind: "claim", key: pin.claim } })
2706
2778
  );
@@ -2715,22 +2787,14 @@ var PolicyInjector = class {
2715
2787
  const updatePolicy = this.update(table);
2716
2788
  const where = andPred(updatePolicy, root.where ? this.injectDocPred(root.where, table) : void 0);
2717
2789
  if (root.op === "update" || root.op === "updateByPk") {
2718
- const check = predContainsExists(updatePolicy) && !this.ctx.composed ? {} : { insertCheck: updatePolicy };
2719
- return { ...root, where, ...check, returning };
2790
+ return { ...root, where, insertCheck: updatePolicy, returning };
2720
2791
  }
2721
2792
  if (root.op === "cowOverride") {
2722
2793
  const insertPolicy = this.insert(table);
2723
- if (predContainsExists(insertPolicy)) {
2724
- throw new DocumentCompileError(
2725
- `table '${table}' cannot be copy-on-write overridden: v2.0 insert checks are claims + inserted-values only, but its insert policy traverses a link (exists)`,
2726
- "insert-check-unrepresentable"
2727
- );
2728
- }
2729
2794
  const pins = (this.ctx.pins.get(table) ?? []).map(
2730
2795
  (pin) => ({ column: pin.column, value: { kind: "claim", key: pin.claim } })
2731
2796
  );
2732
- const check = predContainsExists(updatePolicy) && !this.ctx.composed ? {} : { insertCheck: updatePolicy };
2733
- return { ...root, pinnedColumns: pins, cowCopyCheck: insertPolicy, where, ...check, returning };
2797
+ return { ...root, pinnedColumns: pins, cowCopyCheck: insertPolicy, where, insertCheck: updatePolicy, returning };
2734
2798
  }
2735
2799
  return { ...root, where, returning };
2736
2800
  }
@@ -2964,7 +3028,7 @@ function scopePolicy(pred, namespace) {
2964
3028
  switch (pred.kind) {
2965
3029
  case "capability":
2966
3030
  case "role":
2967
- return { ...pred, namespace };
3031
+ return pred.namespace ? pred : { ...pred, namespace };
2968
3032
  case "and":
2969
3033
  case "or":
2970
3034
  return { ...pred, operands: pred.operands.map((p) => scopePolicy(p, namespace)) };
@@ -3173,7 +3237,14 @@ function buildCompileContext(rawCanonical) {
3173
3237
  const updatePolicies = /* @__PURE__ */ new Map();
3174
3238
  const pins = /* @__PURE__ */ new Map();
3175
3239
  const splitWritePolicyTables = /* @__PURE__ */ new Set();
3176
- const resolver = { tables };
3240
+ const readPolicyExpressions = new Map(relations.map((relation) => [relation.table, relation.policies.read]));
3241
+ const resolver = {
3242
+ tables,
3243
+ readPolicy: (table) => readPolicyExpressions.get(table),
3244
+ // In a composed namespace, an imported table's read policy (inlined by
3245
+ // visible()) is checked against the namespace that owns it.
3246
+ ...normalized.imports?.length ? { policyNamespace: (table) => owners.get(table)?.namespace } : {}
3247
+ };
3177
3248
  for (const table of relations) {
3178
3249
  const isView = viewNames.has(table.table);
3179
3250
  const compilePolicyFor = (access, store) => {
@@ -3182,7 +3253,7 @@ function buildCompileContext(rawCanonical) {
3182
3253
  return;
3183
3254
  }
3184
3255
  try {
3185
- const pred = compilePolicy({ expression, table: table.table, resolver });
3256
+ const pred = compilePolicy({ expression, table: table.table, resolver, access });
3186
3257
  store.set(table.table, normalized.imports?.length ? scopePolicy(pred, owners.get(table.table).namespace) : pred);
3187
3258
  } catch (error) {
3188
3259
  if (error instanceof PolicyCompileError || error instanceof PolicyParseError) {