@sequenceholdings/orm 2.1.1 → 2.3.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 (68) hide show
  1. package/README.md +3 -1
  2. package/dist/authoring.cjs +284 -99
  3. package/dist/authoring.cjs.map +1 -1
  4. package/dist/authoring.d.cts +31 -8
  5. package/dist/authoring.d.ts +31 -8
  6. package/dist/authoring.js +282 -100
  7. package/dist/authoring.js.map +1 -1
  8. package/dist/canonical.cjs +51 -3
  9. package/dist/canonical.cjs.map +1 -1
  10. package/dist/canonical.d.cts +838 -12
  11. package/dist/canonical.d.ts +838 -12
  12. package/dist/canonical.js +47 -4
  13. package/dist/canonical.js.map +1 -1
  14. package/dist/cli.cjs +5876 -4198
  15. package/dist/cli.cjs.map +1 -1
  16. package/dist/cli.js +5876 -4198
  17. package/dist/cli.js.map +1 -1
  18. package/dist/client.cjs +49 -32
  19. package/dist/client.cjs.map +1 -1
  20. package/dist/client.d.cts +49 -6
  21. package/dist/client.d.ts +49 -6
  22. package/dist/client.js +49 -32
  23. package/dist/client.js.map +1 -1
  24. package/dist/compiler.cjs +1275 -264
  25. package/dist/compiler.cjs.map +1 -1
  26. package/dist/compiler.d.cts +57 -3
  27. package/dist/compiler.d.ts +57 -3
  28. package/dist/compiler.js +1268 -265
  29. package/dist/compiler.js.map +1 -1
  30. package/dist/{constants-YoFGjIBM.d.cts → constants-C6EG6Wq7.d.cts} +1 -1
  31. package/dist/{constants-YoFGjIBM.d.ts → constants-C6EG6Wq7.d.ts} +1 -1
  32. package/dist/definition-DjfC7Lll.d.cts +91 -0
  33. package/dist/definition-DjfC7Lll.d.ts +91 -0
  34. package/dist/dynamic-input.cjs +2 -1
  35. package/dist/dynamic-input.cjs.map +1 -1
  36. package/dist/dynamic-input.d.cts +9 -2
  37. package/dist/dynamic-input.d.ts +9 -2
  38. package/dist/dynamic-input.js +2 -1
  39. package/dist/dynamic-input.js.map +1 -1
  40. package/dist/index.cjs +1103 -322
  41. package/dist/index.cjs.map +1 -1
  42. package/dist/index.d.cts +479 -19
  43. package/dist/index.d.ts +479 -19
  44. package/dist/index.js +1090 -323
  45. package/dist/index.js.map +1 -1
  46. package/dist/ir.cjs +93 -11
  47. package/dist/ir.cjs.map +1 -1
  48. package/dist/ir.d.cts +226 -15
  49. package/dist/ir.d.ts +226 -15
  50. package/dist/ir.js +88 -12
  51. package/dist/ir.js.map +1 -1
  52. package/dist/predicate-v2.cjs +116 -10
  53. package/dist/predicate-v2.cjs.map +1 -1
  54. package/dist/predicate-v2.d.cts +11 -3
  55. package/dist/predicate-v2.d.ts +11 -3
  56. package/dist/predicate-v2.js +116 -10
  57. package/dist/predicate-v2.js.map +1 -1
  58. package/dist/sdl.cjs +158 -22
  59. package/dist/sdl.cjs.map +1 -1
  60. package/dist/sdl.d.cts +8 -1
  61. package/dist/sdl.d.ts +8 -1
  62. package/dist/sdl.js +158 -22
  63. package/dist/sdl.js.map +1 -1
  64. package/package.json +5 -4
  65. package/src/codegen-gql/server-fns.cjs +2 -2
  66. package/templates/orm-namespace/AGENTS.md +10 -0
  67. package/templates/orm-namespace/CLAUDE.md +1 -84
  68. package/templates/orm-namespace/package.json +1 -1
