@sequenceholdings/orm 2.3.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
@@ -2177,6 +2177,7 @@ var Parser2 = class {
2177
2177
  return node;
2178
2178
  }
2179
2179
  if (this.peekKeyword("exists")) return this.parseExists();
2180
+ if (this.peekKeyword("visible") && this.tokens[this.index + 1]?.kind === "lparen") return this.parseVisible();
2180
2181
  return this.parseComparison();
2181
2182
  }
2182
2183
  parseBooleanCall(fn) {
@@ -2196,6 +2197,13 @@ var Parser2 = class {
2196
2197
  this.expect("rparen", "')' closing exists(...)");
2197
2198
  return { kind: "exists", path, where };
2198
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
+ }
2199
2207
  parseComparison() {
2200
2208
  const left = this.parseOperand();
2201
2209
  const operator = this.expect("operator", "a comparison operator");
@@ -2311,6 +2319,24 @@ function parsePolicy(source) {
2311
2319
 
2312
2320
  // src/predicate-v2/compile.ts
2313
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
+ }
2314
2340
  var CMP_BY_OP = {
2315
2341
  "=": "eq",
2316
2342
  "!=": "neq",
@@ -2344,7 +2370,7 @@ function resolveLink(info, ref) {
2344
2370
  function columnCandidates(info) {
2345
2371
  return [...info.columns.map((c) => c.name), ...info.links.map((l) => l.name)];
2346
2372
  }
2347
- var PolicyCompiler = class {
2373
+ var PolicyCompiler = class _PolicyCompiler {
2348
2374
  constructor(ctx) {
2349
2375
  this.ctx = ctx;
2350
2376
  }
@@ -2357,13 +2383,15 @@ var PolicyCompiler = class {
2357
2383
  return { kind: node.kind, operands: node.operands.map((operand) => this.compile(operand, table, depth)) };
2358
2384
  case "not":
2359
2385
  assertNoNegatedExternalClaim(node.operand);
2360
- return { kind: "not", operand: this.compile(node.operand, table, depth) };
2386
+ return { kind: "not", operand: new _PolicyCompiler({ ...this.ctx, negated: true }).compile(node.operand, table, depth) };
2361
2387
  case "capability":
2362
2388
  return { kind: "capability", key: node.key };
2363
2389
  case "role":
2364
2390
  return { kind: "role", key: node.key };
2365
2391
  case "exists":
2366
2392
  return this.compileExists(node.path, node.where, table, depth);
2393
+ case "visible":
2394
+ return this.compileVisible(node.path, table, depth);
2367
2395
  case "compare":
2368
2396
  return this.compileCompare(node, table, depth);
2369
2397
  }
@@ -2377,6 +2405,36 @@ var PolicyCompiler = class {
2377
2405
  }
2378
2406
  return pred;
2379
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
+ }
2380
2438
  resolveLinkPath(path, table, depth, { toOneOnly }) {
2381
2439
  const links = [];
2382
2440
  let current = table;
@@ -2437,6 +2495,7 @@ var PolicyCompiler = class {
2437
2495
  );
2438
2496
  }
2439
2497
  if (valueSide.kind === "claim" && isExternalPolicyClaimKey(valueSide.key)) {
2498
+ if (this.ctx.negated) throw negatedExternalClaim(valueSide.key);
2440
2499
  assertExternalClaimComparison({ op, key: valueSide.key, column: info.columns.find((c) => c.name === column) });
2441
2500
  }
2442
2501
  let cmp = this.buildCmp(op, column, valueSide);
@@ -2474,22 +2533,22 @@ function assertExternalClaimComparison({ op, key, column }) {
2474
2533
  );
2475
2534
  }
2476
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
+ }
2477
2542
  function assertNoNegatedExternalClaim(operand) {
2478
2543
  walkPolicy(operand, (node) => {
2479
2544
  if (node.kind !== "compare") return;
2480
2545
  const claim = [node.left, node.right].find((side) => side.kind === "claim" && isExternalPolicyClaimKey(side.key));
2481
- if (claim?.kind === "claim") {
2482
- throw new PolicyCompileError(
2483
- `claim('${claim.key}') cannot appear under not(): a negated borrower comparison admits other borrowers' rows`,
2484
- "unsupported-comparison"
2485
- );
2486
- }
2546
+ if (claim?.kind === "claim") throw negatedExternalClaim(claim.key);
2487
2547
  });
2488
2548
  }
2489
- function compilePredicateV2({ expression, table, resolver, maxLinkDepth }) {
2549
+ function compilePredicateAt({ expression, table, ctx, depth }) {
2490
2550
  const parsed = typeof expression === "string" ? parsePolicy(expression) : expression;
2491
- const compiler = new PolicyCompiler({ resolver, maxLinkDepth: maxLinkDepth ?? POLICY_MAX_LINK_DEPTH });
2492
- return compiler.compile(parsed.ast, table, 0);
2551
+ return new PolicyCompiler(ctx).compile(parsed.ast, table, depth);
2493
2552
  }
2494
2553
  function assertClaimsOnlyWhen(parsed) {
2495
2554
  walkPolicy(parsed.ast, (node) => {
@@ -2506,25 +2565,34 @@ function assertClaimsOnlyWhen(parsed) {
2506
2565
  "when-not-claims-only"
2507
2566
  );
2508
2567
  }
2509
- if (node.kind === "exists") {
2568
+ if (node.kind === "exists" || node.kind === "visible") {
2510
2569
  throw new PolicyCompileError(
2511
- "variant 'when' guards are claims-only \u2014 exists() is not allowed",
2570
+ `variant 'when' guards are claims-only \u2014 ${node.kind}() is not allowed`,
2512
2571
  "when-not-claims-only"
2513
2572
  );
2514
2573
  }
2515
2574
  });
2516
2575
  }
2517
- function compilePolicy({ expression, table, resolver, maxLinkDepth }) {
2518
- if (typeof expression === "string") {
2519
- return compilePredicateV2({ expression, table, resolver, maxLinkDepth });
2520
- }
2521
- 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 });
2522
2590
  if (expression.variants.length === 0) return defaultPred;
2523
2591
  const branches = expression.variants.map((variant) => {
2524
2592
  const parsedWhen = parsePolicy(variant.when);
2525
2593
  assertClaimsOnlyWhen(parsedWhen);
2526
- const when = compilePredicateV2({ expression: parsedWhen, table, resolver, maxLinkDepth });
2527
- 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 });
2528
2596
  return { kind: "and", operands: [when, grant] };
2529
2597
  });
2530
2598
  return { kind: "or", operands: [defaultPred, ...branches] };
@@ -2562,33 +2630,6 @@ function andPred(...preds) {
2562
2630
  if (operands.length === 1) return operands[0];
2563
2631
  return { kind: "and", operands };
2564
2632
  }
2565
- function walkPred(pred, visit2) {
2566
- visit2(pred);
2567
- switch (pred.kind) {
2568
- case "and":
2569
- case "or":
2570
- for (const operand of pred.operands) walkPred(operand, visit2);
2571
- return;
2572
- case "not":
2573
- walkPred(pred.operand, visit2);
2574
- return;
2575
- case "exists":
2576
- walkPred(pred.where, visit2);
2577
- return;
2578
- case "aggPredicate":
2579
- if (pred.where) walkPred(pred.where, visit2);
2580
- return;
2581
- default:
2582
- return;
2583
- }
2584
- }
2585
- function predContainsExists(pred) {
2586
- let found = false;
2587
- walkPred(pred, (node) => {
2588
- if (node.kind === "exists" || node.kind === "aggPredicate") found = true;
2589
- });
2590
- return found;
2591
- }
2592
2633
 
2593
2634
  // src/compiler-gql/inject.ts
2594
2635
  function collectPinnedColumns(writePolicy) {
@@ -2732,12 +2773,6 @@ var PolicyInjector = class {
2732
2773
  if (root.op === "resolvedWrite") return this.injectResolvedWrite(root, returning);
2733
2774
  if (root.op === "insert" || root.op === "insertOne") {
2734
2775
  const insertPolicy = this.insert(table);
2735
- if (predContainsExists(insertPolicy)) {
2736
- throw new DocumentCompileError(
2737
- `table '${table}' cannot be inserted into: v2.0 insert checks are claims + inserted-values only, but its insert policy traverses a link (exists)`,
2738
- "insert-check-unrepresentable"
2739
- );
2740
- }
2741
2776
  const pins = (this.ctx.pins.get(table) ?? []).map(
2742
2777
  (pin) => ({ column: pin.column, value: { kind: "claim", key: pin.claim } })
2743
2778
  );
@@ -2752,22 +2787,14 @@ var PolicyInjector = class {
2752
2787
  const updatePolicy = this.update(table);
2753
2788
  const where = andPred(updatePolicy, root.where ? this.injectDocPred(root.where, table) : void 0);
2754
2789
  if (root.op === "update" || root.op === "updateByPk") {
2755
- const check = predContainsExists(updatePolicy) && !this.ctx.composed ? {} : { insertCheck: updatePolicy };
2756
- return { ...root, where, ...check, returning };
2790
+ return { ...root, where, insertCheck: updatePolicy, returning };
2757
2791
  }
2758
2792
  if (root.op === "cowOverride") {
2759
2793
  const insertPolicy = this.insert(table);
2760
- if (predContainsExists(insertPolicy)) {
2761
- throw new DocumentCompileError(
2762
- `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)`,
2763
- "insert-check-unrepresentable"
2764
- );
2765
- }
2766
2794
  const pins = (this.ctx.pins.get(table) ?? []).map(
2767
2795
  (pin) => ({ column: pin.column, value: { kind: "claim", key: pin.claim } })
2768
2796
  );
2769
- const check = predContainsExists(updatePolicy) && !this.ctx.composed ? {} : { insertCheck: updatePolicy };
2770
- return { ...root, pinnedColumns: pins, cowCopyCheck: insertPolicy, where, ...check, returning };
2797
+ return { ...root, pinnedColumns: pins, cowCopyCheck: insertPolicy, where, insertCheck: updatePolicy, returning };
2771
2798
  }
2772
2799
  return { ...root, where, returning };
2773
2800
  }
@@ -3001,7 +3028,7 @@ function scopePolicy(pred, namespace) {
3001
3028
  switch (pred.kind) {
3002
3029
  case "capability":
3003
3030
  case "role":
3004
- return { ...pred, namespace };
3031
+ return pred.namespace ? pred : { ...pred, namespace };
3005
3032
  case "and":
3006
3033
  case "or":
3007
3034
  return { ...pred, operands: pred.operands.map((p) => scopePolicy(p, namespace)) };
@@ -3210,7 +3237,14 @@ function buildCompileContext(rawCanonical) {
3210
3237
  const updatePolicies = /* @__PURE__ */ new Map();
3211
3238
  const pins = /* @__PURE__ */ new Map();
3212
3239
  const splitWritePolicyTables = /* @__PURE__ */ new Set();
3213
- 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
+ };
3214
3248
  for (const table of relations) {
3215
3249
  const isView = viewNames.has(table.table);
3216
3250
  const compilePolicyFor = (access, store) => {
@@ -3219,7 +3253,7 @@ function buildCompileContext(rawCanonical) {
3219
3253
  return;
3220
3254
  }
3221
3255
  try {
3222
- const pred = compilePolicy({ expression, table: table.table, resolver });
3256
+ const pred = compilePolicy({ expression, table: table.table, resolver, access });
3223
3257
  store.set(table.table, normalized.imports?.length ? scopePolicy(pred, owners.get(table.table).namespace) : pred);
3224
3258
  } catch (error) {
3225
3259
  if (error instanceof PolicyCompileError || error instanceof PolicyParseError) {