@sequenceholdings/orm 2.6.2 → 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.
package/dist/index.cjs CHANGED
@@ -4220,6 +4220,8 @@ function compilePredIrToSql(pred, options = {}) {
4220
4220
  return `(NOT ${compilePredIrToSql(pred.operand, options)})`;
4221
4221
  case "cmp":
4222
4222
  return compileCmp(pred.op, pred.column, pred.value, options);
4223
+ case "servicePrincipal":
4224
+ return `coalesce(${claimExpression("servicePrincipal")} = 'true', false)`;
4223
4225
  case "capability":
4224
4226
  return `(${options.statementClaimChecks ? "SELECT " : ""}${capabilityExpression(pred.key, pred.namespace ?? options.namespace)})`;
4225
4227
  case "role":
@@ -4447,6 +4449,39 @@ function resolveValidatesTarget(definition, entry) {
4447
4449
  }
4448
4450
  return { schema: ormSchemaName(definition.namespace), table: entry.table, key };
4449
4451
  }
4452
+
4453
+ // src/compiler/static-policy-sql.ts
4454
+ function compileStaticPolicySql({ table, predicate, tables, namespace, alias, rootRelation, statementClaimChecks, onRead }) {
4455
+ let sequence = 0;
4456
+ const aliases = new Set([alias, rootRelation].filter((value) => value !== void 0));
4457
+ const column = (meta, qualifier, name2) => {
4458
+ const found = meta.columns.find((c) => c.name === name2 || c.columnName === name2);
4459
+ if (!found) throw new OrmDefinitionError("Static policy relationship column is unavailable");
4460
+ return `${quoteIdentifier(qualifier)}.${quoteIdentifier(found.columnName)}`;
4461
+ };
4462
+ const render = (meta, pred, qualifier, outerRelation) => compilePredIrToSql(pred, {
4463
+ alias: qualifier,
4464
+ namespace: meta.namespace ?? namespace,
4465
+ statementClaimChecks,
4466
+ columnNames: Object.fromEntries(meta.columns.map((c) => [c.name, c.columnName])),
4467
+ columnTypes: Object.fromEntries(meta.columns.map((c) => [c.name, c.type])),
4468
+ exists: (node) => {
4469
+ const link = meta.links.find((candidate) => candidate.name === node.link);
4470
+ const target = link?.target ? tables[link.target] : void 0;
4471
+ const parent = qualifier ?? outerRelation;
4472
+ if (!link || !target || !parent) throw new OrmDefinitionError("Static policy relationship metadata is unavailable");
4473
+ onRead?.(target);
4474
+ let child;
4475
+ do {
4476
+ child = `policy_${++sequence}`;
4477
+ } while (aliases.has(child));
4478
+ aliases.add(child);
4479
+ const join4 = link.kind === "toMany" ? `${column(target, child, link.on)} = ${column(meta, parent, link.references)}` : `${column(meta, parent, link.on)} = ${column(target, child, link.references)}`;
4480
+ return `EXISTS (SELECT 1 FROM ${quoteIdentifier(target.schema)}.${quoteIdentifier(target.table)} AS ${quoteIdentifier(child)} WHERE ${join4} AND (${render(target, node.where, child)}))`;
4481
+ }
4482
+ });
4483
+ return render(table, predicate, alias, rootRelation);
4484
+ }
4450
4485
  var linkSchema = zod.z.strictObject({
4451
4486
  to: tableReferenceSchema,
4452
4487
  on: identifierSchema,
@@ -4796,6 +4831,7 @@ function syncedViewStatements({
4796
4831
  definition,
4797
4832
  backings,
4798
4833
  readPolicyIrByTable,
4834
+ policyTables,
4799
4835
  columnNamesByTable,
4800
4836
  columnTypesByTable,
4801
4837
  executionOwnerRole = SYNCED_VIEW_EXECUTION_OWNER_ROLE,
@@ -4842,7 +4878,15 @@ function syncedViewStatements({
4842
4878
  });
4843
4879
  const readPolicyIr = readPolicyIrByTable?.get(table.table);
4844
4880
  const readPolicy = table.policies?.read;
4845
- const policyClause = readPolicyIr ? ` WHERE ${compilePredIrToSql(readPolicyIr, {
4881
+ const policyTable = policyTables?.[table.table];
4882
+ const policyClause = readPolicyIr ? ` WHERE ${policyTable && policyTables ? compileStaticPolicySql({
4883
+ table: policyTable,
4884
+ predicate: readPolicyIr,
4885
+ tables: policyTables,
4886
+ namespace: definition.namespace,
4887
+ rootRelation: physical2.table,
4888
+ statementClaimChecks
4889
+ }) : compilePredIrToSql(readPolicyIr, {
4846
4890
  namespace: definition.namespace,
4847
4891
  statementClaimChecks,
4848
4892
  columnTypes,
@@ -6427,6 +6471,15 @@ function correctIntrospectionQuery(query) {
6427
6471
  if (!drizzleOrm.is(query, drizzleOrm.SQL)) return query;
6428
6472
  const compiled = new pgCore.PgDialect().sqlToQuery(query);
6429
6473
  let text4 = compiled.sql;
6474
+ if (text4.includes("JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)")) {
6475
+ text4 = text4.replace(/;\s*$/, "") + ` ORDER BY tc.constraint_name,
6476
+ (SELECT key_usage.ordinal_position FROM information_schema.key_column_usage AS key_usage
6477
+ WHERE key_usage.constraint_schema = tc.constraint_schema
6478
+ AND key_usage.constraint_name = tc.constraint_name
6479
+ AND key_usage.table_schema = tc.table_schema
6480
+ AND key_usage.table_name = tc.table_name
6481
+ AND key_usage.column_name = c.column_name)`;
6482
+ }
6430
6483
  const indexJoin = "pg_constraint con ON con.conindid = idx.indexrelid";
6431
6484
  if (text4.includes("AS generated_by_constraint") && text4.includes(indexJoin)) {
6432
6485
  text4 = text4.replace(indexJoin, `${indexJoin} AND con.contype IN ('p', 'u', 'x')`);
@@ -6659,14 +6712,21 @@ function renderOrmMigrationExport(exported) {
6659
6712
  var BUILTIN_SQL_READ_HASH = "sha256:a50c477c952a5e0fe68ed158a4530e4aab573c09bcbb1a8ac454dc88a39a6258";
6660
6713
 
6661
6714
  // src/ir/index.ts
6662
- var IR_VERSION = 5;
6715
+ var IR_VERSION = 6;
6663
6716
  var LEGACY_IR_VERSION = 1;
6664
6717
  var GUARDED_IR_VERSION = 2;
6665
6718
  var COMPOSED_IR_VERSION = 3;
6666
6719
  var SQL_IR_VERSION = 4;
6667
6720
  var RUNTIME_SQL_IR_VERSION = 5;
6668
- 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)]);
6721
+ var SERVICE_PRINCIPAL_IR_VERSION = 6;
6722
+ 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)]);
6669
6723
  var operationVersionSchema = zod.z.object({ irVersion: irVersionSchema });
6724
+ function usesServicePrincipal(value) {
6725
+ if (value === null || typeof value !== "object") return false;
6726
+ if (Array.isArray(value)) return value.some(usesServicePrincipal);
6727
+ const row = value;
6728
+ return row.kind === "servicePrincipal" || Object.values(row).some(usesServicePrincipal);
6729
+ }
6670
6730
  function requiredArtifactIrVersion(operations) {
6671
6731
  let required = LEGACY_IR_VERSION;
6672
6732
  for (const operation of operations) {
@@ -6726,6 +6786,7 @@ var predIrSchema = zod.z.lazy(
6726
6786
  zod.z.object({ kind: zod.z.literal("capability"), key: zod.z.string(), namespace: zod.z.string().optional() }),
6727
6787
  zod.z.object({ kind: zod.z.literal("role"), key: zod.z.string(), namespace: zod.z.string().optional() }),
6728
6788
  zod.z.object({ kind: zod.z.literal("dynamicFilter"), slot: zod.z.string() }),
6789
+ zod.z.object({ kind: zod.z.literal("servicePrincipal") }).strict(),
6729
6790
  zod.z.object({ kind: zod.z.literal("const"), value: zod.z.boolean() })
6730
6791
  ])
6731
6792
  );
@@ -6951,7 +7012,7 @@ var variableIrSchema = zod.z.object({
6951
7012
  /** Absent = no default declared; see {@link variableDefaultIrSchema}. */
6952
7013
  defaultValue: variableDefaultIrSchema.optional()
6953
7014
  });
6954
- 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() });
7015
+ 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() });
6955
7016
  var compiledOperationSchema = zod.z.object({
6956
7017
  language: zod.z.literal("sql").optional(),
6957
7018
  /** Server-compiled object catalog and permission ceiling, never caller SQL. */
@@ -6989,6 +7050,9 @@ var compiledOperationSchema = zod.z.object({
6989
7050
  tableSet: zod.z.array(zod.z.string()).min(1),
6990
7051
  roots: zod.z.array(rootIrSchema)
6991
7052
  }).superRefine((operation, ctx) => {
7053
+ if (operation.irVersion < SERVICE_PRINCIPAL_IR_VERSION && usesServicePrincipal(operation.roots)) {
7054
+ ctx.addIssue({ code: "custom", message: "servicePrincipal() requires IR v6" });
7055
+ }
6992
7056
  if (operation.language === "sql") {
6993
7057
  const validPlan = operation.sqlQuery ? !operation.sql && operation.irVersion === RUNTIME_SQL_IR_VERSION : !!operation.sql && operation.irVersion === SQL_IR_VERSION;
6994
7058
  const builtin = operation.name === "BuiltinSqlRead" && operation.opHash === BUILTIN_SQL_READ_HASH && operation.requires === null;
@@ -7121,6 +7185,9 @@ var compiledArtifactSchema = zod.z.object({
7121
7185
  tables: zod.z.record(zod.z.string(), tableMetaIrSchema),
7122
7186
  operations: zod.z.record(zod.z.string(), compiledOperationSchema)
7123
7187
  }).superRefine((artifact, ctx) => {
7188
+ if (artifact.irVersion < SERVICE_PRINCIPAL_IR_VERSION && usesServicePrincipal(artifact)) {
7189
+ ctx.addIssue({ code: "custom", message: "servicePrincipal() requires IR v6" });
7190
+ }
7124
7191
  if (Object.keys(artifact.dependencies ?? {}).length > 0 && artifact.irVersion < COMPOSED_IR_VERSION) {
7125
7192
  ctx.addIssue({ code: "custom", path: ["irVersion"], message: "cross-namespace dependencies require IR v3" });
7126
7193
  }
@@ -8445,6 +8512,7 @@ exports.RESERVED_TABLE_NAMES = RESERVED_TABLE_NAMES;
8445
8512
  exports.ROLES_CLAIM_KEY = ROLES_CLAIM_KEY;
8446
8513
  exports.RUNTIME_SQL_IR_VERSION = RUNTIME_SQL_IR_VERSION;
8447
8514
  exports.SCHEMA_DIR = SCHEMA_DIR;
8515
+ exports.SERVICE_PRINCIPAL_IR_VERSION = SERVICE_PRINCIPAL_IR_VERSION;
8448
8516
  exports.SQL_IR_VERSION = SQL_IR_VERSION;
8449
8517
  exports.STATEMENT_KINDS = STATEMENT_KINDS;
8450
8518
  exports.SYNCED_CONTRACT_HASH_DOMAIN = SYNCED_CONTRACT_HASH_DOMAIN;
@@ -8619,6 +8687,7 @@ exports.toMany = toMany;
8619
8687
  exports.toOne = toOne;
8620
8688
  exports.toPlatformUser = toPlatformUser;
8621
8689
  exports.typesInterfaceName = typesInterfaceName;
8690
+ exports.usesServicePrincipal = usesServicePrincipal;
8622
8691
  exports.validateNamespaceCatalog = validateNamespaceCatalog;
8623
8692
  exports.validateViewCatalogStatement = validateViewCatalogStatement;
8624
8693
  exports.valueIrSchema = valueIrSchema;