package/dist/ir.js CHANGED
@@ -203,6 +203,7 @@ function compileCheckExpression(source, columns) {
203
203
  // src/constants.ts
204
204
  var COLUMN_IDENTIFIER_PATTERN = /^_?[a-z][a-z0-9_]*$/;
205
205
  var MAX_IDENTIFIER_LENGTH = 63;
206
+ var QUALIFIED_TABLE_PATTERN = /^[a-z][a-z0-9_]{0,62}\.[a-z][a-z0-9_]{0,62}$/;
206
207
  var CELL_PARTS = ["at", "by", "rev", "source", "conflicts"];
207
208
  var CELL_PART_SUFFIX = {
208
209
  at: "__at",
@@ -535,6 +536,8 @@ var canonicalLinkSchema = z.object({
535
536
  kind: z.enum(["toOne", "toMany", "toPlatformUser"]),
536
537
  /** Target table name (namespace-local). Absent for toPlatformUser. */
537
538
  target: identifier2.optional(),
539
+ /** Owning namespace of an imported target; omitted for local links. */
540
+ namespace: identifier2.max(41).optional(),
538
541
  /** FK column on the owning side (toOne/toPlatformUser: on this table; toMany: on the target). */
539
542
  on: columnIdentifier,
540
543
  /** Referenced column on the target (default: target's PK / platform user `sub`). */
@@ -636,11 +639,12 @@ var canonicalForeignKeySchema = z.object({
636
639
  }).strict();
637
640
  var canonicalTableSchema = z.object({
638
641
  table: identifier2,
642
+ displayName: z.string().trim().min(1).max(120).optional(),
639
643
  description: z.string().max(2e3).optional(),
640
644
  /** Table-level rename hint (new in v2). */
641
645
  renamedFrom: identifier2.optional(),
642
- /** Synced/reflected read-only tables (`source: gold.*`) — v1 semantics. */
643
- source: z.string().regex(/^gold\.[a-z][a-z0-9_]*$/).optional(),
646
+ /** Synced/reflected read-only tables (`source: schema.table`) — v1 semantics. */
647
+ source: z.string().regex(QUALIFIED_TABLE_PATTERN).optional(),
644
648
  columns: z.array(canonicalColumnSchema).default([]),
645
649
  primaryKey: z.array(columnIdentifier).min(1).max(8).optional(),
646
650
  links: z.array(canonicalLinkSchema).default([]),
@@ -793,7 +797,9 @@ function validateCellMetadata(table, index, ctx) {
793
797
  }
794
798
  }
795
799
  }
796
- z.object({
800
+ var exportGateSchema = z.union([z.literal(true), z.object({ capability: z.string().min(1) }).strict(), z.object({ role: z.string().min(1) }).strict()]);
801
+ var relationExportSchema = z.object({ read: exportGateSchema, write: exportGateSchema.optional() }).strict();
802
+ var localDefinitionSchema = z.object({
797
803
  definition_format: z.literal(CANONICAL_FORMAT),
798
804
  namespace: identifier2.max(41),
799
805
  description: z.string().max(2e3).optional(),
@@ -801,6 +807,8 @@ z.object({
801
807
  tables: z.array(canonicalTableSchema).min(0).max(500),
802
808
  /** Optional to preserve byte-identical hashes for pre-view canonical v2 documents. */
803
809
  views: z.array(canonicalViewSchema).max(500).optional(),
810
+ /** Target-owned cross-namespace access contract, using existing namespace grants. */
811
+ exports: z.record(identifier2, relationExportSchema).optional(),
804
812
  /** Verbatim custom-roles grammar (@sequenceholdings/custom-roles-manifest). */
805
813
  capabilities: z.object({
806
814
  roles: z.unknown().optional(),
@@ -827,7 +835,16 @@ z.object({
827
835
  }
828
836
  }
829
837
  const fkTables = new Map(definition.tables.map((table) => [table.table, table]));
838
+ const relations = /* @__PURE__ */ new Set([...fkTables.keys(), ...(definition.views ?? []).map((view) => view.view)]);
839
+ for (const name of Object.keys(definition.exports ?? {})) {
840
+ if (!relations.has(name)) ctx.addIssue({ code: "custom", path: ["exports", name], message: `export '${name}' is not a local object` });
841
+ }
830
842
  for (const [tableIndex, table] of definition.tables.entries()) {
843
+ for (const [linkIndex, link] of table.links.entries()) {
844
+ if (link.enforce && link.namespace !== void 0 && link.namespace !== definition.namespace) {
845
+ ctx.addIssue({ code: "custom", path: ["tables", tableIndex, "links", linkIndex], message: "physical foreign keys require a local target; use an ORM link without enforce" });
846
+ }
847
+ }
831
848
  const names = /* @__PURE__ */ new Set([
832
849
  ...table.primaryKey?.length ? [`${table.table}_pkey`] : [],
833
850
  ...table.columns.filter((column) => column.unique && !(table.primaryKey?.length === 1 && table.primaryKey[0] === column.columnName)).map((column) => `${table.table}_${column.columnName}_unique`),
@@ -997,11 +1014,19 @@ z.object({
997
1014
  };
998
1015
  for (const view of views) visit(view.view, []);
999
1016
  });
1017
+ localDefinitionSchema.safeExtend({
1018
+ /** Flattened, pinned snapshots. Imported relations never participate in this namespace's DDL. */
1019
+ imports: z.array(localDefinitionSchema).max(32).optional()
1020
+ });
1000
1021
 
1001
1022
  // src/ir/index.ts
1002
- var IR_VERSION = 2;
1023
+ var IR_VERSION = 5;
1003
1024
  var LEGACY_IR_VERSION = 1;
1004
- var irVersionSchema = z.union([z.literal(LEGACY_IR_VERSION), z.literal(IR_VERSION)]);
1025
+ var GUARDED_IR_VERSION = 2;
1026
+ var COMPOSED_IR_VERSION = 3;
1027
+ var SQL_IR_VERSION = 4;
1028
+ var RUNTIME_SQL_IR_VERSION = 5;
1029
+ var irVersionSchema = z.union([z.literal(LEGACY_IR_VERSION), z.literal(GUARDED_IR_VERSION), z.literal(COMPOSED_IR_VERSION), z.literal(SQL_IR_VERSION), z.literal(RUNTIME_SQL_IR_VERSION)]);
1005
1030
  var operationVersionSchema = z.object({ irVersion: irVersionSchema });
1006
1031
  function requiredArtifactIrVersion(operations) {
1007
1032
  let required = LEGACY_IR_VERSION;
@@ -1014,14 +1039,20 @@ function requiredArtifactIrVersion(operations) {
1014
1039
  var CLAIMS_CONTRACT = {
1015
1040
  sub: "string",
1016
1041
  org: "string?",
1042
+ ext_subject: "string?",
1043
+ ext_email: "string?",
1017
1044
  roles: "string[]",
1018
1045
  capabilities: "string[]"
1019
1046
  };
1047
+ var EXTERNAL_POLICY_CLAIM_KEYS = ["ext_subject", "ext_email"];
1048
+ function isExternalPolicyClaimKey(key) {
1049
+ return EXTERNAL_POLICY_CLAIM_KEYS.includes(key);
1050
+ }
1020
1051
  var scalarLiteral = z.union([z.string(), z.number(), z.boolean(), z.null()]);
1021
1052
  var valueIrSchema = z.discriminatedUnion("kind", [
1022
1053
  z.object({ kind: z.literal("literal"), value: z.union([scalarLiteral, z.array(scalarLiteral)]) }),
1023
1054
  z.object({ kind: z.literal("variable"), name: z.string() }),
1024
- z.object({ kind: z.literal("claim"), key: z.enum(["sub", "org"]) })
1055
+ z.object({ kind: z.literal("claim"), key: z.enum(["sub", "org", ...EXTERNAL_POLICY_CLAIM_KEYS]) })
1025
1056
  ]);
1026
1057
  var CMP_OPS = [
1027
1058
  "eq",
@@ -1053,8 +1084,8 @@ var predIrSchema = z.lazy(
1053
1084
  value: z.number(),
1054
1085
  where: predIrSchema.optional()
1055
1086
  }),
1056
- z.object({ kind: z.literal("capability"), key: z.string() }),
1057
- z.object({ kind: z.literal("role"), key: z.string() }),
1087
+ z.object({ kind: z.literal("capability"), key: z.string(), namespace: z.string().optional() }),
1088
+ z.object({ kind: z.literal("role"), key: z.string(), namespace: z.string().optional() }),
1058
1089
  z.object({ kind: z.literal("dynamicFilter"), slot: z.string() }),
1059
1090
  z.object({ kind: z.literal("const"), value: z.boolean() })
1060
1091
  ])
@@ -1281,7 +1312,31 @@ var variableIrSchema = z.object({
1281
1312
  /** Absent = no default declared; see {@link variableDefaultIrSchema}. */
1282
1313
  defaultValue: variableDefaultIrSchema.optional()
1283
1314
  });
1315
+ var sqlColumnSchema = z.object({ name: z.string(), type: z.enum(["text", "boolean", "integer", "bigint", "numeric", "double", "uuid", "date", "timestamptz"]), nullable: z.boolean() });
1284
1316
  var compiledOperationSchema = z.object({
1317
+ language: z.literal("sql").optional(),
1318
+ /** Server-compiled object catalog and permission ceiling, never caller SQL. */
1319
+ sqlQuery: z.object({
1320
+ catalog: z.object({
1321
+ identity: z.string(),
1322
+ objects: z.array(z.object({
1323
+ ref: z.string(),
1324
+ table: z.string(),
1325
+ columns: z.array(sqlColumnSchema.extend({ type: z.string() })).min(1),
1326
+ scan: z.string().min(1),
1327
+ read: z.array(z.string()).min(1)
1328
+ }).strict()).min(1).max(256)
1329
+ }).strict(),
1330
+ read: z.array(z.string()).min(1).max(256),
1331
+ maxRows: z.number().int().min(1).max(1e4)
1332
+ }).strict().optional(),
1333
+ sql: z.object({
1334
+ text: z.string().min(1).max(1048576),
1335
+ columns: z.array(sqlColumnSchema).min(1).max(256),
1336
+ parameters: z.array(z.string()).max(128),
1337
+ read: z.array(z.string()).min(1).max(256),
1338
+ maxRows: z.number().int().min(1).max(1e4)
1339
+ }).strict().optional(),
1285
1340
  irVersion: irVersionSchema,
1286
1341
  name: z.string(),
1287
1342
  opHash: z.string().regex(/^sha256:[0-9a-f]{64}$/),
@@ -1293,8 +1348,16 @@ var compiledOperationSchema = z.object({
1293
1348
  variables: z.array(variableIrSchema),
1294
1349
  /** Every table the op touches — data-token scoping + audit. */
1295
1350
  tableSet: z.array(z.string()).min(1),
1296
- roots: z.array(rootIrSchema).min(1)
1351
+ roots: z.array(rootIrSchema)
1297
1352
  }).superRefine((operation, ctx) => {
1353
+ if (operation.language === "sql") {
1354
+ const validPlan = operation.sqlQuery ? !operation.sql && operation.irVersion === RUNTIME_SQL_IR_VERSION : !!operation.sql && operation.irVersion === SQL_IR_VERSION;
1355
+ if (!validPlan || operation.kind !== "query" || operation.roots.length !== 0 || !operation.requires?.capability) {
1356
+ ctx.addIssue({ code: "custom", message: "SQL operations require a query-only v4 plan or v5 runtime contract, capability, and no GraphQL roots" });
1357
+ }
1358
+ } else if (operation.sql || operation.sqlQuery || operation.roots.length === 0) {
1359
+ ctx.addIssue({ code: "custom", message: "GraphQL operations require roots and cannot carry SQL" });
1360
+ }
1298
1361
  for (const [index, root] of operation.roots.entries()) {
1299
1362
  if (root.kind !== "mutation" || root.assertAffectedRows === void 0) continue;
1300
1363
  if (operation.irVersion < 2 || operation.kind !== "mutation" || root.includeIf !== void 0) {
@@ -1341,11 +1404,16 @@ var resolvedMetaIrSchema = z.object({
1341
1404
  });
1342
1405
  var tableMetaIrSchema = z.object({
1343
1406
  table: z.string(),
1407
+ namespace: z.string().optional(),
1408
+ dependencies: z.array(z.string()).optional(),
1409
+ foreignReferences: z.array(z.object({ column: z.string(), target: z.string(), targetColumn: z.string() })).optional(),
1410
+ exportRead: z.union([z.literal(true), z.object({ capability: z.string() }), z.object({ role: z.string() })]).optional(),
1411
+ exportWrite: z.union([z.literal(true), z.object({ capability: z.string() }), z.object({ role: z.string() })]).optional(),
1344
1412
  schema: z.string(),
1345
1413
  typename: z.string(),
1346
1414
  /**
1347
1415
  * True when statements over this relation evaluate a claims-gated synced
1348
- * view: synced twins (`source: gold.*`) materialize as views whose baked
1416
+ * view: synced twins (`source: schema.table`) materialize as views whose baked
1349
1417
  * policy reads `current_setting('request.jwt.claims')`, and namespace views
1350
1418
  * inherit the gate transitively through their declared dependencies. The
1351
1419
  * executor must stamp that GUC (transaction-scoped) before any statement
@@ -1404,10 +1472,18 @@ var compiledArtifactSchema = z.object({
1404
1472
  irVersion: irVersionSchema,
1405
1473
  namespace: z.string(),
1406
1474
  schemaHash: z.string(),
1475
+ ownSchemaHash: z.string().optional(),
1476
+ dependencies: z.record(z.string(), z.string()).optional(),
1477
+ readPolicies: z.record(z.string(), predIrSchema).optional(),
1407
1478
  opsHash: z.string(),
1479
+ /** Apply-observed synced objects without a serving backing; not part of the authored schema hash. */
1480
+ unbackedTables: z.array(z.string()).optional(),
1408
1481
  tables: z.record(z.string(), tableMetaIrSchema),
1409
1482
  operations: z.record(z.string(), compiledOperationSchema)
1410
1483
  }).superRefine((artifact, ctx) => {
1484
+ if (Object.keys(artifact.dependencies ?? {}).length > 0 && artifact.irVersion < COMPOSED_IR_VERSION) {
1485
+ ctx.addIssue({ code: "custom", path: ["irVersion"], message: "cross-namespace dependencies require IR v3" });
1486
+ }
1411
1487
  for (const [name, operation] of Object.entries(artifact.operations)) {
1412
1488
  if (operation.irVersion > artifact.irVersion) {
1413
1489
  ctx.addIssue({ code: "custom", path: ["operations", name, "irVersion"], message: "operation requires a newer IR version than its artifact" });
@@ -1423,7 +1499,7 @@ var IrVersionError = class extends Error {
1423
1499
  function normalizeIr(raw) {
1424
1500
  if (!raw || typeof raw !== "object") throw new IrVersionError(void 0);
1425
1501
  const version = raw.irVersion;
1426
- if (version !== LEGACY_IR_VERSION && version !== IR_VERSION) throw new IrVersionError(version);
1502
+ if (!irVersionSchema.safeParse(version).success) throw new IrVersionError(version);
1427
1503
  return compiledArtifactSchema.parse(raw);
1428
1504
  }
1429
1505
  function indexByHash(artifact) {
@@ -1432,6 +1508,6 @@ function indexByHash(artifact) {
1432
1508
  return map;
1433
1509
  }
1434
1510
 
1435
- export { CELLS_FIELD, CELL_PARTS, CLAIMS_CONTRACT, CMP_OPS, DATE_BUCKETS, INPUT_REF_KINDS, IR_VERSION, IrVersionError, LEGACY_IR_VERSION, cellColumnName, compiledArtifactSchema, compiledOperationSchema, indexByHash, limitIrSchema, normalizeIr, orderIrSchema, predIrSchema, prefetchPlanIrSchema, requiredArtifactIrVersion, resolvedMetaIrSchema, resolvedSourceIrSchema, rootIrSchema, selectionIrSchema, tableMetaIrSchema, valueIrSchema, variableDefaultIrSchema, variableIrSchema };
1511
+ export { CELLS_FIELD, CELL_PARTS, CLAIMS_CONTRACT, CMP_OPS, COMPOSED_IR_VERSION, DATE_BUCKETS, EXTERNAL_POLICY_CLAIM_KEYS, GUARDED_IR_VERSION, INPUT_REF_KINDS, IR_VERSION, IrVersionError, LEGACY_IR_VERSION, RUNTIME_SQL_IR_VERSION, SQL_IR_VERSION, cellColumnName, compiledArtifactSchema, compiledOperationSchema, indexByHash, isExternalPolicyClaimKey, limitIrSchema, normalizeIr, orderIrSchema, predIrSchema, prefetchPlanIrSchema, requiredArtifactIrVersion, resolvedMetaIrSchema, resolvedSourceIrSchema, rootIrSchema, selectionIrSchema, tableMetaIrSchema, valueIrSchema, variableDefaultIrSchema, variableIrSchema };
1436
1512
  //# sourceMappingURL=ir.js.map
1437
1513
  //# sourceMappingURL=ir.js.map