@sequenceholdings/orm 2.5.0 → 2.6.1

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
@@ -1455,6 +1455,9 @@ function normalizeCanonical(definition) {
1455
1455
  return normalized;
1456
1456
  }
1457
1457
 
1458
+ // src/client/sql.ts
1459
+ var BUILTIN_SQL_READ_HASH = "sha256:a50c477c952a5e0fe68ed158a4530e4aab573c09bcbb1a8ac454dc88a39a6258";
1460
+
1458
1461
  // src/ir/index.ts
1459
1462
  var IR_VERSION = 5;
1460
1463
  var LEGACY_IR_VERSION = 1;
@@ -1788,7 +1791,8 @@ var compiledOperationSchema = z.object({
1788
1791
  }).superRefine((operation, ctx) => {
1789
1792
  if (operation.language === "sql") {
1790
1793
  const validPlan = operation.sqlQuery ? !operation.sql && operation.irVersion === RUNTIME_SQL_IR_VERSION : !!operation.sql && operation.irVersion === SQL_IR_VERSION;
1791
- if (!validPlan || operation.kind !== "query" || operation.roots.length !== 0 || !operation.requires?.capability) {
1794
+ const builtin = operation.name === "BuiltinSqlRead" && operation.opHash === BUILTIN_SQL_READ_HASH && operation.requires === null;
1795
+ if (!validPlan || operation.kind !== "query" || operation.roots.length !== 0 || !builtin && !operation.requires?.capability) {
1792
1796
  ctx.addIssue({ code: "custom", message: "SQL operations require a query-only v4 plan or v5 runtime contract, capability, and no GraphQL roots" });
1793
1797
  }
1794
1798
  } else if (operation.sql || operation.sqlQuery || operation.roots.length === 0) {
@@ -5170,9 +5174,9 @@ function compilePredIrToSql(pred, options = {}) {
5170
5174
  case "cmp":
5171
5175
  return compileCmp(pred.op, pred.column, pred.value, options);
5172
5176
  case "capability":
5173
- return `(${capabilityExpression(pred.key, pred.namespace ?? options.namespace)})`;
5177
+ return `(${options.statementClaimChecks ? "SELECT " : ""}${capabilityExpression(pred.key, pred.namespace ?? options.namespace)})`;
5174
5178
  case "role":
5175
- return `(${roleExpression(pred.key, pred.namespace ?? options.namespace)})`;
5179
+ return `(${options.statementClaimChecks ? "SELECT " : ""}${roleExpression(pred.key, pred.namespace ?? options.namespace)})`;
5176
5180
  case "exists":
5177
5181
  if (options.exists) return options.exists(pred);
5178
5182
  throw new OrmDefinitionError("synced view read policy cannot use 'exists' without a relationship resolver");
@@ -5280,7 +5284,8 @@ function createSqlCatalog({ canonical, publishedObjects, executionTarget }) {
5280
5284
  const { ctx, diagnostics } = buildCompileContext(canonical);
5281
5285
  if (diagnostics.some((d) => d.severity === "error")) throw new Error(diagnostics.map((d) => d.message).join("; "));
5282
5286
  const identity = sqlHash(`ontology-sql-v1\0${computeSchemaHash(canonical)}\0${executionTarget}`);
5283
- const refOf = (meta) => `${meta.namespace ?? canonical.namespace}/${meta.table}`;
5287
+ const logicalNames = new Map(Object.entries(ctx.tables).map(([key, meta]) => [meta, key]));
5288
+ const refOf = (meta) => `${meta.namespace ?? canonical.namespace}/${logicalNames.get(meta)}`;
5284
5289
  const definitions = [canonical, ...canonical.imports ?? []];
5285
5290
  const internal = new Set(definitions.flatMap((owner) => [
5286
5291
  ...owner.tables.filter((t2) => t2.internal).map((t2) => `${owner.namespace}/${t2.table}`),
@@ -5356,15 +5361,22 @@ function only(node, keys) {
5356
5361
  var parser;
5357
5362
  var SQL_TYPES = ["text", "boolean", "integer", "bigint", "numeric", "double", "uuid", "date", "timestamptz"];
5358
5363
  var pgType = (type) => type === "double" ? "double precision" : SQL_TYPES.some((t2) => t2 === type) ? type : fail3("unsupported SQL type");
5359
- async function compileOntologySql({ catalog, sql: sql4, parameterTypes = [], maxRows = 1e3 }) {
5360
- if (!Number.isInteger(maxRows) || maxRows < 1 || maxRows > 1e4 || Buffer.byteLength(sql4) > 65536) fail3("SQL limit exceeded");
5364
+ async function parseSelect(sql4) {
5365
+ if (Buffer.byteLength(sql4) > 65536) fail3("SQL limit exceeded");
5361
5366
  parser ??= new PgParser({ version: 17 });
5362
5367
  const parsed = await parser.parse(sql4);
5363
5368
  if (parsed.error || !parsed.tree) fail3("invalid SQL");
5364
5369
  const statements = list(obj(parsed.tree).stmts);
5365
5370
  if (statements.length !== 1) fail3("one SQL SELECT is required");
5371
+ const statement = obj(obj(statements[0]).stmt);
5372
+ if (!statement.SelectStmt) fail3("one SQL SELECT is required");
5373
+ return statement;
5374
+ }
5375
+ async function compileOntologySql({ catalog, sql: sql4, parameterTypes = [], maxRows = 1e3 }) {
5376
+ if (!Number.isInteger(maxRows) || maxRows < 1 || maxRows > 1e4 || Buffer.byteLength(sql4) > 65536) fail3("SQL limit exceeded");
5377
+ const statement = await parseSelect(sql4);
5366
5378
  const emitter = new SqlEmitter({ catalog, parameterTypes });
5367
- const result = emitter.select(obj(obj(statements[0]).stmt), { relations: [], ctes: /* @__PURE__ */ new Map() });
5379
+ const result = emitter.select(statement, { relations: [], ctes: /* @__PURE__ */ new Map() });
5368
5380
  if (emitter.used.size !== parameterTypes.length) fail3("SQL parameters must be used exactly as declared");
5369
5381
  if (!emitter.read.size) fail3("SQL operation must read an ontology object");
5370
5382
  return {
@@ -7783,19 +7795,19 @@ function compileComparison(op, left, right, columnTypes) {
7783
7795
  }
7784
7796
  return `${compileOperand(left, right, columnTypes)} ${SQL_COMPARE_OPS[op]} ${compileOperand(right, left, columnTypes)}`;
7785
7797
  }
7786
- function compilePredicate(predicate, columnTypes) {
7798
+ function compilePredicate(predicate, columnTypes, options = {}) {
7787
7799
  switch (predicate.kind) {
7788
7800
  case "and":
7789
7801
  case "or": {
7790
- const joined = predicate.operands.map((operand) => compilePredicate(operand, columnTypes)).join(` ${predicate.kind.toUpperCase()} `);
7802
+ const joined = predicate.operands.map((operand) => compilePredicate(operand, columnTypes, options)).join(` ${predicate.kind.toUpperCase()} `);
7791
7803
  return `(${joined})`;
7792
7804
  }
7793
7805
  case "not":
7794
- return `(NOT ${compilePredicate(predicate.operand, columnTypes)})`;
7806
+ return `(NOT ${compilePredicate(predicate.operand, columnTypes, options)})`;
7795
7807
  case "compare":
7796
7808
  return `(${compileComparison(predicate.op, predicate.left, predicate.right, columnTypes)})`;
7797
7809
  case "capability":
7798
- return `(${capabilityExpression(predicate.key)})`;
7810
+ return `(${options.statementClaimChecks ? "SELECT " : ""}${capabilityExpression(predicate.key)})`;
7799
7811
  }
7800
7812
  }
7801
7813