@sequenceholdings/orm 2.6.1 → 2.7.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.
Files changed (48) hide show
  1. package/README.md +76 -2
  2. package/dist/authoring.d.cts +1 -1
  3. package/dist/authoring.d.ts +1 -1
  4. package/dist/canonical.d.cts +71 -71
  5. package/dist/canonical.d.ts +71 -71
  6. package/dist/cli.cjs +81 -29
  7. package/dist/cli.cjs.map +1 -1
  8. package/dist/cli.js +81 -29
  9. package/dist/cli.js.map +1 -1
  10. package/dist/client.cjs +79 -13
  11. package/dist/client.cjs.map +1 -1
  12. package/dist/client.d.cts +26 -4
  13. package/dist/client.d.ts +26 -4
  14. package/dist/client.js +79 -13
  15. package/dist/client.js.map +1 -1
  16. package/dist/compiler.cjs +68 -29
  17. package/dist/compiler.cjs.map +1 -1
  18. package/dist/compiler.d.cts +1 -1
  19. package/dist/compiler.d.ts +1 -1
  20. package/dist/compiler.js +68 -29
  21. package/dist/compiler.js.map +1 -1
  22. package/dist/index.cjs +73 -4
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +63 -61
  25. package/dist/index.d.ts +63 -61
  26. package/dist/index.js +72 -5
  27. package/dist/index.js.map +1 -1
  28. package/dist/ir.cjs +19 -3
  29. package/dist/ir.cjs.map +1 -1
  30. package/dist/ir.d.cts +83 -76
  31. package/dist/ir.d.ts +83 -76
  32. package/dist/ir.js +18 -4
  33. package/dist/ir.js.map +1 -1
  34. package/dist/predicate-v2.cjs +24 -2
  35. package/dist/predicate-v2.cjs.map +1 -1
  36. package/dist/predicate-v2.d.cts +4 -0
  37. package/dist/predicate-v2.d.ts +4 -0
  38. package/dist/predicate-v2.js +24 -2
  39. package/dist/predicate-v2.js.map +1 -1
  40. package/dist/sdl.cjs +16 -2
  41. package/dist/sdl.cjs.map +1 -1
  42. package/dist/sdl.js +16 -2
  43. package/dist/sdl.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/codegen-gql/preset.cjs +45 -8
  46. package/templates/orm-namespace/codegen.ts +5 -0
  47. package/dist/{definition-B99BsOAO.d.cts → definition-DjfC7Lll.d.cts} +4 -4
  48. package/dist/{definition-B99BsOAO.d.ts → definition-DjfC7Lll.d.ts} +4 -4
package/dist/compiler.cjs CHANGED
@@ -1314,14 +1314,21 @@ function normalizeCanonical(definition) {
1314
1314
  var BUILTIN_SQL_READ_HASH = "sha256:a50c477c952a5e0fe68ed158a4530e4aab573c09bcbb1a8ac454dc88a39a6258";
1315
1315
 
1316
1316
  // src/ir/index.ts
1317
- var IR_VERSION = 5;
1317
+ var IR_VERSION = 6;
1318
1318
  var LEGACY_IR_VERSION = 1;
1319
1319
  var GUARDED_IR_VERSION = 2;
1320
1320
  var COMPOSED_IR_VERSION = 3;
1321
1321
  var SQL_IR_VERSION = 4;
1322
1322
  var RUNTIME_SQL_IR_VERSION = 5;
1323
- var irVersionSchema = zod.z.union([zod.z.literal(LEGACY_IR_VERSION), zod.z.literal(GUARDED_IR_VERSION), zod.z.literal(COMPOSED_IR_VERSION), zod.z.literal(SQL_IR_VERSION), zod.z.literal(RUNTIME_SQL_IR_VERSION)]);
1323
+ var SERVICE_PRINCIPAL_IR_VERSION = 6;
1324
+ var irVersionSchema = zod.z.union([zod.z.literal(LEGACY_IR_VERSION), zod.z.literal(GUARDED_IR_VERSION), zod.z.literal(COMPOSED_IR_VERSION), zod.z.literal(SQL_IR_VERSION), zod.z.literal(RUNTIME_SQL_IR_VERSION), zod.z.literal(SERVICE_PRINCIPAL_IR_VERSION)]);
1324
1325
  var operationVersionSchema = zod.z.object({ irVersion: irVersionSchema });
1326
+ function usesServicePrincipal(value) {
1327
+ if (value === null || typeof value !== "object") return false;
1328
+ if (Array.isArray(value)) return value.some(usesServicePrincipal);
1329
+ const row = value;
1330
+ return row.kind === "servicePrincipal" || Object.values(row).some(usesServicePrincipal);
1331
+ }
1325
1332
  function requiredArtifactIrVersion(operations) {
1326
1333
  let required = LEGACY_IR_VERSION;
1327
1334
  for (const operation of operations) {
@@ -1381,6 +1388,7 @@ var predIrSchema = zod.z.lazy(
1381
1388
  zod.z.object({ kind: zod.z.literal("capability"), key: zod.z.string(), namespace: zod.z.string().optional() }),
1382
1389
  zod.z.object({ kind: zod.z.literal("role"), key: zod.z.string(), namespace: zod.z.string().optional() }),
1383
1390
  zod.z.object({ kind: zod.z.literal("dynamicFilter"), slot: zod.z.string() }),
1391
+ zod.z.object({ kind: zod.z.literal("servicePrincipal") }).strict(),
1384
1392
  zod.z.object({ kind: zod.z.literal("const"), value: zod.z.boolean() })
1385
1393
  ])
1386
1394
  );
@@ -1606,7 +1614,7 @@ var variableIrSchema = zod.z.object({
1606
1614
  /** Absent = no default declared; see {@link variableDefaultIrSchema}. */
1607
1615
  defaultValue: variableDefaultIrSchema.optional()
1608
1616
  });
1609
- var sqlColumnSchema = zod.z.object({ name: zod.z.string(), type: zod.z.enum(["text", "boolean", "integer", "bigint", "numeric", "double", "uuid", "date", "timestamptz"]), nullable: zod.z.boolean() });
1617
+ var sqlColumnSchema = zod.z.object({ name: zod.z.string(), type: zod.z.enum(["text", "boolean", "integer", "bigint", "numeric", "double", "uuid", "date", "timestamptz", "jsonb"]), nullable: zod.z.boolean() });
1610
1618
  var compiledOperationSchema = zod.z.object({
1611
1619
  language: zod.z.literal("sql").optional(),
1612
1620
  /** Server-compiled object catalog and permission ceiling, never caller SQL. */
@@ -1644,6 +1652,9 @@ var compiledOperationSchema = zod.z.object({
1644
1652
  tableSet: zod.z.array(zod.z.string()).min(1),
1645
1653
  roots: zod.z.array(rootIrSchema)
1646
1654
  }).superRefine((operation, ctx) => {
1655
+ if (operation.irVersion < SERVICE_PRINCIPAL_IR_VERSION && usesServicePrincipal(operation.roots)) {
1656
+ ctx.addIssue({ code: "custom", message: "servicePrincipal() requires IR v6" });
1657
+ }
1647
1658
  if (operation.language === "sql") {
1648
1659
  const validPlan = operation.sqlQuery ? !operation.sql && operation.irVersion === RUNTIME_SQL_IR_VERSION : !!operation.sql && operation.irVersion === SQL_IR_VERSION;
1649
1660
  const builtin = operation.name === "BuiltinSqlRead" && operation.opHash === BUILTIN_SQL_READ_HASH && operation.requires === null;
@@ -1776,6 +1787,9 @@ var compiledArtifactSchema = zod.z.object({
1776
1787
  tables: zod.z.record(zod.z.string(), tableMetaIrSchema),
1777
1788
  operations: zod.z.record(zod.z.string(), compiledOperationSchema)
1778
1789
  }).superRefine((artifact, ctx) => {
1790
+ if (artifact.irVersion < SERVICE_PRINCIPAL_IR_VERSION && usesServicePrincipal(artifact)) {
1791
+ ctx.addIssue({ code: "custom", message: "servicePrincipal() requires IR v6" });
1792
+ }
1779
1793
  if (Object.keys(artifact.dependencies ?? {}).length > 0 && artifact.irVersion < COMPOSED_IR_VERSION) {
1780
1794
  ctx.addIssue({ code: "custom", path: ["irVersion"], message: "cross-namespace dependencies require IR v3" });
1781
1795
  }
@@ -2066,6 +2080,12 @@ var Parser2 = class {
2066
2080
  }
2067
2081
  return node;
2068
2082
  }
2083
+ if (this.peekKeyword("servicePrincipal") && this.tokens[this.index + 1]?.kind === "lparen") {
2084
+ this.advance();
2085
+ this.expect("lparen", "'(' after servicePrincipal");
2086
+ this.expect("rparen", "')' closing servicePrincipal()");
2087
+ return { kind: "servicePrincipal" };
2088
+ }
2069
2089
  if (this.peekKeyword("exists")) return this.parseExists();
2070
2090
  if (this.peekKeyword("visible") && this.tokens[this.index + 1]?.kind === "lparen") return this.parseVisible();
2071
2091
  return this.parseComparison();
@@ -2278,6 +2298,8 @@ var PolicyCompiler = class _PolicyCompiler {
2278
2298
  return { kind: "capability", key: node.key };
2279
2299
  case "role":
2280
2300
  return { kind: "role", key: node.key };
2301
+ case "servicePrincipal":
2302
+ return { kind: "servicePrincipal" };
2281
2303
  case "exists":
2282
2304
  return this.compileExists(node.path, node.where, table, depth);
2283
2305
  case "visible":
@@ -2973,7 +2995,10 @@ function platformUserMeta() {
2973
2995
  { name: "role", columnName: "role", type: "text", nullable: true },
2974
2996
  { name: "managerId", columnName: "manager_id", type: "text", nullable: true },
2975
2997
  // NOT NULL DEFAULT true on chat.users, so non-null through the view.
2976
- { name: "accountEnabled", columnName: "account_enabled", type: "boolean", nullable: false }
2998
+ { name: "accountEnabled", columnName: "account_enabled", type: "boolean", nullable: false },
2999
+ { name: "canonicalSub", columnName: "canonical_sub", type: "text", nullable: false },
3000
+ { name: "normalizedEmail", columnName: "normalized_email", type: "text", nullable: true },
3001
+ { name: "emailDomain", columnName: "email_domain", type: "text", nullable: true }
2977
3002
  ];
2978
3003
  return {
2979
3004
  table: "users_directory",
@@ -4960,7 +4985,7 @@ function compileNamespace({ canonical, sdl, documents }) {
4960
4985
  }
4961
4986
  }
4962
4987
  operations[restricted.name] = {
4963
- irVersion: canonical.imports?.length ? COMPOSED_IR_VERSION : roots.some((root) => root.kind === "mutation" && root.assertAffectedRows !== void 0) ? GUARDED_IR_VERSION : LEGACY_IR_VERSION,
4988
+ irVersion: usesServicePrincipal(roots) ? SERVICE_PRINCIPAL_IR_VERSION : canonical.imports?.length ? COMPOSED_IR_VERSION : roots.some((root) => root.kind === "mutation" && root.assertAffectedRows !== void 0) ? GUARDED_IR_VERSION : LEGACY_IR_VERSION,
4964
4989
  name: restricted.name,
4965
4990
  opHash: computeOpHash({ definition: flattened, schemaHash }),
4966
4991
  kind: restricted.kind,
@@ -4987,7 +5012,7 @@ function compileNamespace({ canonical, sdl, documents }) {
4987
5012
  }
4988
5013
  if (diagnostics.some((d) => d.severity === "error")) return { ok: false, diagnostics };
4989
5014
  const artifact = {
4990
- irVersion: canonical.imports?.length ? COMPOSED_IR_VERSION : requiredArtifactIrVersion(Object.values(operations)),
5015
+ irVersion: usesServicePrincipal([...ctx.readPolicies.values(), ...ctx.writePolicies.values(), ...ctx.insertPolicies.values(), ...ctx.updatePolicies.values(), ...Object.values(operations)]) ? SERVICE_PRINCIPAL_IR_VERSION : canonical.imports?.length ? COMPOSED_IR_VERSION : requiredArtifactIrVersion(Object.values(operations)),
4991
5016
  namespace: ctx.namespace,
4992
5017
  schemaHash,
4993
5018
  ownSchemaHash: computeSchemaHash(localContract(canonical)),
@@ -5424,6 +5449,8 @@ function compilePredIrToSql(pred, options = {}) {
5424
5449
  return `(NOT ${compilePredIrToSql(pred.operand, options)})`;
5425
5450
  case "cmp":
5426
5451
  return compileCmp(pred.op, pred.column, pred.value, options);
5452
+ case "servicePrincipal":
5453
+ return `coalesce(${claimExpression("servicePrincipal")} = 'true', false)`;
5427
5454
  case "capability":
5428
5455
  return `(${options.statementClaimChecks ? "SELECT " : ""}${capabilityExpression(pred.key, pred.namespace ?? options.namespace)})`;
5429
5456
  case "role":
@@ -5526,6 +5553,39 @@ function compileArrayValue(value, columnType, _options) {
5526
5553
  return columnType ? `${array}::${sqlTypeName(columnType)}[]` : array;
5527
5554
  }
5528
5555
 
5556
+ // src/compiler/static-policy-sql.ts
5557
+ function compileStaticPolicySql({ table, predicate, tables, namespace, alias, rootRelation, statementClaimChecks, onRead }) {
5558
+ let sequence = 0;
5559
+ const aliases = new Set([alias, rootRelation].filter((value) => value !== void 0));
5560
+ const column = (meta, qualifier, name2) => {
5561
+ const found = meta.columns.find((c) => c.name === name2 || c.columnName === name2);
5562
+ if (!found) throw new OrmDefinitionError("Static policy relationship column is unavailable");
5563
+ return `${quoteIdentifier(qualifier)}.${quoteIdentifier(found.columnName)}`;
5564
+ };
5565
+ const render = (meta, pred, qualifier, outerRelation) => compilePredIrToSql(pred, {
5566
+ alias: qualifier,
5567
+ namespace: meta.namespace ?? namespace,
5568
+ statementClaimChecks,
5569
+ columnNames: Object.fromEntries(meta.columns.map((c) => [c.name, c.columnName])),
5570
+ columnTypes: Object.fromEntries(meta.columns.map((c) => [c.name, c.type])),
5571
+ exists: (node) => {
5572
+ const link = meta.links.find((candidate) => candidate.name === node.link);
5573
+ const target = link?.target ? tables[link.target] : void 0;
5574
+ const parent = qualifier ?? outerRelation;
5575
+ if (!link || !target || !parent) throw new OrmDefinitionError("Static policy relationship metadata is unavailable");
5576
+ onRead?.(target);
5577
+ let child;
5578
+ do {
5579
+ child = `policy_${++sequence}`;
5580
+ } while (aliases.has(child));
5581
+ aliases.add(child);
5582
+ const join = link.kind === "toMany" ? `${column(target, child, link.on)} = ${column(meta, parent, link.references)}` : `${column(meta, parent, link.on)} = ${column(target, child, link.references)}`;
5583
+ return `EXISTS (SELECT 1 FROM ${quoteIdentifier(target.schema)}.${quoteIdentifier(target.table)} AS ${quoteIdentifier(child)} WHERE ${join} AND (${render(target, node.where, child)}))`;
5584
+ }
5585
+ });
5586
+ return render(table, predicate, alias, rootRelation);
5587
+ }
5588
+
5529
5589
  // src/sql/catalog.ts
5530
5590
  function sqlHash(value) {
5531
5591
  return `sha256:${crypto.createHash("sha256").update(value).digest("hex")}`;
@@ -5546,27 +5606,6 @@ function createSqlCatalog({ canonical, publishedObjects, executionTarget }) {
5546
5606
  const meta = Object.values(ctx.tables).find((table) => refOf(table) === ref);
5547
5607
  if (!meta || internal.has(ref) || meta.namespace !== canonical.namespace && !meta.exportRead) throw new Error(`SQL object not published: ${ref}`);
5548
5608
  const reads = /* @__PURE__ */ new Set([ref]);
5549
- let sequence = 0;
5550
- const predicate = (table, pred, alias) => compilePredIrToSql(pred, {
5551
- alias,
5552
- namespace: table.namespace ?? canonical.namespace,
5553
- columnNames: Object.fromEntries(table.columns.map((c) => [c.name, c.columnName])),
5554
- columnTypes: Object.fromEntries(table.columns.map((c) => [c.name, c.type])),
5555
- exists: (node) => {
5556
- const link = table.links.find((candidate) => candidate.name === node.link);
5557
- const target = link?.target ? ctx.tables[link.target] : void 0;
5558
- if (!link || !target) throw new Error("SQL policy link is unavailable");
5559
- reads.add(refOf(target));
5560
- const child = `p${++sequence}`;
5561
- const col = (t, a, name2) => {
5562
- const c = t.columns.find((c2) => c2.name === name2 || c2.columnName === name2);
5563
- if (!c) throw new Error("SQL policy column is unavailable");
5564
- return `${quoteIdentifier(a)}.${quoteIdentifier(c.columnName)}`;
5565
- };
5566
- const join = link.kind === "toMany" ? `${col(target, child, link.on)} = ${col(table, alias, link.references)}` : `${col(table, alias, link.on)} = ${col(target, child, link.references)}`;
5567
- return `EXISTS (SELECT 1 FROM ${quoteIdentifier(target.schema)}.${quoteIdentifier(target.table)} AS ${quoteIdentifier(child)} WHERE ${join} AND (${predicate(target, node.where, child)}))`;
5568
- }
5569
- });
5570
5609
  const columns = meta.columns.filter((c) => meta.filterable.includes(c.name)).map((c) => ({
5571
5610
  name: c.name,
5572
5611
  type: typeof c.type === "string" ? c.type : "text",
@@ -5577,7 +5616,7 @@ function createSqlCatalog({ canonical, publishedObjects, executionTarget }) {
5577
5616
  return `${quoteIdentifier("p0")}.${quoteIdentifier(physical.columnName)}${typeof physical.type === "object" ? "::text" : ""} AS ${quoteIdentifier(c.name)}`;
5578
5617
  }).join(", ");
5579
5618
  const policy = ctx.readPolicies.get(meta.table) ?? { kind: "const", value: true };
5580
- const scan = `SELECT ${projection} FROM ${quoteIdentifier(meta.schema)}.${quoteIdentifier(meta.table)} AS ${quoteIdentifier("p0")} WHERE ${predicate(meta, policy, "p0")}`;
5619
+ const scan = `SELECT ${projection} FROM ${quoteIdentifier(meta.schema)}.${quoteIdentifier(meta.table)} AS ${quoteIdentifier("p0")} WHERE ${compileStaticPolicySql({ table: meta, predicate: policy, tables: ctx.tables, namespace: canonical.namespace, alias: "p0", onRead: (target) => reads.add(refOf(target)) })}`;
5581
5620
  return { ref, table: meta.table, columns, scan, read: [...reads].sort() };
5582
5621
  });
5583
5622
  return { identity, objects };
@@ -6370,7 +6409,7 @@ function compileDatasetPreviews(input) {
6370
6409
  }
6371
6410
  const hash = crypto.createHash("sha256").update("sequence-orm-dataset-preview-v1\0").update(stableJson({ schemaHash, roots, tableSet })).digest("hex");
6372
6411
  operations[relation.table] = compiledOperationSchema.parse({
6373
- irVersion: canonical.imports?.length ? COMPOSED_IR_VERSION : LEGACY_IR_VERSION,
6412
+ irVersion: usesServicePrincipal(roots) ? SERVICE_PRINCIPAL_IR_VERSION : canonical.imports?.length ? COMPOSED_IR_VERSION : LEGACY_IR_VERSION,
6374
6413
  name: `DatasetPreview_${relation.table}`,
6375
6414
  opHash: `sha256:${hash}`,
6376
6415
  kind: "query",