@prisma-next/sql-builder 0.5.0-dev.60 → 0.5.0-dev.61

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.
@@ -8,14 +8,21 @@ import { ExecutionContext } from "@prisma-next/sql-relational-core/query-lane-co
8
8
 
9
9
  //#region src/runtime/expression-impl.d.ts
10
10
  /**
11
- * Runtime wrapper around a relational-core AST expression node.
12
- * Carries ScopeField metadata (codecId, nullable) so aggregate-like
13
- * combinators can propagate the input codec onto their result.
11
+ * Runtime wrapper around a relational-core AST expression node. Carries ScopeField metadata (codecId, nullable) so aggregate-like combinators can propagate the input codec onto their result.
12
+ *
13
+ * `refs` records the column-bound binding (`{ table, column }`) when known — the field-proxy populates it for both the namespaced form (`f.user.email` → `ColumnRef`) and the top-level shortcut (`f.email` → `IdentifierRef` + refs metadata). Encode-side dispatch and the `validateParamRefRefs` pass read it via `refsOf(expression)`.
14
14
  */
15
15
  declare class ExpressionImpl<T extends ScopeField = ScopeField> implements Expression<T> {
16
16
  private readonly ast;
17
17
  readonly returnType: T;
18
- constructor(ast: AnyExpression, returnType: T);
18
+ readonly refs: {
19
+ readonly table: string;
20
+ readonly column: string;
21
+ } | undefined;
22
+ constructor(ast: AnyExpression, returnType: T, refs?: {
23
+ readonly table: string;
24
+ readonly column: string;
25
+ });
19
26
  buildAst(): AnyExpression;
20
27
  }
21
28
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/runtime/expression-impl.ts","../../src/runtime/field-proxy.ts","../../src/runtime/functions.ts","../../src/runtime/sql.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;cASa,yBAAyB,aAAa,uBAAuB,WAAW;EAAxE,iBAAc,GAAA;EAAW,SAAA,UAAA,EAEf,CAFe;EAAa,WAAA,CAAA,GAAA,EAIhC,aAJgC,EAAA,UAAA,EAIL,CAJK;EAAkC,QAAA,CAAA,CAAA,EASvE,aATuE;;;;iBCJrE,2BAA2B,cAAc,IAAI,WAAW;;;iBCmJxD,2BAA2B,0BAC7B,SAAS,eAAe,sBACnC,UAAU;iBAeG,oCAAoC,0BACtC,SAAS,eAAe,sBACnC,mBAAmB;;;UCpKL,qBAAqB,SAAS;oBAC3B,iBAAiB;;iBAGrB,cAAc,SAAS,sBAAsB,WAAW,KAAK,GAAG"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/runtime/expression-impl.ts","../../src/runtime/field-proxy.ts","../../src/runtime/functions.ts","../../src/runtime/sql.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;cASa,yBAAyB,aAAa,uBAAuB,WAAW;EAAxE,iBAAc,GAAA;EAAW,SAAA,UAAA,EAEf,CAFe;EAAa,SAAA,IAAA,EAAA;IAAkC,SAAA,KAAA,EAAA,MAAA;IAE9D,SAAA,MAAA,EAAA,MAAA;EAId,CAAA,GAAA,SAAA;EACO,WAAA,CAAA,GAAA,EADP,aACO,EAAA,UAAA,EAAA,CAAA,EAAA,IAPoE,CAOpE,EAAA;IAQF,SAAA,KAAA,EAAA,MAAA;IAf4D,SAAA,MAAA,EAAA,MAAA;EAAU,CAAA;cAetE;;;;iBCJE,2BAA2B,cAAc,IAAI,WAAW;;;iBC+KxD,2BAA2B,0BAC7B,SAAS,eAAe,sBACnC,UAAU;iBAeG,oCAAoC,0BACtC,SAAS,eAAe,sBACnC,mBAAmB;;;UC/ML,qBAAqB,SAAS;oBAC3B,iBAAiB;;iBAGrB,cAAc,SAAS,sBAAsB,WAAW,KAAK,GAAG"}
@@ -1,18 +1,20 @@
1
1
  import { AggregateExpr, AndExpr, BinaryExpr, ColumnRef, DeleteAst, DerivedTableSource, ExistsExpr, IdentifierRef, InsertAst, JoinAst, ListExpression, LiteralExpr, NullCheckExpr, OrExpr, OrderByItem, ParamRef, ProjectionItem, SelectAst, SubqueryExpr, TableSource, UpdateAst, collectOrderedParamRefs } from "@prisma-next/sql-relational-core/ast";
2
- import { toExpr } from "@prisma-next/sql-relational-core/expression";
2
+ import { refsOf, toExpr } from "@prisma-next/sql-relational-core/expression";
3
3
 
4
4
  //#region src/runtime/expression-impl.ts
5
5
  /**
6
- * Runtime wrapper around a relational-core AST expression node.
7
- * Carries ScopeField metadata (codecId, nullable) so aggregate-like
8
- * combinators can propagate the input codec onto their result.
6
+ * Runtime wrapper around a relational-core AST expression node. Carries ScopeField metadata (codecId, nullable) so aggregate-like combinators can propagate the input codec onto their result.
7
+ *
8
+ * `refs` records the column-bound binding (`{ table, column }`) when known — the field-proxy populates it for both the namespaced form (`f.user.email` → `ColumnRef`) and the top-level shortcut (`f.email` → `IdentifierRef` + refs metadata). Encode-side dispatch and the `validateParamRefRefs` pass read it via `refsOf(expression)`.
9
9
  */
10
10
  var ExpressionImpl = class {
11
11
  ast;
12
12
  returnType;
13
- constructor(ast, returnType) {
13
+ refs;
14
+ constructor(ast, returnType, refs) {
14
15
  this.ast = ast;
15
16
  this.returnType = returnType;
17
+ this.refs = refs;
16
18
  }
17
19
  buildAst() {
18
20
  return this.ast;
@@ -21,11 +23,30 @@ var ExpressionImpl = class {
21
23
 
22
24
  //#endregion
23
25
  //#region src/runtime/field-proxy.ts
26
+ /**
27
+ * For a top-level field name, find the namespace (table alias) that contributed it. When exactly one namespace owns the field, the top-level binding is unambiguously column-bound and we record that `(table, column)` pair on the `ExpressionImpl` so encode-side dispatch (`forColumn`) and the `validateParamRefRefs` pass can find it. The AST stays as `IdentifierRef` to preserve SQL rendering — adapters render top-level
28
+ * identifiers without an explicit table qualifier — so this change is metadata-only and produces no SQL drift.
29
+ */
30
+ function findUniqueNamespaceFor$1(scope, fieldName) {
31
+ let found;
32
+ for (const [namespace, fields] of Object.entries(scope.namespaces)) if (Object.hasOwn(fields, fieldName)) {
33
+ if (found !== void 0) return void 0;
34
+ found = namespace;
35
+ }
36
+ return found;
37
+ }
24
38
  function createFieldProxy(scope) {
25
39
  return new Proxy({}, { get(_target, prop) {
26
40
  if (Object.hasOwn(scope.topLevel, prop)) {
27
41
  const topField = scope.topLevel[prop];
28
- if (topField) return new ExpressionImpl(IdentifierRef.of(prop), topField);
42
+ if (topField) {
43
+ const namespace = findUniqueNamespaceFor$1(scope, prop);
44
+ const refs = namespace ? {
45
+ table: namespace,
46
+ column: prop
47
+ } : void 0;
48
+ return new ExpressionImpl(IdentifierRef.of(prop), topField, refs);
49
+ }
29
50
  }
30
51
  if (Object.hasOwn(scope.namespaces, prop)) {
31
52
  const nsFields = scope.namespaces[prop];
@@ -50,16 +71,29 @@ const BOOL_FIELD = {
50
71
  };
51
72
  const resolve = toExpr;
52
73
  /**
53
- * Resolves an Expression via `buildAst()`, or wraps a raw value as a
54
- * `LiteralExpr` — an SQL literal inlined into the query text, not a bound
55
- * parameter.
74
+ * Resolve a binary-comparison operand into an AST expression, threading the column-bound side's `codecId` + `refs` to the raw-value side.
75
+ *
76
+ * For `fns.eq(f.email, 'alice@example.com')`, `f.email` is the column-bound expression carrying a `ColumnRef` AST and a `returnType.codecId` (`pg/varchar@1`); the raw string operand has no codec context. By deriving the codec context from the column-bound side and forwarding it via `toExpr(value, codecId, refs)`, the resulting `ParamRef` carries the column refs that encode-side `forColumn` dispatch needs (and that the
77
+ * validator pass requires for parameterized codec ids like `pg/varchar@1` with a length parameter).
78
+ */
79
+ function resolveOperand(operand, otherCodecId, otherRefs) {
80
+ if (isExpressionLike(operand)) return operand.buildAst();
81
+ return toExpr(operand, otherCodecId, otherRefs);
82
+ }
83
+ function isExpressionLike(value) {
84
+ return typeof value === "object" && value !== null && "buildAst" in value && typeof value.buildAst === "function";
85
+ }
86
+ function operandCodecId(operand) {
87
+ if (!isExpressionLike(operand)) return void 0;
88
+ return operand.returnType?.codecId;
89
+ }
90
+ function operandRefs(operand) {
91
+ return refsOf(operand);
92
+ }
93
+ /**
94
+ * Resolves an Expression via `buildAst()`, or wraps a raw value as a `LiteralExpr` — an SQL literal inlined into the query text, not a bound parameter.
56
95
  *
57
- * Used for `and` / `or` operands. The usual operand is an `Expression<bool>`
58
- * (e.g. the result of `fns.eq`), which this function passes through by calling
59
- * `buildAst()`. The only time the raw-value branch fires is when the caller
60
- * writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals
61
- * lets the SQL planner statically simplify `TRUE AND x` to `x`, which it
62
- * cannot do for an opaque `ParamRef`.
96
+ * Used for `and` / `or` operands. The usual operand is an `Expression<bool>` (e.g. the result of `fns.eq`), which this function passes through by calling `buildAst()`. The only time the raw-value branch fires is when the caller writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals lets the SQL planner statically simplify `TRUE AND x` to `x`, which it cannot do for an opaque `ParamRef`.
63
97
  */
64
98
  function toLiteralExpr(value) {
65
99
  if (typeof value === "object" && value !== null && "buildAst" in value && typeof value.buildAst === "function") return value.buildAst();
@@ -68,24 +102,32 @@ function toLiteralExpr(value) {
68
102
  function boolExpr(astNode) {
69
103
  return new ExpressionImpl(astNode, BOOL_FIELD);
70
104
  }
105
+ function binaryWithSharedCodec(a, b, build) {
106
+ const aCodecId = operandCodecId(a);
107
+ const bCodecId = operandCodecId(b);
108
+ const aRefs = operandRefs(a);
109
+ return build(resolveOperand(a, bCodecId, operandRefs(b)), resolveOperand(b, aCodecId, aRefs));
110
+ }
71
111
  function eq(a, b) {
72
112
  if (b === null) return boolExpr(NullCheckExpr.isNull(resolve(a)));
73
113
  if (a === null) return boolExpr(NullCheckExpr.isNull(resolve(b)));
74
- return boolExpr(new BinaryExpr("eq", resolve(a), resolve(b)));
114
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr("eq", l, r)));
75
115
  }
76
116
  function ne(a, b) {
77
117
  if (b === null) return boolExpr(NullCheckExpr.isNotNull(resolve(a)));
78
118
  if (a === null) return boolExpr(NullCheckExpr.isNotNull(resolve(b)));
79
- return boolExpr(new BinaryExpr("neq", resolve(a), resolve(b)));
119
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr("neq", l, r)));
80
120
  }
81
121
  function comparison(a, b, op) {
82
- return boolExpr(new BinaryExpr(op, resolve(a), resolve(b)));
122
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr(op, l, r)));
83
123
  }
84
124
  function inOrNotIn(expr, valuesOrSubquery, op) {
85
125
  const left = expr.buildAst();
126
+ const leftCodecId = expr.returnType.codecId;
127
+ const leftRefs = refsOf(expr);
86
128
  const binaryFn = op === "in" ? BinaryExpr.in : BinaryExpr.notIn;
87
129
  if (Array.isArray(valuesOrSubquery)) {
88
- const refs = valuesOrSubquery.map((v) => resolve(v));
130
+ const refs = valuesOrSubquery.map((v) => resolveOperand(v, leftCodecId, leftRefs));
89
131
  return boolExpr(binaryFn(left, ListExpression.of(refs)));
90
132
  }
91
133
  return boolExpr(binaryFn(left, SubqueryExpr.of(valuesOrSubquery.buildAst())));
@@ -188,6 +230,17 @@ function combineWhereExprs(exprs) {
188
230
  if (exprs.length === 1) return exprs[0];
189
231
  return AndExpr.of(exprs);
190
232
  }
233
+ /**
234
+ * Same uniqueness rule as the field-proxy's `findUniqueNamespaceFor`: when exactly one namespace owns a top-level field, the binding is unambiguous. Used by `select('col', ...)` to attach `refs` metadata to the resulting `ProjectionItem` while keeping the AST as `IdentifierRef` (so SQL renders unchanged).
235
+ */
236
+ function findUniqueNamespaceFor(scope, fieldName) {
237
+ let found;
238
+ for (const [namespace, fields] of Object.entries(scope.namespaces)) if (Object.hasOwn(fields, fieldName)) {
239
+ if (found !== void 0) return void 0;
240
+ found = namespace;
241
+ }
242
+ return found;
243
+ }
191
244
  function buildSelectAst(state) {
192
245
  const where = combineWhereExprs(state.where);
193
246
  return new SelectAst({
@@ -283,7 +336,12 @@ function resolveSelectArgs(args, scope, ctx) {
283
336
  for (const colName of args) {
284
337
  const field = scope.topLevel[colName];
285
338
  if (!field) throw new Error(`Column "${colName}" not found in scope`);
286
- projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId));
339
+ const namespace = findUniqueNamespaceFor(scope, colName);
340
+ const refs = namespace ? {
341
+ table: namespace,
342
+ column: colName
343
+ } : void 0;
344
+ projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId, refs));
287
345
  newRowFields[colName] = field;
288
346
  }
289
347
  return {
@@ -533,7 +591,13 @@ function buildParamValues(values, table, tableName, op, ctx) {
533
591
  const params = {};
534
592
  for (const [col, value] of Object.entries(values)) {
535
593
  const column = table.columns[col];
536
- params[col] = ParamRef.of(value, column ? { codecId: column.codecId } : void 0);
594
+ params[col] = ParamRef.of(value, column ? {
595
+ codecId: column.codecId,
596
+ refs: {
597
+ table: tableName,
598
+ column: col
599
+ }
600
+ } : void 0);
537
601
  }
538
602
  for (const def of ctx.applyMutationDefaults({
539
603
  op,
@@ -541,7 +605,13 @@ function buildParamValues(values, table, tableName, op, ctx) {
541
605
  values
542
606
  })) {
543
607
  const column = table.columns[def.column];
544
- params[def.column] = ParamRef.of(def.value, column ? { codecId: column.codecId } : void 0);
608
+ params[def.column] = ParamRef.of(def.value, column ? {
609
+ codecId: column.codecId,
610
+ refs: {
611
+ table: tableName,
612
+ column: def.column
613
+ }
614
+ } : void 0);
545
615
  }
546
616
  return params;
547
617
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["BOOL_FIELD: BooleanCodecType","meta: PlanMeta","fields: ScopeTable","topLevel: ScopeTable","result: ScopeTable","namespaces: Record<string, ScopeTable>","projections: ProjectionItem[]","newRowFields: Record<string, ScopeField>","combined","#state","#buildLateral","#addLateralJoin","#addJoin","subqueryRowFields: ScopeTable","params: Record<string, ParamRef>","#tableName","#table","#scope","#values","#returningColumns","#rowFields","newRowFields: Record<string, ScopeField>","#setValues","#whereCallbacks","#tableName","#table","#fromSource","#scope","#toJoined","ctx: BuilderContext","options"],"sources":["../../src/runtime/expression-impl.ts","../../src/runtime/field-proxy.ts","../../src/runtime/functions.ts","../../src/runtime/builder-base.ts","../../src/runtime/query-impl.ts","../../src/runtime/joined-tables-impl.ts","../../src/runtime/mutation-impl.ts","../../src/runtime/table-proxy-impl.ts","../../src/runtime/sql.ts"],"sourcesContent":["import type { AnyExpression as AstExpression } from '@prisma-next/sql-relational-core/ast';\nimport type { Expression } from '@prisma-next/sql-relational-core/expression';\nimport type { ScopeField } from '../scope';\n\n/**\n * Runtime wrapper around a relational-core AST expression node.\n * Carries ScopeField metadata (codecId, nullable) so aggregate-like\n * combinators can propagate the input codec onto their result.\n */\nexport class ExpressionImpl<T extends ScopeField = ScopeField> implements Expression<T> {\n private readonly ast: AstExpression;\n readonly returnType: T;\n\n constructor(ast: AstExpression, returnType: T) {\n this.ast = ast;\n this.returnType = returnType;\n }\n\n buildAst(): AstExpression {\n return this.ast;\n }\n}\n","import { ColumnRef, IdentifierRef } from '@prisma-next/sql-relational-core/ast';\nimport type { FieldProxy } from '../expression';\nimport type { Scope, ScopeTable } from '../scope';\nimport { ExpressionImpl } from './expression-impl';\n\nexport function createFieldProxy<S extends Scope>(scope: S): FieldProxy<S> {\n return new Proxy({} as FieldProxy<S>, {\n get(_target, prop: string) {\n if (Object.hasOwn(scope.topLevel, prop)) {\n const topField = scope.topLevel[prop];\n if (topField) return new ExpressionImpl(IdentifierRef.of(prop), topField);\n }\n\n if (Object.hasOwn(scope.namespaces, prop)) {\n const nsFields = scope.namespaces[prop];\n if (nsFields) return createNamespaceProxy(prop, nsFields);\n }\n\n return undefined;\n },\n });\n}\n\nfunction createNamespaceProxy(\n namespaceName: string,\n fields: ScopeTable,\n): Record<string, ExpressionImpl> {\n return new Proxy({} as Record<string, ExpressionImpl>, {\n get(_target, prop: string) {\n if (Object.hasOwn(fields, prop)) {\n const field = fields[prop];\n if (field) return new ExpressionImpl(ColumnRef.of(namespaceName, prop), field);\n }\n return undefined;\n },\n });\n}\n","import type { SqlOperationEntry } from '@prisma-next/sql-operations';\nimport {\n AggregateExpr,\n AndExpr,\n type AnyExpression as AstExpression,\n BinaryExpr,\n type BinaryOp,\n ExistsExpr,\n ListExpression,\n LiteralExpr,\n NullCheckExpr,\n OrExpr,\n SubqueryExpr,\n} from '@prisma-next/sql-relational-core/ast';\nimport { toExpr } from '@prisma-next/sql-relational-core/expression';\nimport type {\n AggregateFunctions,\n AggregateOnlyFunctions,\n BooleanCodecType,\n BuiltinFunctions,\n CodecExpression,\n Expression,\n Functions,\n} from '../expression';\nimport type { QueryContext, ScopeField, Subquery } from '../scope';\nimport { ExpressionImpl } from './expression-impl';\n\ntype CodecTypes = Record<string, { readonly input: unknown }>;\n// Runtime-level ExprOrVal — accepts any codec, any nullability. Concrete codec\n// typing lives on the public BuiltinFunctions surface in `../expression`.\ntype ExprOrVal<CodecId extends string = string, N extends boolean = boolean> = CodecExpression<\n CodecId,\n N,\n CodecTypes\n>;\n\nconst BOOL_FIELD: BooleanCodecType = { codecId: 'pg/bool@1', nullable: false };\n\nconst resolve = toExpr;\n\n/**\n * Resolves an Expression via `buildAst()`, or wraps a raw value as a\n * `LiteralExpr` — an SQL literal inlined into the query text, not a bound\n * parameter.\n *\n * Used for `and` / `or` operands. The usual operand is an `Expression<bool>`\n * (e.g. the result of `fns.eq`), which this function passes through by calling\n * `buildAst()`. The only time the raw-value branch fires is when the caller\n * writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals\n * lets the SQL planner statically simplify `TRUE AND x` to `x`, which it\n * cannot do for an opaque `ParamRef`.\n */\nfunction toLiteralExpr(value: unknown): AstExpression {\n if (\n typeof value === 'object' &&\n value !== null &&\n 'buildAst' in value &&\n typeof (value as { buildAst: unknown }).buildAst === 'function'\n ) {\n return (value as { buildAst(): AstExpression }).buildAst();\n }\n return new LiteralExpr(value);\n}\n\nfunction boolExpr(astNode: AstExpression): ExpressionImpl<BooleanCodecType> {\n return new ExpressionImpl(astNode, BOOL_FIELD);\n}\n\nfunction eq(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {\n if (b === null) return boolExpr(NullCheckExpr.isNull(resolve(a)));\n if (a === null) return boolExpr(NullCheckExpr.isNull(resolve(b)));\n return boolExpr(new BinaryExpr('eq', resolve(a), resolve(b)));\n}\n\nfunction ne(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {\n if (b === null) return boolExpr(NullCheckExpr.isNotNull(resolve(a)));\n if (a === null) return boolExpr(NullCheckExpr.isNotNull(resolve(b)));\n return boolExpr(new BinaryExpr('neq', resolve(a), resolve(b)));\n}\n\nfunction comparison(a: ExprOrVal, b: ExprOrVal, op: BinaryOp): ExpressionImpl<BooleanCodecType> {\n return boolExpr(new BinaryExpr(op, resolve(a), resolve(b)));\n}\n\nfunction inOrNotIn(\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n op: 'in' | 'notIn',\n): ExpressionImpl<BooleanCodecType> {\n const left = expr.buildAst();\n const binaryFn = op === 'in' ? BinaryExpr.in : BinaryExpr.notIn;\n\n if (Array.isArray(valuesOrSubquery)) {\n const refs = valuesOrSubquery.map((v) => resolve(v));\n return boolExpr(binaryFn(left, ListExpression.of(refs)));\n }\n return boolExpr(binaryFn(left, SubqueryExpr.of(valuesOrSubquery.buildAst())));\n}\n\nfunction numericAgg(\n fn: 'sum' | 'avg' | 'min' | 'max',\n expr: Expression<ScopeField>,\n): ExpressionImpl<{ codecId: string; nullable: true }> {\n return new ExpressionImpl(AggregateExpr[fn](expr.buildAst()), {\n codecId: expr.returnType.codecId,\n nullable: true as const,\n });\n}\n\nfunction createBuiltinFunctions() {\n return {\n eq: (a: ExprOrVal, b: ExprOrVal) => eq(a, b),\n ne: (a: ExprOrVal, b: ExprOrVal) => ne(a, b),\n gt: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'gt'),\n gte: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'gte'),\n lt: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'lt'),\n lte: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'lte'),\n and: (...exprs: ExprOrVal<'pg/bool@1', boolean>[]) =>\n boolExpr(AndExpr.of(exprs.map(toLiteralExpr))),\n or: (...exprs: ExprOrVal<'pg/bool@1', boolean>[]) =>\n boolExpr(OrExpr.of(exprs.map(toLiteralExpr))),\n exists: (subquery: Subquery<Record<string, ScopeField>>) =>\n boolExpr(ExistsExpr.exists(subquery.buildAst())),\n notExists: (subquery: Subquery<Record<string, ScopeField>>) =>\n boolExpr(ExistsExpr.notExists(subquery.buildAst())),\n in: (\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n ) => inOrNotIn(expr, valuesOrSubquery, 'in'),\n notIn: (\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n ) => inOrNotIn(expr, valuesOrSubquery, 'notIn'),\n } satisfies BuiltinFunctions<CodecTypes>;\n}\n\nfunction createAggregateOnlyFunctions() {\n return {\n count: (expr?: Expression<ScopeField>) => {\n const astExpr = expr ? expr.buildAst() : undefined;\n return new ExpressionImpl(AggregateExpr.count(astExpr), {\n codecId: 'pg/int8@1',\n nullable: false,\n });\n },\n sum: (expr: Expression<ScopeField>) => numericAgg('sum', expr),\n avg: (expr: Expression<ScopeField>) => numericAgg('avg', expr),\n min: (expr: Expression<ScopeField>) => numericAgg('min', expr),\n max: (expr: Expression<ScopeField>) => numericAgg('max', expr),\n } satisfies AggregateOnlyFunctions;\n}\n\nexport function createFunctions<QC extends QueryContext>(\n operations: Readonly<Record<string, SqlOperationEntry>>,\n): Functions<QC> {\n const builtins = createBuiltinFunctions();\n\n return new Proxy({} as Functions<QC>, {\n get(_target, prop: string) {\n const builtin = (builtins as Record<string, unknown>)[prop];\n if (builtin) return builtin;\n\n const op = operations[prop];\n if (op) return op.impl;\n return undefined;\n },\n });\n}\n\nexport function createAggregateFunctions<QC extends QueryContext>(\n operations: Readonly<Record<string, SqlOperationEntry>>,\n): AggregateFunctions<QC> {\n const baseFns = createFunctions<QC>(operations);\n const aggregates = createAggregateOnlyFunctions();\n\n return new Proxy({} as AggregateFunctions<QC>, {\n get(_target, prop: string) {\n const agg = (aggregates as Record<string, unknown>)[prop];\n if (agg) return agg;\n\n return (baseFns as Record<string, unknown>)[prop];\n },\n });\n}\n","import type { PlanMeta } from '@prisma-next/contract/types';\nimport type { StorageTable } from '@prisma-next/sql-contract/types';\nimport type { SqlOperationEntry } from '@prisma-next/sql-operations';\nimport {\n AndExpr,\n type AnyExpression as AstExpression,\n collectOrderedParamRefs,\n IdentifierRef,\n OrderByItem,\n ProjectionItem,\n SelectAst,\n type TableSource,\n} from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type {\n AppliedMutationDefault,\n MutationDefaultsOptions,\n} from '@prisma-next/sql-relational-core/query-lane-context';\nimport type {\n AggregateFunctions,\n Expression,\n FieldProxy,\n OrderByOptions,\n OrderByScope,\n} from '../expression';\nimport type {\n GatedMethod,\n MergeScopes,\n NullableScope,\n QueryContext,\n Scope,\n ScopeField,\n ScopeTable,\n} from '../scope';\nimport { createFieldProxy } from './field-proxy';\nimport { createAggregateFunctions, createFunctions } from './functions';\n\nexport type ExprCallback = (fields: FieldProxy<Scope>, fns: unknown) => Expression<ScopeField>;\n\nexport class BuilderBase<Capabilities = unknown> {\n protected readonly ctx: BuilderContext;\n\n constructor(ctx: BuilderContext) {\n this.ctx = ctx;\n }\n\n protected _gate<Req extends Record<string, Record<string, boolean>>, Args extends unknown[], R>(\n required: Req,\n methodName: string,\n method: (...args: Args) => R,\n ): GatedMethod<Capabilities, Req, (...args: Args) => R> {\n return ((...args: Args): R => {\n assertCapability(this.ctx, required, methodName);\n return method(...args);\n }) as GatedMethod<Capabilities, Req, (...args: Args) => R>;\n }\n}\n\nexport interface BuilderState {\n readonly from: TableSource;\n readonly joins: readonly import('@prisma-next/sql-relational-core/ast').JoinAst[];\n readonly projections: readonly ProjectionItem[];\n readonly where: readonly AstExpression[];\n readonly orderBy: readonly OrderByItem[];\n readonly groupBy: readonly AstExpression[];\n readonly having: AstExpression | undefined;\n readonly limit: number | undefined;\n readonly offset: number | undefined;\n readonly distinct: true | undefined;\n readonly distinctOn: readonly AstExpression[] | undefined;\n readonly scope: Scope;\n readonly rowFields: Record<string, ScopeField>;\n}\n\nexport interface BuilderContext {\n readonly capabilities: Record<string, Record<string, boolean>>;\n readonly queryOperationTypes: Readonly<Record<string, SqlOperationEntry>>;\n readonly target: string;\n readonly storageHash: string;\n readonly applyMutationDefaults: (\n options: MutationDefaultsOptions,\n ) => ReadonlyArray<AppliedMutationDefault>;\n}\n\nexport function emptyState(from: TableSource, scope: Scope): BuilderState {\n return {\n from,\n joins: [],\n projections: [],\n where: [],\n orderBy: [],\n groupBy: [],\n having: undefined,\n limit: undefined,\n offset: undefined,\n distinct: undefined,\n distinctOn: undefined,\n scope,\n rowFields: {},\n };\n}\n\nexport function cloneState(state: BuilderState, overrides: Partial<BuilderState>): BuilderState {\n return { ...state, ...overrides };\n}\n\nexport function combineWhereExprs(exprs: readonly AstExpression[]): AstExpression | undefined {\n if (exprs.length === 0) return undefined;\n if (exprs.length === 1) return exprs[0];\n return AndExpr.of(exprs);\n}\n\nexport function buildSelectAst(state: BuilderState): SelectAst {\n const where = combineWhereExprs(state.where);\n return new SelectAst({\n from: state.from,\n joins: state.joins.length > 0 ? state.joins : undefined,\n projection: state.projections,\n where,\n orderBy: state.orderBy.length > 0 ? state.orderBy : undefined,\n distinct: state.distinct,\n distinctOn: state.distinctOn && state.distinctOn.length > 0 ? state.distinctOn : undefined,\n groupBy: state.groupBy.length > 0 ? state.groupBy : undefined,\n having: state.having,\n limit: state.limit,\n offset: state.offset,\n selectAllIntent: undefined,\n });\n}\n\nexport function buildQueryPlan<Row = unknown>(\n ast: import('@prisma-next/sql-relational-core/ast').AnyQueryAst,\n ctx: BuilderContext,\n): SqlQueryPlan<Row> {\n const paramValues = collectOrderedParamRefs(ast).map((r) => r.value);\n\n const meta: PlanMeta = Object.freeze({\n target: ctx.target,\n storageHash: ctx.storageHash,\n lane: 'dsl',\n });\n\n return Object.freeze({ ast, params: paramValues, meta });\n}\n\nexport function buildPlan<Row = unknown>(\n state: BuilderState,\n ctx: BuilderContext,\n): SqlQueryPlan<Row> {\n return buildQueryPlan<Row>(buildSelectAst(state), ctx);\n}\n\nexport function tableToScope(name: string, table: StorageTable): Scope {\n const fields: ScopeTable = {};\n for (const [colName, col] of Object.entries(table.columns)) {\n fields[colName] = { codecId: col.codecId, nullable: col.nullable };\n }\n return { topLevel: { ...fields }, namespaces: { [name]: fields } };\n}\n\nexport function mergeScopes<A extends Scope, B extends Scope>(a: A, b: B): MergeScopes<A, B> {\n const topLevel: ScopeTable = {};\n for (const [k, v] of Object.entries(a.topLevel)) {\n if (!(k in b.topLevel)) topLevel[k] = v;\n }\n for (const [k, v] of Object.entries(b.topLevel)) {\n if (!(k in a.topLevel)) topLevel[k] = v;\n }\n return {\n topLevel,\n namespaces: { ...a.namespaces, ...b.namespaces },\n } as MergeScopes<A, B>;\n}\n\nexport function nullableScope<S extends Scope>(scope: S): NullableScope<S> {\n const mkNullable = (tbl: ScopeTable): ScopeTable => {\n const result: ScopeTable = {};\n for (const [k, v] of Object.entries(tbl)) {\n result[k] = { codecId: v.codecId, nullable: true };\n }\n return result;\n };\n const namespaces: Record<string, ScopeTable> = {};\n for (const [k, v] of Object.entries(scope.namespaces)) {\n namespaces[k] = mkNullable(v);\n }\n return { topLevel: mkNullable(scope.topLevel), namespaces } as NullableScope<S>;\n}\n\nexport function orderByScopeOf<S extends Scope, R extends Record<string, ScopeField>>(\n scope: S,\n rowFields: R,\n): OrderByScope<S, R> {\n return {\n topLevel: { ...scope.topLevel, ...rowFields },\n namespaces: scope.namespaces,\n };\n}\n\nexport function assertCapability(\n ctx: BuilderContext,\n required: Record<string, Record<string, boolean>>,\n methodName: string,\n): void {\n for (const [ns, keys] of Object.entries(required)) {\n for (const key of Object.keys(keys)) {\n if (!ctx.capabilities[ns]?.[key]) {\n throw new Error(`${methodName}() requires capability ${ns}.${key}`);\n }\n }\n }\n}\n\nexport function resolveSelectArgs(\n args: unknown[],\n scope: Scope,\n ctx: BuilderContext,\n): { projections: ProjectionItem[]; newRowFields: Record<string, ScopeField> } {\n const projections: ProjectionItem[] = [];\n const newRowFields: Record<string, ScopeField> = {};\n\n if (args.length === 0) return { projections, newRowFields };\n\n if (typeof args[0] === 'string' && (args.length === 1 || typeof args[1] !== 'function')) {\n for (const colName of args as string[]) {\n const field = scope.topLevel[colName];\n if (!field) throw new Error(`Column \"${colName}\" not found in scope`);\n projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId));\n newRowFields[colName] = field;\n }\n return { projections, newRowFields };\n }\n\n if (typeof args[0] === 'string' && typeof args[1] === 'function') {\n const alias = args[0] as string;\n const exprFn = args[1] as (\n f: FieldProxy<Scope>,\n fns: AggregateFunctions<QueryContext>,\n ) => Expression<ScopeField>;\n const fns = createAggregateFunctions(ctx.queryOperationTypes);\n const result = exprFn(createFieldProxy(scope), fns);\n const field = result.returnType;\n projections.push(ProjectionItem.of(alias, result.buildAst(), field.codecId));\n newRowFields[alias] = field;\n return { projections, newRowFields };\n }\n\n if (typeof args[0] === 'function') {\n const callbackFn = args[0] as (\n f: FieldProxy<Scope>,\n fns: AggregateFunctions<QueryContext>,\n ) => Record<string, Expression<ScopeField>>;\n const fns = createAggregateFunctions(ctx.queryOperationTypes);\n const record = callbackFn(createFieldProxy(scope), fns);\n for (const [key, expr] of Object.entries(record)) {\n const field = expr.returnType;\n projections.push(ProjectionItem.of(key, expr.buildAst(), field.codecId));\n newRowFields[key] = field;\n }\n return { projections, newRowFields };\n }\n\n throw new Error('Invalid .select() arguments');\n}\n\nexport function resolveOrderBy(\n arg: unknown,\n options: OrderByOptions | undefined,\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n useAggregateFns: boolean,\n): OrderByItem {\n const dir = options?.direction ?? 'asc';\n\n if (typeof arg === 'string') {\n const combined = orderByScopeOf(scope, rowFields);\n if (!(arg in combined.topLevel))\n throw new Error(`Column \"${arg}\" not found in scope for orderBy`);\n const expr = IdentifierRef.of(arg);\n return dir === 'asc' ? OrderByItem.asc(expr) : OrderByItem.desc(expr);\n }\n\n if (typeof arg === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = useAggregateFns\n ? createAggregateFunctions(ctx.queryOperationTypes)\n : createFunctions(ctx.queryOperationTypes);\n const result = (arg as ExprCallback)(createFieldProxy(combined), fns);\n return dir === 'asc' ? OrderByItem.asc(result.buildAst()) : OrderByItem.desc(result.buildAst());\n }\n\n throw new Error('Invalid orderBy argument');\n}\n\nexport function resolveGroupBy(\n args: unknown[],\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n): AstExpression[] {\n if (typeof args[0] === 'string') {\n const combined = orderByScopeOf(scope, rowFields);\n return (args as string[]).map((colName) => {\n if (!(colName in combined.topLevel))\n throw new Error(`Column \"${colName}\" not found in scope for groupBy`);\n return IdentifierRef.of(colName);\n });\n }\n\n if (typeof args[0] === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = createFunctions(ctx.queryOperationTypes);\n const result = (args[0] as ExprCallback)(createFieldProxy(combined), fns);\n return [result.buildAst()];\n }\n\n throw new Error('Invalid groupBy arguments');\n}\n\nexport function resolveDistinctOn(\n args: unknown[],\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n): AstExpression[] {\n if (args.length === 1 && typeof args[0] === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = createFunctions(ctx.queryOperationTypes);\n const result = (args[0] as ExprCallback)(createFieldProxy(combined), fns);\n return [result.buildAst()];\n }\n const combined = orderByScopeOf(scope, rowFields);\n return (args as string[]).map((colName) => {\n if (!(colName in combined.topLevel))\n throw new Error(`Column \"${colName}\" not found in scope for distinctOn`);\n return IdentifierRef.of(colName);\n });\n}\n","import { DerivedTableSource, type SelectAst } from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type {\n AggregateFunctions,\n BooleanCodecType,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n Functions,\n OrderByOptions,\n OrderByScope,\n WithField,\n WithFields,\n} from '../expression';\nimport type { ResolveRow } from '../resolve';\nimport type {\n Expand,\n JoinOuterScope,\n JoinSource,\n QueryContext,\n Scope,\n ScopeField,\n // biome-ignore lint/correctness/noUnusedImports: used in `declare` property\n SubqueryMarker,\n} from '../scope';\nimport type { GroupedQuery } from '../types/grouped-query';\nimport type { SelectQuery } from '../types/select-query';\nimport {\n BuilderBase,\n type BuilderContext,\n type BuilderState,\n buildPlan,\n buildSelectAst,\n cloneState,\n orderByScopeOf,\n resolveDistinctOn,\n resolveGroupBy,\n resolveOrderBy,\n resolveSelectArgs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createAggregateFunctions, createFunctions } from './functions';\n\nabstract class QueryBase<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n> extends BuilderBase<QC['capabilities']> {\n protected readonly state: BuilderState;\n\n constructor(state: BuilderState, ctx: BuilderContext) {\n super(ctx);\n this.state = state;\n }\n\n protected abstract clone(state: BuilderState): this;\n\n distinctOn = this._gate(\n { postgres: { distinctOn: true } },\n 'distinctOn',\n (...args: unknown[]) => {\n const exprs = resolveDistinctOn(args, this.state.scope, this.state.rowFields, this.ctx);\n return this.clone(\n cloneState(this.state, {\n distinctOn: [...(this.state.distinctOn ?? []), ...exprs],\n }),\n );\n },\n );\n\n limit(count: number): this {\n return this.clone(cloneState(this.state, { limit: count }));\n }\n\n offset(count: number): this {\n return this.clone(cloneState(this.state, { offset: count }));\n }\n\n distinct(): this {\n return this.clone(cloneState(this.state, { distinct: true }));\n }\n\n groupBy(\n ...fields: ((keyof RowType | keyof AvailableScope['topLevel']) & string)[]\n ): GroupedQuery<QC, AvailableScope, RowType>;\n groupBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: Functions<QC>,\n ) => Expression<ScopeField>,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n groupBy(...args: unknown[]): unknown {\n const exprs = resolveGroupBy(args, this.state.scope, this.state.rowFields, this.ctx);\n return new GroupedQueryImpl<QC, AvailableScope, RowType>(\n cloneState(this.state, { groupBy: [...this.state.groupBy, ...exprs] }),\n this.ctx,\n );\n }\n\n as<Alias extends string>(alias: Alias): JoinSource<RowType, Alias> {\n const ast = buildSelectAst(this.state);\n const derivedSource = DerivedTableSource.as(alias, ast);\n const scope = {\n topLevel: this.state.rowFields as RowType,\n namespaces: { [alias]: this.state.rowFields } as Record<Alias, RowType>,\n };\n return {\n getJoinOuterScope: () => scope,\n buildAst: () => derivedSource,\n\n // `as unknown` is necessary, because JoinOuterScope is a phantom type-only property that does not exist at runtime\n } satisfies Omit<JoinSource<RowType, Alias>, typeof JoinOuterScope> as unknown as JoinSource<\n RowType,\n Alias\n >;\n }\n\n getRowFields(): Record<string, ScopeField> {\n return this.state.rowFields;\n }\n\n buildAst(): SelectAst {\n return buildSelectAst(this.state);\n }\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n return buildPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n this.state,\n this.ctx,\n );\n }\n}\n\nexport class SelectQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends QueryBase<QC, AvailableScope, RowType>\n implements SelectQuery<QC, AvailableScope, RowType>\n{\n declare readonly [SubqueryMarker]: RowType;\n\n protected clone(state: BuilderState): this {\n return new SelectQueryImpl<QC, AvailableScope, RowType>(state, this.ctx) as this;\n }\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<RowType, AvailableScope['topLevel'], Columns>>;\n select<Alias extends string, Field extends ScopeField>(\n alias: Alias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<RowType, Field, Alias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<RowType & ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n const { projections, newRowFields } = resolveSelectArgs(args, this.state.scope, this.ctx);\n return new SelectQueryImpl(\n cloneState(this.state, {\n projections: [...this.state.projections, ...projections],\n rowFields: { ...this.state.rowFields, ...newRowFields },\n }),\n this.ctx,\n );\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): SelectQuery<QC, AvailableScope, RowType> {\n const fieldProxy = createFieldProxy(this.state.scope);\n const fns = createFunctions<QC>(this.ctx.queryOperationTypes);\n const result = (expr as ExpressionBuilder<Scope, QueryContext>)(fieldProxy, fns as never);\n return new SelectQueryImpl(\n cloneState(this.state, {\n where: [...this.state.where, result.buildAst()],\n }),\n this.ctx,\n );\n }\n\n orderBy(\n field: (keyof RowType | keyof AvailableScope['topLevel']) & string,\n options?: OrderByOptions,\n ): SelectQuery<QC, AvailableScope, RowType>;\n orderBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: Functions<QC>,\n ) => Expression<ScopeField>,\n options?: OrderByOptions,\n ): SelectQuery<QC, AvailableScope, RowType>;\n orderBy(arg: unknown, options?: OrderByOptions): unknown {\n const item = resolveOrderBy(\n arg,\n options,\n this.state.scope,\n this.state.rowFields,\n this.ctx,\n false,\n );\n return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));\n }\n}\n\nexport class GroupedQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends QueryBase<QC, AvailableScope, RowType>\n implements GroupedQuery<QC, AvailableScope, RowType>\n{\n declare readonly [SubqueryMarker]: RowType;\n\n protected clone(state: BuilderState): this {\n return new GroupedQueryImpl<QC, AvailableScope, RowType>(state, this.ctx) as this;\n }\n\n having(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: AggregateFunctions<QC>,\n ) => Expression<BooleanCodecType>,\n ): GroupedQuery<QC, AvailableScope, RowType> {\n const combined = orderByScopeOf(\n this.state.scope as AvailableScope,\n this.state.rowFields as RowType,\n );\n const fns = createAggregateFunctions(this.ctx.queryOperationTypes);\n const result = expr(createFieldProxy(combined), fns);\n return new GroupedQueryImpl(cloneState(this.state, { having: result.buildAst() }), this.ctx);\n }\n\n orderBy(\n field: (keyof RowType | keyof AvailableScope['topLevel']) & string,\n options?: OrderByOptions,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n orderBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: AggregateFunctions<QC>,\n ) => Expression<ScopeField>,\n options?: OrderByOptions,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n orderBy(arg: unknown, options?: OrderByOptions): unknown {\n const item = resolveOrderBy(\n arg,\n options,\n this.state.scope,\n this.state.rowFields,\n this.ctx,\n true,\n );\n return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));\n }\n}\n","import {\n AndExpr,\n DerivedTableSource,\n JoinAst,\n type TableSource,\n} from '@prisma-next/sql-relational-core/ast';\nimport type {\n AggregateFunctions,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n WithField,\n WithFields,\n} from '../expression';\nimport type {\n EmptyRow,\n Expand,\n JoinOuterScope,\n JoinSource,\n MergeScopes,\n NullableScope,\n QueryContext,\n Scope,\n ScopeField,\n ScopeTable,\n Subquery,\n} from '../scope';\nimport type { JoinedTables } from '../types/joined-tables';\nimport type { SelectQuery } from '../types/select-query';\nimport type { LateralBuilder } from '../types/shared';\nimport {\n BuilderBase,\n type BuilderContext,\n type BuilderState,\n cloneState,\n emptyState,\n mergeScopes,\n nullableScope,\n resolveSelectArgs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createFunctions } from './functions';\nimport { SelectQueryImpl } from './query-impl';\n\nexport class JoinedTablesImpl<QC extends QueryContext, AvailableScope extends Scope>\n extends BuilderBase<QC['capabilities']>\n implements JoinedTables<QC, AvailableScope>\n{\n readonly #state: BuilderState;\n\n constructor(state: BuilderState, ctx: BuilderContext) {\n super(ctx);\n this.#state = state;\n }\n\n lateralJoin = this._gate(\n { sql: { lateral: true } },\n 'lateralJoin',\n <Alias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: Alias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<AvailableScope, { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> }>\n > => {\n const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);\n const resultScope = mergeScopes(\n this.#state.scope as AvailableScope,\n lateralScope as { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> },\n );\n return this.#addLateralJoin('inner', resultScope, derivedSource);\n },\n ) as JoinedTables<QC, AvailableScope>['lateralJoin'];\n\n outerLateralJoin = this._gate(\n { sql: { lateral: true } },\n 'outerLateralJoin',\n <Alias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: Alias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<\n AvailableScope,\n NullableScope<{ topLevel: LateralRow; namespaces: Record<Alias, LateralRow> }>\n >\n > => {\n const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);\n const resultScope = mergeScopes(\n this.#state.scope as AvailableScope,\n nullableScope(\n lateralScope as { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> },\n ),\n );\n return this.#addLateralJoin('left', resultScope, derivedSource);\n },\n ) as JoinedTables<QC, AvailableScope>['outerLateralJoin'];\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<EmptyRow, AvailableScope['topLevel'], Columns>>;\n select<Alias extends string, Field extends ScopeField>(\n alias: Alias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<EmptyRow, Field, Alias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n const { projections, newRowFields } = resolveSelectArgs(args, this.#state.scope, this.ctx);\n return new SelectQueryImpl<QC, AvailableScope>(\n cloneState(this.#state, {\n projections: [...this.#state.projections, ...projections],\n rowFields: { ...this.#state.rowFields, ...newRowFields },\n }),\n this.ctx,\n );\n }\n\n innerJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>> {\n const targetScope = mergeScopes(\n this.#state.scope as AvailableScope,\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n );\n return this.#addJoin(other, 'inner', targetScope, on);\n }\n\n outerLeftJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, NullableScope<Other[typeof JoinOuterScope]>>> {\n const targetScope = mergeScopes(\n this.#state.scope as AvailableScope,\n nullableScope(other.getJoinOuterScope() as Other[typeof JoinOuterScope]),\n );\n return this.#addJoin(other, 'left', targetScope, on);\n }\n\n outerRightJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<NullableScope<AvailableScope>, Other[typeof JoinOuterScope]>> {\n const targetScope = mergeScopes(\n nullableScope(this.#state.scope as AvailableScope),\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n );\n return this.#addJoin(other, 'right', targetScope, on);\n }\n\n outerFullJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<\n QC,\n MergeScopes<NullableScope<AvailableScope>, NullableScope<Other[typeof JoinOuterScope]>>\n > {\n const targetScope = mergeScopes(\n nullableScope(this.#state.scope as AvailableScope),\n nullableScope(other.getJoinOuterScope() as Other[typeof JoinOuterScope]),\n );\n return this.#addJoin(other, 'full', targetScope, on);\n }\n\n #addJoin<Other extends JoinSource<ScopeTable, string | never>, ResultScope extends Scope>(\n other: Other,\n joinType: 'inner' | 'left' | 'right' | 'full',\n resultScope: ResultScope,\n onExpr: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, ResultScope> {\n const fieldProxy = createFieldProxy(\n mergeScopes(\n this.#state.scope as AvailableScope,\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n ),\n ) as FieldProxy<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>>;\n const fns = createFunctions<QC>(this.ctx.queryOperationTypes);\n const onResult = onExpr(fieldProxy, fns);\n const joinAst = new JoinAst(joinType, other.buildAst(), onResult.buildAst());\n\n return new JoinedTablesImpl(\n cloneState(this.#state, {\n joins: [...this.#state.joins, joinAst],\n scope: resultScope,\n }),\n this.ctx,\n );\n }\n\n #buildLateral(\n alias: string,\n builderFn: (\n lateral: LateralBuilder<QC, AvailableScope>,\n ) => Subquery<Record<string, ScopeField>>,\n ) {\n const lateralBuilder: LateralBuilder<QC, AvailableScope> = {\n from: (other) => {\n const otherScope = other.getJoinOuterScope();\n const parentMerged = mergeScopes(this.#state.scope, otherScope);\n return new SelectQueryImpl(\n emptyState(other.buildAst() as TableSource, parentMerged),\n this.ctx,\n ) as unknown as SelectQuery<QC, AvailableScope, EmptyRow>;\n },\n };\n\n const subquery = builderFn(lateralBuilder);\n const subqueryAst = subquery.buildAst();\n const derivedSource = DerivedTableSource.as(alias, subqueryAst);\n const subqueryRowFields: ScopeTable = subquery.getRowFields();\n const lateralScope: Scope = {\n topLevel: subqueryRowFields,\n namespaces: { [alias]: subqueryRowFields },\n };\n\n return { derivedSource, lateralScope };\n }\n\n #addLateralJoin<ResultScope extends Scope>(\n joinType: 'inner' | 'left',\n resultScope: ResultScope,\n derivedSource: DerivedTableSource,\n ): JoinedTables<QC, ResultScope> {\n const onExpr = AndExpr.of([]);\n const joinAst = new JoinAst(joinType, derivedSource, onExpr, true);\n\n return new JoinedTablesImpl(\n cloneState(this.#state, {\n joins: [...this.#state.joins, joinAst],\n scope: resultScope,\n }),\n this.ctx,\n );\n }\n}\n","import type { StorageTable } from '@prisma-next/sql-contract/types';\nimport {\n type AnyExpression as AstExpression,\n ColumnRef,\n DeleteAst,\n InsertAst,\n ParamRef,\n ProjectionItem,\n TableSource,\n UpdateAst,\n} from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type { MutationDefaultsOp } from '@prisma-next/sql-relational-core/query-lane-context';\nimport type { ExpressionBuilder } from '../expression';\nimport type { ResolveRow } from '../resolve';\nimport type { QueryContext, Scope, ScopeField } from '../scope';\nimport type {\n DeleteQuery,\n InsertQuery,\n ReturningCapability,\n UpdateQuery,\n} from '../types/mutation-query';\nimport {\n BuilderBase,\n type BuilderContext,\n buildQueryPlan,\n combineWhereExprs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createFunctions } from './functions';\n\ntype WhereCallback = ExpressionBuilder<Scope, QueryContext>;\n\nfunction buildParamValues(\n values: Record<string, unknown>,\n table: StorageTable,\n tableName: string,\n op: MutationDefaultsOp,\n ctx: BuilderContext,\n): Record<string, ParamRef> {\n const params: Record<string, ParamRef> = {};\n for (const [col, value] of Object.entries(values)) {\n const column = table.columns[col];\n params[col] = ParamRef.of(value, column ? { codecId: column.codecId } : undefined);\n }\n for (const def of ctx.applyMutationDefaults({ op, table: tableName, values })) {\n const column = table.columns[def.column];\n params[def.column] = ParamRef.of(def.value, column ? { codecId: column.codecId } : undefined);\n }\n return params;\n}\n\nfunction buildReturningProjections(\n tableName: string,\n columns: string[],\n rowFields: Record<string, ScopeField>,\n): ProjectionItem[] {\n return columns.map((col) =>\n ProjectionItem.of(col, ColumnRef.of(tableName, col), rowFields[col]?.codecId),\n );\n}\n\nfunction evaluateWhere(\n whereCallback: WhereCallback,\n scope: Scope,\n queryOperationTypes: BuilderContext['queryOperationTypes'],\n): AstExpression {\n const fieldProxy = createFieldProxy(scope);\n const fns = createFunctions(queryOperationTypes);\n const result = whereCallback(fieldProxy, fns as never);\n return result.buildAst();\n}\n\nexport class InsertQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements InsertQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #scope: Scope;\n readonly #values: Record<string, unknown>;\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n table: StorageTable,\n scope: Scope,\n values: Record<string, unknown>,\n ctx: BuilderContext,\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = scope;\n this.#values = values;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n returning = this._gate<ReturningCapability, string[], InsertQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new InsertQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#values,\n this.ctx,\n columns,\n newRowFields,\n ) as unknown as InsertQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const paramValues = buildParamValues(\n this.#values,\n this.#table,\n this.#tableName,\n 'create',\n this.ctx,\n );\n\n let ast = InsertAst.into(TableSource.named(this.#tableName)).withValues(paramValues);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n\nexport class UpdateQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements UpdateQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #scope: Scope;\n readonly #setValues: Record<string, unknown>;\n readonly #whereCallbacks: readonly WhereCallback[];\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n table: StorageTable,\n scope: Scope,\n setValues: Record<string, unknown>,\n ctx: BuilderContext,\n whereCallbacks: readonly WhereCallback[] = [],\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = scope;\n this.#setValues = setValues;\n this.#whereCallbacks = whereCallbacks;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): UpdateQuery<QC, AvailableScope, RowType> {\n return new UpdateQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#setValues,\n this.ctx,\n [...this.#whereCallbacks, expr as unknown as WhereCallback],\n this.#returningColumns,\n this.#rowFields,\n );\n }\n\n returning = this._gate<ReturningCapability, string[], UpdateQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new UpdateQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#setValues,\n this.ctx,\n this.#whereCallbacks,\n columns,\n newRowFields,\n ) as unknown as UpdateQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const setParams = buildParamValues(\n this.#setValues,\n this.#table,\n this.#tableName,\n 'update',\n this.ctx,\n );\n\n const whereExpr = combineWhereExprs(\n this.#whereCallbacks.map((cb) =>\n evaluateWhere(cb, this.#scope, this.ctx.queryOperationTypes),\n ),\n );\n\n let ast = UpdateAst.table(TableSource.named(this.#tableName))\n .withSet(setParams)\n .withWhere(whereExpr);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n\nexport class DeleteQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements DeleteQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #scope: Scope;\n readonly #whereCallbacks: readonly WhereCallback[];\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n scope: Scope,\n ctx: BuilderContext,\n whereCallbacks: readonly WhereCallback[] = [],\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#scope = scope;\n this.#whereCallbacks = whereCallbacks;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): DeleteQuery<QC, AvailableScope, RowType> {\n return new DeleteQueryImpl(\n this.#tableName,\n this.#scope,\n this.ctx,\n [...this.#whereCallbacks, expr as unknown as WhereCallback],\n this.#returningColumns,\n this.#rowFields,\n );\n }\n\n returning = this._gate<ReturningCapability, string[], DeleteQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new DeleteQueryImpl(\n this.#tableName,\n this.#scope,\n this.ctx,\n this.#whereCallbacks,\n columns,\n newRowFields,\n ) as unknown as DeleteQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const whereExpr = combineWhereExprs(\n this.#whereCallbacks.map((cb) =>\n evaluateWhere(cb, this.#scope, this.ctx.queryOperationTypes),\n ),\n );\n\n let ast = DeleteAst.from(TableSource.named(this.#tableName)).withWhere(whereExpr);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n","import type { StorageTable } from '@prisma-next/sql-contract/types';\nimport { type AnyFromSource, TableSource } from '@prisma-next/sql-relational-core/ast';\nimport type {\n AggregateFunctions,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n WithField,\n WithFields,\n} from '../expression';\nimport type {\n EmptyRow,\n Expand,\n JoinOuterScope,\n JoinSource,\n MergeScopes,\n NullableScope,\n QueryContext,\n RebindScope,\n Scope,\n ScopeField,\n ScopeTable,\n StorageTableToScopeTable,\n Subquery,\n} from '../scope';\nimport type { TableProxyContract } from '../types/db';\nimport type { JoinedTables } from '../types/joined-tables';\nimport type { DeleteQuery, InsertQuery, UpdateQuery } from '../types/mutation-query';\nimport type { SelectQuery } from '../types/select-query';\nimport type { LateralBuilder } from '../types/shared';\nimport type { TableProxy } from '../types/table-proxy';\nimport { BuilderBase, type BuilderContext, emptyState, tableToScope } from './builder-base';\nimport { JoinedTablesImpl } from './joined-tables-impl';\nimport { DeleteQueryImpl, InsertQueryImpl, UpdateQueryImpl } from './mutation-impl';\nimport { SelectQueryImpl } from './query-impl';\n\nexport class TableProxyImpl<\n C extends TableProxyContract,\n Name extends string & keyof C['storage']['tables'],\n Alias extends string,\n AvailableScope extends Scope,\n QC extends QueryContext,\n >\n extends BuilderBase<C['capabilities']>\n implements TableProxy<C, Name, Alias, AvailableScope, QC>\n{\n declare readonly [JoinOuterScope]: JoinSource<\n StorageTableToScopeTable<C['storage']['tables'][Name]>,\n Alias\n >[typeof JoinOuterScope];\n\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #fromSource: TableSource;\n readonly #scope: Scope;\n\n constructor(tableName: string, table: StorageTable, alias: string, ctx: BuilderContext) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = tableToScope(alias, table);\n this.#fromSource = TableSource.named(tableName, alias !== tableName ? alias : undefined);\n }\n\n lateralJoin = this._gate(\n { sql: { lateral: true } },\n 'lateralJoin',\n <LAlias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: LAlias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<AvailableScope, { topLevel: LateralRow; namespaces: Record<LAlias, LateralRow> }>\n > => {\n return this.#toJoined().lateralJoin(alias, builder);\n },\n ) as TableProxy<C, Name, Alias, AvailableScope, QC>['lateralJoin'];\n\n outerLateralJoin = this._gate(\n { sql: { lateral: true } },\n 'outerLateralJoin',\n <LAlias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: LAlias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<\n AvailableScope,\n NullableScope<{ topLevel: LateralRow; namespaces: Record<LAlias, LateralRow> }>\n >\n > => {\n return this.#toJoined().outerLateralJoin(alias, builder);\n },\n ) as TableProxy<C, Name, Alias, AvailableScope, QC>['outerLateralJoin'];\n\n getJoinOuterScope(): Scope {\n return this.#scope;\n }\n\n buildAst(): AnyFromSource {\n return this.#fromSource;\n }\n\n as<NewAlias extends string>(\n newAlias: NewAlias,\n ): TableProxy<C, Name, NewAlias, RebindScope<AvailableScope, Alias, NewAlias>> {\n return new TableProxyImpl(this.#tableName, this.#table, newAlias, this.ctx);\n }\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<EmptyRow, AvailableScope['topLevel'], Columns>>;\n select<LAlias extends string, Field extends ScopeField>(\n alias: LAlias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<EmptyRow, Field, LAlias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n return new SelectQueryImpl(emptyState(this.#fromSource, this.#scope), this.ctx).select(\n ...(args as string[]),\n );\n }\n\n innerJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>> {\n return this.#toJoined().innerJoin(other, on);\n }\n\n outerLeftJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, NullableScope<Other[typeof JoinOuterScope]>>> {\n return this.#toJoined().outerLeftJoin(other, on);\n }\n\n outerRightJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<NullableScope<AvailableScope>, Other[typeof JoinOuterScope]>> {\n return this.#toJoined().outerRightJoin(other, on);\n }\n\n outerFullJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<\n QC,\n MergeScopes<NullableScope<AvailableScope>, NullableScope<Other[typeof JoinOuterScope]>>\n > {\n return this.#toJoined().outerFullJoin(other, on);\n }\n\n insert(values: Record<string, unknown>): InsertQuery<QC, AvailableScope, EmptyRow> {\n return new InsertQueryImpl(this.#tableName, this.#table, this.#scope, values, this.ctx);\n }\n\n update(set: Record<string, unknown>): UpdateQuery<QC, AvailableScope, EmptyRow> {\n return new UpdateQueryImpl(this.#tableName, this.#table, this.#scope, set, this.ctx);\n }\n\n delete(): DeleteQuery<QC, AvailableScope, EmptyRow> {\n return new DeleteQueryImpl(this.#tableName, this.#scope, this.ctx);\n }\n\n #toJoined(): JoinedTables<QC, AvailableScope> {\n return new JoinedTablesImpl(emptyState(this.#fromSource, this.#scope), this.ctx);\n }\n}\n","import type { Contract } from '@prisma-next/contract/types';\nimport type { SqlStorage } from '@prisma-next/sql-contract/types';\nimport type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';\nimport type { Db } from '../types/db';\nimport type { BuilderContext } from './builder-base';\nimport { TableProxyImpl } from './table-proxy-impl';\n\nexport interface SqlOptions<C extends Contract<SqlStorage>> {\n readonly context: ExecutionContext<C>;\n}\n\nexport function sql<C extends Contract<SqlStorage>>(options: SqlOptions<C>): Db<C> {\n const { context } = options;\n const ctx: BuilderContext = {\n capabilities: context.contract.capabilities,\n queryOperationTypes: context.queryOperations.entries(),\n target: context.contract.target ?? 'unknown',\n storageHash: context.contract.storage.storageHash ?? 'unknown',\n applyMutationDefaults: (options) => context.applyMutationDefaults(options),\n };\n\n return new Proxy({} as Db<C>, {\n get(_target, prop: string) {\n const tables = context.contract.storage.tables;\n const table = Object.hasOwn(tables, prop) ? tables[prop] : undefined;\n if (table) {\n return new TableProxyImpl(prop, table, prop, ctx);\n }\n return undefined;\n },\n });\n}\n"],"mappings":";;;;;;;;;AASA,IAAa,iBAAb,MAAwF;CACtF,AAAiB;CACjB,AAAS;CAET,YAAY,KAAoB,YAAe;AAC7C,OAAK,MAAM;AACX,OAAK,aAAa;;CAGpB,WAA0B;AACxB,SAAO,KAAK;;;;;;ACdhB,SAAgB,iBAAkC,OAAyB;AACzE,QAAO,IAAI,MAAM,EAAE,EAAmB,EACpC,IAAI,SAAS,MAAc;AACzB,MAAI,OAAO,OAAO,MAAM,UAAU,KAAK,EAAE;GACvC,MAAM,WAAW,MAAM,SAAS;AAChC,OAAI,SAAU,QAAO,IAAI,eAAe,cAAc,GAAG,KAAK,EAAE,SAAS;;AAG3E,MAAI,OAAO,OAAO,MAAM,YAAY,KAAK,EAAE;GACzC,MAAM,WAAW,MAAM,WAAW;AAClC,OAAI,SAAU,QAAO,qBAAqB,MAAM,SAAS;;IAK9D,CAAC;;AAGJ,SAAS,qBACP,eACA,QACgC;AAChC,QAAO,IAAI,MAAM,EAA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B,MAAKH,WAAY,MAAKG,OAAQ,KAAK,IAAI;;CAGpE,YAA8C;AAC5C,SAAO,IAAI,iBAAiB,WAAW,MAAKD,YAAa,MAAKC,MAAO,EAAE,KAAK,IAAI;;;;;;AC/JpF,SAAgB,IAAoC,SAA+B;CACjF,MAAM,EAAE,YAAY;CACpB,MAAME,MAAsB;EAC1B,cAAc,QAAQ,SAAS;EAC/B,qBAAqB,QAAQ,gBAAgB,SAAS;EACtD,QAAQ,QAAQ,SAAS,UAAU;EACnC,aAAa,QAAQ,SAAS,QAAQ,eAAe;EACrD,wBAAwB,cAAY,QAAQ,sBAAsBC,UAAQ;EAC3E;AAED,QAAO,IAAI,MAAM,EAAE,EAAW,EAC5B,IAAI,SAAS,MAAc;EACzB,MAAM,SAAS,QAAQ,SAAS,QAAQ;EACxC,MAAM,QAAQ,OAAO,OAAO,QAAQ,KAAK,GAAG,OAAO,QAAQ;AAC3D,MAAI,MACF,QAAO,IAAI,eAAe,MAAM,OAAO,MAAM,IAAI;IAItD,CAAC"}
1
+ {"version":3,"file":"index.mjs","names":["findUniqueNamespaceFor","found: string | undefined","BOOL_FIELD: BooleanCodecType","found: string | undefined","meta: PlanMeta","fields: ScopeTable","topLevel: ScopeTable","result: ScopeTable","namespaces: Record<string, ScopeTable>","projections: ProjectionItem[]","newRowFields: Record<string, ScopeField>","combined","#state","#buildLateral","#addLateralJoin","#addJoin","subqueryRowFields: ScopeTable","params: Record<string, ParamRef>","#tableName","#table","#scope","#values","#returningColumns","#rowFields","newRowFields: Record<string, ScopeField>","#setValues","#whereCallbacks","#tableName","#table","#fromSource","#scope","#toJoined","ctx: BuilderContext","options"],"sources":["../../src/runtime/expression-impl.ts","../../src/runtime/field-proxy.ts","../../src/runtime/functions.ts","../../src/runtime/builder-base.ts","../../src/runtime/query-impl.ts","../../src/runtime/joined-tables-impl.ts","../../src/runtime/mutation-impl.ts","../../src/runtime/table-proxy-impl.ts","../../src/runtime/sql.ts"],"sourcesContent":["import type { AnyExpression as AstExpression } from '@prisma-next/sql-relational-core/ast';\nimport type { Expression } from '@prisma-next/sql-relational-core/expression';\nimport type { ScopeField } from '../scope';\n\n/**\n * Runtime wrapper around a relational-core AST expression node. Carries ScopeField metadata (codecId, nullable) so aggregate-like combinators can propagate the input codec onto their result.\n *\n * `refs` records the column-bound binding (`{ table, column }`) when known — the field-proxy populates it for both the namespaced form (`f.user.email` → `ColumnRef`) and the top-level shortcut (`f.email` → `IdentifierRef` + refs metadata). Encode-side dispatch and the `validateParamRefRefs` pass read it via `refsOf(expression)`.\n */\nexport class ExpressionImpl<T extends ScopeField = ScopeField> implements Expression<T> {\n private readonly ast: AstExpression;\n readonly returnType: T;\n readonly refs: { readonly table: string; readonly column: string } | undefined;\n\n constructor(\n ast: AstExpression,\n returnType: T,\n refs?: { readonly table: string; readonly column: string },\n ) {\n this.ast = ast;\n this.returnType = returnType;\n this.refs = refs;\n }\n\n buildAst(): AstExpression {\n return this.ast;\n }\n}\n","import { ColumnRef, IdentifierRef } from '@prisma-next/sql-relational-core/ast';\nimport type { FieldProxy } from '../expression';\nimport type { Scope, ScopeTable } from '../scope';\nimport { ExpressionImpl } from './expression-impl';\n\n/**\n * For a top-level field name, find the namespace (table alias) that contributed it. When exactly one namespace owns the field, the top-level binding is unambiguously column-bound and we record that `(table, column)` pair on the `ExpressionImpl` so encode-side dispatch (`forColumn`) and the `validateParamRefRefs` pass can find it. The AST stays as `IdentifierRef` to preserve SQL rendering — adapters render top-level\n * identifiers without an explicit table qualifier — so this change is metadata-only and produces no SQL drift.\n */\nfunction findUniqueNamespaceFor(scope: Scope, fieldName: string): string | undefined {\n let found: string | undefined;\n for (const [namespace, fields] of Object.entries(scope.namespaces)) {\n if (Object.hasOwn(fields, fieldName)) {\n if (found !== undefined) return undefined;\n found = namespace;\n }\n }\n return found;\n}\n\nexport function createFieldProxy<S extends Scope>(scope: S): FieldProxy<S> {\n return new Proxy({} as FieldProxy<S>, {\n get(_target, prop: string) {\n if (Object.hasOwn(scope.topLevel, prop)) {\n const topField = scope.topLevel[prop];\n if (topField) {\n const namespace = findUniqueNamespaceFor(scope, prop);\n const refs = namespace ? { table: namespace, column: prop } : undefined;\n return new ExpressionImpl(IdentifierRef.of(prop), topField, refs);\n }\n }\n\n if (Object.hasOwn(scope.namespaces, prop)) {\n const nsFields = scope.namespaces[prop];\n if (nsFields) return createNamespaceProxy(prop, nsFields);\n }\n\n return undefined;\n },\n });\n}\n\nfunction createNamespaceProxy(\n namespaceName: string,\n fields: ScopeTable,\n): Record<string, ExpressionImpl> {\n return new Proxy({} as Record<string, ExpressionImpl>, {\n get(_target, prop: string) {\n if (Object.hasOwn(fields, prop)) {\n const field = fields[prop];\n if (field) return new ExpressionImpl(ColumnRef.of(namespaceName, prop), field);\n }\n return undefined;\n },\n });\n}\n","import type { SqlOperationEntry } from '@prisma-next/sql-operations';\nimport {\n AggregateExpr,\n AndExpr,\n type AnyExpression as AstExpression,\n BinaryExpr,\n type BinaryOp,\n ExistsExpr,\n ListExpression,\n LiteralExpr,\n NullCheckExpr,\n OrExpr,\n SubqueryExpr,\n} from '@prisma-next/sql-relational-core/ast';\nimport { refsOf, toExpr } from '@prisma-next/sql-relational-core/expression';\nimport type {\n AggregateFunctions,\n AggregateOnlyFunctions,\n BooleanCodecType,\n BuiltinFunctions,\n CodecExpression,\n Expression,\n Functions,\n} from '../expression';\nimport type { QueryContext, ScopeField, Subquery } from '../scope';\nimport { ExpressionImpl } from './expression-impl';\n\ntype CodecTypes = Record<string, { readonly input: unknown }>;\n// Runtime-level ExprOrVal — accepts any codec, any nullability. Concrete codec typing lives on the public BuiltinFunctions surface in `../expression`.\ntype ExprOrVal<CodecId extends string = string, N extends boolean = boolean> = CodecExpression<\n CodecId,\n N,\n CodecTypes\n>;\n\nconst BOOL_FIELD: BooleanCodecType = { codecId: 'pg/bool@1', nullable: false };\n\nconst resolve = toExpr;\n\n/**\n * Resolve a binary-comparison operand into an AST expression, threading the column-bound side's `codecId` + `refs` to the raw-value side.\n *\n * For `fns.eq(f.email, 'alice@example.com')`, `f.email` is the column-bound expression carrying a `ColumnRef` AST and a `returnType.codecId` (`pg/varchar@1`); the raw string operand has no codec context. By deriving the codec context from the column-bound side and forwarding it via `toExpr(value, codecId, refs)`, the resulting `ParamRef` carries the column refs that encode-side `forColumn` dispatch needs (and that the\n * validator pass requires for parameterized codec ids like `pg/varchar@1` with a length parameter).\n */\nfunction resolveOperand(\n operand: ExprOrVal,\n otherCodecId?: string,\n otherRefs?: { table: string; column: string },\n): AstExpression {\n if (isExpressionLike(operand)) return operand.buildAst();\n return toExpr(operand, otherCodecId, otherRefs);\n}\n\nfunction isExpressionLike(\n value: unknown,\n): value is { buildAst: () => AstExpression; returnType?: { codecId: string } } {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'buildAst' in value &&\n typeof (value as { buildAst: unknown }).buildAst === 'function'\n );\n}\n\nfunction operandCodecId(operand: ExprOrVal): string | undefined {\n if (!isExpressionLike(operand)) return undefined;\n return (operand as { returnType?: { codecId: string } }).returnType?.codecId;\n}\n\nfunction operandRefs(operand: ExprOrVal): { table: string; column: string } | undefined {\n return refsOf(operand);\n}\n\n/**\n * Resolves an Expression via `buildAst()`, or wraps a raw value as a `LiteralExpr` — an SQL literal inlined into the query text, not a bound parameter.\n *\n * Used for `and` / `or` operands. The usual operand is an `Expression<bool>` (e.g. the result of `fns.eq`), which this function passes through by calling `buildAst()`. The only time the raw-value branch fires is when the caller writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals lets the SQL planner statically simplify `TRUE AND x` to `x`, which it cannot do for an opaque `ParamRef`.\n */\nfunction toLiteralExpr(value: unknown): AstExpression {\n if (\n typeof value === 'object' &&\n value !== null &&\n 'buildAst' in value &&\n typeof (value as { buildAst: unknown }).buildAst === 'function'\n ) {\n return (value as { buildAst(): AstExpression }).buildAst();\n }\n return new LiteralExpr(value);\n}\n\nfunction boolExpr(astNode: AstExpression): ExpressionImpl<BooleanCodecType> {\n return new ExpressionImpl(astNode, BOOL_FIELD);\n}\n\nfunction binaryWithSharedCodec(\n a: ExprOrVal,\n b: ExprOrVal,\n build: (left: AstExpression, right: AstExpression) => AstExpression,\n): AstExpression {\n const aCodecId = operandCodecId(a);\n const bCodecId = operandCodecId(b);\n const aRefs = operandRefs(a);\n const bRefs = operandRefs(b);\n const left = resolveOperand(a, bCodecId, bRefs);\n const right = resolveOperand(b, aCodecId, aRefs);\n return build(left, right);\n}\n\nfunction eq(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {\n if (b === null) return boolExpr(NullCheckExpr.isNull(resolve(a)));\n if (a === null) return boolExpr(NullCheckExpr.isNull(resolve(b)));\n return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr('eq', l, r)));\n}\n\nfunction ne(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {\n if (b === null) return boolExpr(NullCheckExpr.isNotNull(resolve(a)));\n if (a === null) return boolExpr(NullCheckExpr.isNotNull(resolve(b)));\n return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr('neq', l, r)));\n}\n\nfunction comparison(a: ExprOrVal, b: ExprOrVal, op: BinaryOp): ExpressionImpl<BooleanCodecType> {\n return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr(op, l, r)));\n}\n\nfunction inOrNotIn(\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n op: 'in' | 'notIn',\n): ExpressionImpl<BooleanCodecType> {\n const left = expr.buildAst();\n const leftCodecId = expr.returnType.codecId;\n const leftRefs = refsOf(expr);\n const binaryFn = op === 'in' ? BinaryExpr.in : BinaryExpr.notIn;\n\n if (Array.isArray(valuesOrSubquery)) {\n const refs = valuesOrSubquery.map((v) => resolveOperand(v, leftCodecId, leftRefs));\n return boolExpr(binaryFn(left, ListExpression.of(refs)));\n }\n return boolExpr(binaryFn(left, SubqueryExpr.of(valuesOrSubquery.buildAst())));\n}\n\nfunction numericAgg(\n fn: 'sum' | 'avg' | 'min' | 'max',\n expr: Expression<ScopeField>,\n): ExpressionImpl<{ codecId: string; nullable: true }> {\n return new ExpressionImpl(AggregateExpr[fn](expr.buildAst()), {\n codecId: expr.returnType.codecId,\n nullable: true as const,\n });\n}\n\nfunction createBuiltinFunctions() {\n return {\n eq: (a: ExprOrVal, b: ExprOrVal) => eq(a, b),\n ne: (a: ExprOrVal, b: ExprOrVal) => ne(a, b),\n gt: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'gt'),\n gte: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'gte'),\n lt: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'lt'),\n lte: (a: ExprOrVal, b: ExprOrVal) => comparison(a, b, 'lte'),\n and: (...exprs: ExprOrVal<'pg/bool@1', boolean>[]) =>\n boolExpr(AndExpr.of(exprs.map(toLiteralExpr))),\n or: (...exprs: ExprOrVal<'pg/bool@1', boolean>[]) =>\n boolExpr(OrExpr.of(exprs.map(toLiteralExpr))),\n exists: (subquery: Subquery<Record<string, ScopeField>>) =>\n boolExpr(ExistsExpr.exists(subquery.buildAst())),\n notExists: (subquery: Subquery<Record<string, ScopeField>>) =>\n boolExpr(ExistsExpr.notExists(subquery.buildAst())),\n in: (\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n ) => inOrNotIn(expr, valuesOrSubquery, 'in'),\n notIn: (\n expr: Expression<ScopeField>,\n valuesOrSubquery: Subquery<Record<string, ScopeField>> | ExprOrVal[],\n ) => inOrNotIn(expr, valuesOrSubquery, 'notIn'),\n } satisfies BuiltinFunctions<CodecTypes>;\n}\n\nfunction createAggregateOnlyFunctions() {\n return {\n count: (expr?: Expression<ScopeField>) => {\n const astExpr = expr ? expr.buildAst() : undefined;\n return new ExpressionImpl(AggregateExpr.count(astExpr), {\n codecId: 'pg/int8@1',\n nullable: false,\n });\n },\n sum: (expr: Expression<ScopeField>) => numericAgg('sum', expr),\n avg: (expr: Expression<ScopeField>) => numericAgg('avg', expr),\n min: (expr: Expression<ScopeField>) => numericAgg('min', expr),\n max: (expr: Expression<ScopeField>) => numericAgg('max', expr),\n } satisfies AggregateOnlyFunctions;\n}\n\nexport function createFunctions<QC extends QueryContext>(\n operations: Readonly<Record<string, SqlOperationEntry>>,\n): Functions<QC> {\n const builtins = createBuiltinFunctions();\n\n return new Proxy({} as Functions<QC>, {\n get(_target, prop: string) {\n const builtin = (builtins as Record<string, unknown>)[prop];\n if (builtin) return builtin;\n\n const op = operations[prop];\n if (op) return op.impl;\n return undefined;\n },\n });\n}\n\nexport function createAggregateFunctions<QC extends QueryContext>(\n operations: Readonly<Record<string, SqlOperationEntry>>,\n): AggregateFunctions<QC> {\n const baseFns = createFunctions<QC>(operations);\n const aggregates = createAggregateOnlyFunctions();\n\n return new Proxy({} as AggregateFunctions<QC>, {\n get(_target, prop: string) {\n const agg = (aggregates as Record<string, unknown>)[prop];\n if (agg) return agg;\n\n return (baseFns as Record<string, unknown>)[prop];\n },\n });\n}\n","import type { PlanMeta } from '@prisma-next/contract/types';\nimport type { StorageTable } from '@prisma-next/sql-contract/types';\nimport type { SqlOperationEntry } from '@prisma-next/sql-operations';\nimport {\n AndExpr,\n type AnyExpression as AstExpression,\n collectOrderedParamRefs,\n IdentifierRef,\n OrderByItem,\n ProjectionItem,\n SelectAst,\n type TableSource,\n} from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type {\n AppliedMutationDefault,\n MutationDefaultsOptions,\n} from '@prisma-next/sql-relational-core/query-lane-context';\nimport type {\n AggregateFunctions,\n Expression,\n FieldProxy,\n OrderByOptions,\n OrderByScope,\n} from '../expression';\nimport type {\n GatedMethod,\n MergeScopes,\n NullableScope,\n QueryContext,\n Scope,\n ScopeField,\n ScopeTable,\n} from '../scope';\nimport { createFieldProxy } from './field-proxy';\nimport { createAggregateFunctions, createFunctions } from './functions';\n\nexport type ExprCallback = (fields: FieldProxy<Scope>, fns: unknown) => Expression<ScopeField>;\n\nexport class BuilderBase<Capabilities = unknown> {\n protected readonly ctx: BuilderContext;\n\n constructor(ctx: BuilderContext) {\n this.ctx = ctx;\n }\n\n protected _gate<Req extends Record<string, Record<string, boolean>>, Args extends unknown[], R>(\n required: Req,\n methodName: string,\n method: (...args: Args) => R,\n ): GatedMethod<Capabilities, Req, (...args: Args) => R> {\n return ((...args: Args): R => {\n assertCapability(this.ctx, required, methodName);\n return method(...args);\n }) as GatedMethod<Capabilities, Req, (...args: Args) => R>;\n }\n}\n\nexport interface BuilderState {\n readonly from: TableSource;\n readonly joins: readonly import('@prisma-next/sql-relational-core/ast').JoinAst[];\n readonly projections: readonly ProjectionItem[];\n readonly where: readonly AstExpression[];\n readonly orderBy: readonly OrderByItem[];\n readonly groupBy: readonly AstExpression[];\n readonly having: AstExpression | undefined;\n readonly limit: number | undefined;\n readonly offset: number | undefined;\n readonly distinct: true | undefined;\n readonly distinctOn: readonly AstExpression[] | undefined;\n readonly scope: Scope;\n readonly rowFields: Record<string, ScopeField>;\n}\n\nexport interface BuilderContext {\n readonly capabilities: Record<string, Record<string, boolean>>;\n readonly queryOperationTypes: Readonly<Record<string, SqlOperationEntry>>;\n readonly target: string;\n readonly storageHash: string;\n readonly applyMutationDefaults: (\n options: MutationDefaultsOptions,\n ) => ReadonlyArray<AppliedMutationDefault>;\n}\n\nexport function emptyState(from: TableSource, scope: Scope): BuilderState {\n return {\n from,\n joins: [],\n projections: [],\n where: [],\n orderBy: [],\n groupBy: [],\n having: undefined,\n limit: undefined,\n offset: undefined,\n distinct: undefined,\n distinctOn: undefined,\n scope,\n rowFields: {},\n };\n}\n\nexport function cloneState(state: BuilderState, overrides: Partial<BuilderState>): BuilderState {\n return { ...state, ...overrides };\n}\n\nexport function combineWhereExprs(exprs: readonly AstExpression[]): AstExpression | undefined {\n if (exprs.length === 0) return undefined;\n if (exprs.length === 1) return exprs[0];\n return AndExpr.of(exprs);\n}\n\n/**\n * Same uniqueness rule as the field-proxy's `findUniqueNamespaceFor`: when exactly one namespace owns a top-level field, the binding is unambiguous. Used by `select('col', ...)` to attach `refs` metadata to the resulting `ProjectionItem` while keeping the AST as `IdentifierRef` (so SQL renders unchanged).\n */\nfunction findUniqueNamespaceFor(scope: Scope, fieldName: string): string | undefined {\n let found: string | undefined;\n for (const [namespace, fields] of Object.entries(scope.namespaces)) {\n if (Object.hasOwn(fields, fieldName)) {\n if (found !== undefined) return undefined;\n found = namespace;\n }\n }\n return found;\n}\n\nexport function buildSelectAst(state: BuilderState): SelectAst {\n const where = combineWhereExprs(state.where);\n return new SelectAst({\n from: state.from,\n joins: state.joins.length > 0 ? state.joins : undefined,\n projection: state.projections,\n where,\n orderBy: state.orderBy.length > 0 ? state.orderBy : undefined,\n distinct: state.distinct,\n distinctOn: state.distinctOn && state.distinctOn.length > 0 ? state.distinctOn : undefined,\n groupBy: state.groupBy.length > 0 ? state.groupBy : undefined,\n having: state.having,\n limit: state.limit,\n offset: state.offset,\n selectAllIntent: undefined,\n });\n}\n\nexport function buildQueryPlan<Row = unknown>(\n ast: import('@prisma-next/sql-relational-core/ast').AnyQueryAst,\n ctx: BuilderContext,\n): SqlQueryPlan<Row> {\n const paramValues = collectOrderedParamRefs(ast).map((r) => r.value);\n\n const meta: PlanMeta = Object.freeze({\n target: ctx.target,\n storageHash: ctx.storageHash,\n lane: 'dsl',\n });\n\n return Object.freeze({ ast, params: paramValues, meta });\n}\n\nexport function buildPlan<Row = unknown>(\n state: BuilderState,\n ctx: BuilderContext,\n): SqlQueryPlan<Row> {\n return buildQueryPlan<Row>(buildSelectAst(state), ctx);\n}\n\nexport function tableToScope(name: string, table: StorageTable): Scope {\n const fields: ScopeTable = {};\n for (const [colName, col] of Object.entries(table.columns)) {\n fields[colName] = { codecId: col.codecId, nullable: col.nullable };\n }\n return { topLevel: { ...fields }, namespaces: { [name]: fields } };\n}\n\nexport function mergeScopes<A extends Scope, B extends Scope>(a: A, b: B): MergeScopes<A, B> {\n const topLevel: ScopeTable = {};\n for (const [k, v] of Object.entries(a.topLevel)) {\n if (!(k in b.topLevel)) topLevel[k] = v;\n }\n for (const [k, v] of Object.entries(b.topLevel)) {\n if (!(k in a.topLevel)) topLevel[k] = v;\n }\n return {\n topLevel,\n namespaces: { ...a.namespaces, ...b.namespaces },\n } as MergeScopes<A, B>;\n}\n\nexport function nullableScope<S extends Scope>(scope: S): NullableScope<S> {\n const mkNullable = (tbl: ScopeTable): ScopeTable => {\n const result: ScopeTable = {};\n for (const [k, v] of Object.entries(tbl)) {\n result[k] = { codecId: v.codecId, nullable: true };\n }\n return result;\n };\n const namespaces: Record<string, ScopeTable> = {};\n for (const [k, v] of Object.entries(scope.namespaces)) {\n namespaces[k] = mkNullable(v);\n }\n return { topLevel: mkNullable(scope.topLevel), namespaces } as NullableScope<S>;\n}\n\nexport function orderByScopeOf<S extends Scope, R extends Record<string, ScopeField>>(\n scope: S,\n rowFields: R,\n): OrderByScope<S, R> {\n return {\n topLevel: { ...scope.topLevel, ...rowFields },\n namespaces: scope.namespaces,\n };\n}\n\nexport function assertCapability(\n ctx: BuilderContext,\n required: Record<string, Record<string, boolean>>,\n methodName: string,\n): void {\n for (const [ns, keys] of Object.entries(required)) {\n for (const key of Object.keys(keys)) {\n if (!ctx.capabilities[ns]?.[key]) {\n throw new Error(`${methodName}() requires capability ${ns}.${key}`);\n }\n }\n }\n}\n\nexport function resolveSelectArgs(\n args: unknown[],\n scope: Scope,\n ctx: BuilderContext,\n): { projections: ProjectionItem[]; newRowFields: Record<string, ScopeField> } {\n const projections: ProjectionItem[] = [];\n const newRowFields: Record<string, ScopeField> = {};\n\n if (args.length === 0) return { projections, newRowFields };\n\n if (typeof args[0] === 'string' && (args.length === 1 || typeof args[1] !== 'function')) {\n for (const colName of args as string[]) {\n const field = scope.topLevel[colName];\n if (!field) throw new Error(`Column \"${colName}\" not found in scope`);\n const namespace = findUniqueNamespaceFor(scope, colName);\n const refs = namespace ? { table: namespace, column: colName } : undefined;\n projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId, refs));\n newRowFields[colName] = field;\n }\n return { projections, newRowFields };\n }\n\n if (typeof args[0] === 'string' && typeof args[1] === 'function') {\n const alias = args[0] as string;\n const exprFn = args[1] as (\n f: FieldProxy<Scope>,\n fns: AggregateFunctions<QueryContext>,\n ) => Expression<ScopeField>;\n const fns = createAggregateFunctions(ctx.queryOperationTypes);\n const result = exprFn(createFieldProxy(scope), fns);\n const field = result.returnType;\n projections.push(ProjectionItem.of(alias, result.buildAst(), field.codecId));\n newRowFields[alias] = field;\n return { projections, newRowFields };\n }\n\n if (typeof args[0] === 'function') {\n const callbackFn = args[0] as (\n f: FieldProxy<Scope>,\n fns: AggregateFunctions<QueryContext>,\n ) => Record<string, Expression<ScopeField>>;\n const fns = createAggregateFunctions(ctx.queryOperationTypes);\n const record = callbackFn(createFieldProxy(scope), fns);\n for (const [key, expr] of Object.entries(record)) {\n const field = expr.returnType;\n projections.push(ProjectionItem.of(key, expr.buildAst(), field.codecId));\n newRowFields[key] = field;\n }\n return { projections, newRowFields };\n }\n\n throw new Error('Invalid .select() arguments');\n}\n\nexport function resolveOrderBy(\n arg: unknown,\n options: OrderByOptions | undefined,\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n useAggregateFns: boolean,\n): OrderByItem {\n const dir = options?.direction ?? 'asc';\n\n if (typeof arg === 'string') {\n const combined = orderByScopeOf(scope, rowFields);\n if (!(arg in combined.topLevel))\n throw new Error(`Column \"${arg}\" not found in scope for orderBy`);\n const expr = IdentifierRef.of(arg);\n return dir === 'asc' ? OrderByItem.asc(expr) : OrderByItem.desc(expr);\n }\n\n if (typeof arg === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = useAggregateFns\n ? createAggregateFunctions(ctx.queryOperationTypes)\n : createFunctions(ctx.queryOperationTypes);\n const result = (arg as ExprCallback)(createFieldProxy(combined), fns);\n return dir === 'asc' ? OrderByItem.asc(result.buildAst()) : OrderByItem.desc(result.buildAst());\n }\n\n throw new Error('Invalid orderBy argument');\n}\n\nexport function resolveGroupBy(\n args: unknown[],\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n): AstExpression[] {\n if (typeof args[0] === 'string') {\n const combined = orderByScopeOf(scope, rowFields);\n return (args as string[]).map((colName) => {\n if (!(colName in combined.topLevel))\n throw new Error(`Column \"${colName}\" not found in scope for groupBy`);\n return IdentifierRef.of(colName);\n });\n }\n\n if (typeof args[0] === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = createFunctions(ctx.queryOperationTypes);\n const result = (args[0] as ExprCallback)(createFieldProxy(combined), fns);\n return [result.buildAst()];\n }\n\n throw new Error('Invalid groupBy arguments');\n}\n\nexport function resolveDistinctOn(\n args: unknown[],\n scope: Scope,\n rowFields: Record<string, ScopeField>,\n ctx: BuilderContext,\n): AstExpression[] {\n if (args.length === 1 && typeof args[0] === 'function') {\n const combined = orderByScopeOf(scope, rowFields);\n const fns = createFunctions(ctx.queryOperationTypes);\n const result = (args[0] as ExprCallback)(createFieldProxy(combined), fns);\n return [result.buildAst()];\n }\n const combined = orderByScopeOf(scope, rowFields);\n return (args as string[]).map((colName) => {\n if (!(colName in combined.topLevel))\n throw new Error(`Column \"${colName}\" not found in scope for distinctOn`);\n return IdentifierRef.of(colName);\n });\n}\n","import { DerivedTableSource, type SelectAst } from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type {\n AggregateFunctions,\n BooleanCodecType,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n Functions,\n OrderByOptions,\n OrderByScope,\n WithField,\n WithFields,\n} from '../expression';\nimport type { ResolveRow } from '../resolve';\nimport type {\n Expand,\n JoinOuterScope,\n JoinSource,\n QueryContext,\n Scope,\n ScopeField,\n // biome-ignore lint/correctness/noUnusedImports: used in `declare` property\n SubqueryMarker,\n} from '../scope';\nimport type { GroupedQuery } from '../types/grouped-query';\nimport type { SelectQuery } from '../types/select-query';\nimport {\n BuilderBase,\n type BuilderContext,\n type BuilderState,\n buildPlan,\n buildSelectAst,\n cloneState,\n orderByScopeOf,\n resolveDistinctOn,\n resolveGroupBy,\n resolveOrderBy,\n resolveSelectArgs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createAggregateFunctions, createFunctions } from './functions';\n\nabstract class QueryBase<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n> extends BuilderBase<QC['capabilities']> {\n protected readonly state: BuilderState;\n\n constructor(state: BuilderState, ctx: BuilderContext) {\n super(ctx);\n this.state = state;\n }\n\n protected abstract clone(state: BuilderState): this;\n\n distinctOn = this._gate(\n { postgres: { distinctOn: true } },\n 'distinctOn',\n (...args: unknown[]) => {\n const exprs = resolveDistinctOn(args, this.state.scope, this.state.rowFields, this.ctx);\n return this.clone(\n cloneState(this.state, {\n distinctOn: [...(this.state.distinctOn ?? []), ...exprs],\n }),\n );\n },\n );\n\n limit(count: number): this {\n return this.clone(cloneState(this.state, { limit: count }));\n }\n\n offset(count: number): this {\n return this.clone(cloneState(this.state, { offset: count }));\n }\n\n distinct(): this {\n return this.clone(cloneState(this.state, { distinct: true }));\n }\n\n groupBy(\n ...fields: ((keyof RowType | keyof AvailableScope['topLevel']) & string)[]\n ): GroupedQuery<QC, AvailableScope, RowType>;\n groupBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: Functions<QC>,\n ) => Expression<ScopeField>,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n groupBy(...args: unknown[]): unknown {\n const exprs = resolveGroupBy(args, this.state.scope, this.state.rowFields, this.ctx);\n return new GroupedQueryImpl<QC, AvailableScope, RowType>(\n cloneState(this.state, { groupBy: [...this.state.groupBy, ...exprs] }),\n this.ctx,\n );\n }\n\n as<Alias extends string>(alias: Alias): JoinSource<RowType, Alias> {\n const ast = buildSelectAst(this.state);\n const derivedSource = DerivedTableSource.as(alias, ast);\n const scope = {\n topLevel: this.state.rowFields as RowType,\n namespaces: { [alias]: this.state.rowFields } as Record<Alias, RowType>,\n };\n return {\n getJoinOuterScope: () => scope,\n buildAst: () => derivedSource,\n\n // `as unknown` is necessary, because JoinOuterScope is a phantom type-only property that does not exist at runtime\n } satisfies Omit<JoinSource<RowType, Alias>, typeof JoinOuterScope> as unknown as JoinSource<\n RowType,\n Alias\n >;\n }\n\n getRowFields(): Record<string, ScopeField> {\n return this.state.rowFields;\n }\n\n buildAst(): SelectAst {\n return buildSelectAst(this.state);\n }\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n return buildPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n this.state,\n this.ctx,\n );\n }\n}\n\nexport class SelectQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends QueryBase<QC, AvailableScope, RowType>\n implements SelectQuery<QC, AvailableScope, RowType>\n{\n declare readonly [SubqueryMarker]: RowType;\n\n protected clone(state: BuilderState): this {\n return new SelectQueryImpl<QC, AvailableScope, RowType>(state, this.ctx) as this;\n }\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<RowType, AvailableScope['topLevel'], Columns>>;\n select<Alias extends string, Field extends ScopeField>(\n alias: Alias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<RowType, Field, Alias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<RowType & ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n const { projections, newRowFields } = resolveSelectArgs(args, this.state.scope, this.ctx);\n return new SelectQueryImpl(\n cloneState(this.state, {\n projections: [...this.state.projections, ...projections],\n rowFields: { ...this.state.rowFields, ...newRowFields },\n }),\n this.ctx,\n );\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): SelectQuery<QC, AvailableScope, RowType> {\n const fieldProxy = createFieldProxy(this.state.scope);\n const fns = createFunctions<QC>(this.ctx.queryOperationTypes);\n const result = (expr as ExpressionBuilder<Scope, QueryContext>)(fieldProxy, fns as never);\n return new SelectQueryImpl(\n cloneState(this.state, {\n where: [...this.state.where, result.buildAst()],\n }),\n this.ctx,\n );\n }\n\n orderBy(\n field: (keyof RowType | keyof AvailableScope['topLevel']) & string,\n options?: OrderByOptions,\n ): SelectQuery<QC, AvailableScope, RowType>;\n orderBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: Functions<QC>,\n ) => Expression<ScopeField>,\n options?: OrderByOptions,\n ): SelectQuery<QC, AvailableScope, RowType>;\n orderBy(arg: unknown, options?: OrderByOptions): unknown {\n const item = resolveOrderBy(\n arg,\n options,\n this.state.scope,\n this.state.rowFields,\n this.ctx,\n false,\n );\n return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));\n }\n}\n\nexport class GroupedQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends QueryBase<QC, AvailableScope, RowType>\n implements GroupedQuery<QC, AvailableScope, RowType>\n{\n declare readonly [SubqueryMarker]: RowType;\n\n protected clone(state: BuilderState): this {\n return new GroupedQueryImpl<QC, AvailableScope, RowType>(state, this.ctx) as this;\n }\n\n having(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: AggregateFunctions<QC>,\n ) => Expression<BooleanCodecType>,\n ): GroupedQuery<QC, AvailableScope, RowType> {\n const combined = orderByScopeOf(\n this.state.scope as AvailableScope,\n this.state.rowFields as RowType,\n );\n const fns = createAggregateFunctions(this.ctx.queryOperationTypes);\n const result = expr(createFieldProxy(combined), fns);\n return new GroupedQueryImpl(cloneState(this.state, { having: result.buildAst() }), this.ctx);\n }\n\n orderBy(\n field: (keyof RowType | keyof AvailableScope['topLevel']) & string,\n options?: OrderByOptions,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n orderBy(\n expr: (\n fields: FieldProxy<OrderByScope<AvailableScope, RowType>>,\n fns: AggregateFunctions<QC>,\n ) => Expression<ScopeField>,\n options?: OrderByOptions,\n ): GroupedQuery<QC, AvailableScope, RowType>;\n orderBy(arg: unknown, options?: OrderByOptions): unknown {\n const item = resolveOrderBy(\n arg,\n options,\n this.state.scope,\n this.state.rowFields,\n this.ctx,\n true,\n );\n return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));\n }\n}\n","import {\n AndExpr,\n DerivedTableSource,\n JoinAst,\n type TableSource,\n} from '@prisma-next/sql-relational-core/ast';\nimport type {\n AggregateFunctions,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n WithField,\n WithFields,\n} from '../expression';\nimport type {\n EmptyRow,\n Expand,\n JoinOuterScope,\n JoinSource,\n MergeScopes,\n NullableScope,\n QueryContext,\n Scope,\n ScopeField,\n ScopeTable,\n Subquery,\n} from '../scope';\nimport type { JoinedTables } from '../types/joined-tables';\nimport type { SelectQuery } from '../types/select-query';\nimport type { LateralBuilder } from '../types/shared';\nimport {\n BuilderBase,\n type BuilderContext,\n type BuilderState,\n cloneState,\n emptyState,\n mergeScopes,\n nullableScope,\n resolveSelectArgs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createFunctions } from './functions';\nimport { SelectQueryImpl } from './query-impl';\n\nexport class JoinedTablesImpl<QC extends QueryContext, AvailableScope extends Scope>\n extends BuilderBase<QC['capabilities']>\n implements JoinedTables<QC, AvailableScope>\n{\n readonly #state: BuilderState;\n\n constructor(state: BuilderState, ctx: BuilderContext) {\n super(ctx);\n this.#state = state;\n }\n\n lateralJoin = this._gate(\n { sql: { lateral: true } },\n 'lateralJoin',\n <Alias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: Alias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<AvailableScope, { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> }>\n > => {\n const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);\n const resultScope = mergeScopes(\n this.#state.scope as AvailableScope,\n lateralScope as { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> },\n );\n return this.#addLateralJoin('inner', resultScope, derivedSource);\n },\n ) as JoinedTables<QC, AvailableScope>['lateralJoin'];\n\n outerLateralJoin = this._gate(\n { sql: { lateral: true } },\n 'outerLateralJoin',\n <Alias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: Alias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<\n AvailableScope,\n NullableScope<{ topLevel: LateralRow; namespaces: Record<Alias, LateralRow> }>\n >\n > => {\n const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);\n const resultScope = mergeScopes(\n this.#state.scope as AvailableScope,\n nullableScope(\n lateralScope as { topLevel: LateralRow; namespaces: Record<Alias, LateralRow> },\n ),\n );\n return this.#addLateralJoin('left', resultScope, derivedSource);\n },\n ) as JoinedTables<QC, AvailableScope>['outerLateralJoin'];\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<EmptyRow, AvailableScope['topLevel'], Columns>>;\n select<Alias extends string, Field extends ScopeField>(\n alias: Alias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<EmptyRow, Field, Alias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n const { projections, newRowFields } = resolveSelectArgs(args, this.#state.scope, this.ctx);\n return new SelectQueryImpl<QC, AvailableScope>(\n cloneState(this.#state, {\n projections: [...this.#state.projections, ...projections],\n rowFields: { ...this.#state.rowFields, ...newRowFields },\n }),\n this.ctx,\n );\n }\n\n innerJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>> {\n const targetScope = mergeScopes(\n this.#state.scope as AvailableScope,\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n );\n return this.#addJoin(other, 'inner', targetScope, on);\n }\n\n outerLeftJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, NullableScope<Other[typeof JoinOuterScope]>>> {\n const targetScope = mergeScopes(\n this.#state.scope as AvailableScope,\n nullableScope(other.getJoinOuterScope() as Other[typeof JoinOuterScope]),\n );\n return this.#addJoin(other, 'left', targetScope, on);\n }\n\n outerRightJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<NullableScope<AvailableScope>, Other[typeof JoinOuterScope]>> {\n const targetScope = mergeScopes(\n nullableScope(this.#state.scope as AvailableScope),\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n );\n return this.#addJoin(other, 'right', targetScope, on);\n }\n\n outerFullJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<\n QC,\n MergeScopes<NullableScope<AvailableScope>, NullableScope<Other[typeof JoinOuterScope]>>\n > {\n const targetScope = mergeScopes(\n nullableScope(this.#state.scope as AvailableScope),\n nullableScope(other.getJoinOuterScope() as Other[typeof JoinOuterScope]),\n );\n return this.#addJoin(other, 'full', targetScope, on);\n }\n\n #addJoin<Other extends JoinSource<ScopeTable, string | never>, ResultScope extends Scope>(\n other: Other,\n joinType: 'inner' | 'left' | 'right' | 'full',\n resultScope: ResultScope,\n onExpr: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, ResultScope> {\n const fieldProxy = createFieldProxy(\n mergeScopes(\n this.#state.scope as AvailableScope,\n other.getJoinOuterScope() as Other[typeof JoinOuterScope],\n ),\n ) as FieldProxy<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>>;\n const fns = createFunctions<QC>(this.ctx.queryOperationTypes);\n const onResult = onExpr(fieldProxy, fns);\n const joinAst = new JoinAst(joinType, other.buildAst(), onResult.buildAst());\n\n return new JoinedTablesImpl(\n cloneState(this.#state, {\n joins: [...this.#state.joins, joinAst],\n scope: resultScope,\n }),\n this.ctx,\n );\n }\n\n #buildLateral(\n alias: string,\n builderFn: (\n lateral: LateralBuilder<QC, AvailableScope>,\n ) => Subquery<Record<string, ScopeField>>,\n ) {\n const lateralBuilder: LateralBuilder<QC, AvailableScope> = {\n from: (other) => {\n const otherScope = other.getJoinOuterScope();\n const parentMerged = mergeScopes(this.#state.scope, otherScope);\n return new SelectQueryImpl(\n emptyState(other.buildAst() as TableSource, parentMerged),\n this.ctx,\n ) as unknown as SelectQuery<QC, AvailableScope, EmptyRow>;\n },\n };\n\n const subquery = builderFn(lateralBuilder);\n const subqueryAst = subquery.buildAst();\n const derivedSource = DerivedTableSource.as(alias, subqueryAst);\n const subqueryRowFields: ScopeTable = subquery.getRowFields();\n const lateralScope: Scope = {\n topLevel: subqueryRowFields,\n namespaces: { [alias]: subqueryRowFields },\n };\n\n return { derivedSource, lateralScope };\n }\n\n #addLateralJoin<ResultScope extends Scope>(\n joinType: 'inner' | 'left',\n resultScope: ResultScope,\n derivedSource: DerivedTableSource,\n ): JoinedTables<QC, ResultScope> {\n const onExpr = AndExpr.of([]);\n const joinAst = new JoinAst(joinType, derivedSource, onExpr, true);\n\n return new JoinedTablesImpl(\n cloneState(this.#state, {\n joins: [...this.#state.joins, joinAst],\n scope: resultScope,\n }),\n this.ctx,\n );\n }\n}\n","import type { StorageTable } from '@prisma-next/sql-contract/types';\nimport {\n type AnyExpression as AstExpression,\n ColumnRef,\n DeleteAst,\n InsertAst,\n ParamRef,\n ProjectionItem,\n TableSource,\n UpdateAst,\n} from '@prisma-next/sql-relational-core/ast';\nimport type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';\nimport type { MutationDefaultsOp } from '@prisma-next/sql-relational-core/query-lane-context';\nimport type { ExpressionBuilder } from '../expression';\nimport type { ResolveRow } from '../resolve';\nimport type { QueryContext, Scope, ScopeField } from '../scope';\nimport type {\n DeleteQuery,\n InsertQuery,\n ReturningCapability,\n UpdateQuery,\n} from '../types/mutation-query';\nimport {\n BuilderBase,\n type BuilderContext,\n buildQueryPlan,\n combineWhereExprs,\n} from './builder-base';\nimport { createFieldProxy } from './field-proxy';\nimport { createFunctions } from './functions';\n\ntype WhereCallback = ExpressionBuilder<Scope, QueryContext>;\n\nfunction buildParamValues(\n values: Record<string, unknown>,\n table: StorageTable,\n tableName: string,\n op: MutationDefaultsOp,\n ctx: BuilderContext,\n): Record<string, ParamRef> {\n const params: Record<string, ParamRef> = {};\n for (const [col, value] of Object.entries(values)) {\n const column = table.columns[col];\n params[col] = ParamRef.of(\n value,\n column ? { codecId: column.codecId, refs: { table: tableName, column: col } } : undefined,\n );\n }\n for (const def of ctx.applyMutationDefaults({ op, table: tableName, values })) {\n const column = table.columns[def.column];\n params[def.column] = ParamRef.of(\n def.value,\n column\n ? { codecId: column.codecId, refs: { table: tableName, column: def.column } }\n : undefined,\n );\n }\n return params;\n}\n\nfunction buildReturningProjections(\n tableName: string,\n columns: string[],\n rowFields: Record<string, ScopeField>,\n): ProjectionItem[] {\n return columns.map((col) =>\n ProjectionItem.of(col, ColumnRef.of(tableName, col), rowFields[col]?.codecId),\n );\n}\n\nfunction evaluateWhere(\n whereCallback: WhereCallback,\n scope: Scope,\n queryOperationTypes: BuilderContext['queryOperationTypes'],\n): AstExpression {\n const fieldProxy = createFieldProxy(scope);\n const fns = createFunctions(queryOperationTypes);\n const result = whereCallback(fieldProxy, fns as never);\n return result.buildAst();\n}\n\nexport class InsertQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements InsertQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #scope: Scope;\n readonly #values: Record<string, unknown>;\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n table: StorageTable,\n scope: Scope,\n values: Record<string, unknown>,\n ctx: BuilderContext,\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = scope;\n this.#values = values;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n returning = this._gate<ReturningCapability, string[], InsertQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new InsertQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#values,\n this.ctx,\n columns,\n newRowFields,\n ) as unknown as InsertQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const paramValues = buildParamValues(\n this.#values,\n this.#table,\n this.#tableName,\n 'create',\n this.ctx,\n );\n\n let ast = InsertAst.into(TableSource.named(this.#tableName)).withValues(paramValues);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n\nexport class UpdateQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements UpdateQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #scope: Scope;\n readonly #setValues: Record<string, unknown>;\n readonly #whereCallbacks: readonly WhereCallback[];\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n table: StorageTable,\n scope: Scope,\n setValues: Record<string, unknown>,\n ctx: BuilderContext,\n whereCallbacks: readonly WhereCallback[] = [],\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = scope;\n this.#setValues = setValues;\n this.#whereCallbacks = whereCallbacks;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): UpdateQuery<QC, AvailableScope, RowType> {\n return new UpdateQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#setValues,\n this.ctx,\n [...this.#whereCallbacks, expr as unknown as WhereCallback],\n this.#returningColumns,\n this.#rowFields,\n );\n }\n\n returning = this._gate<ReturningCapability, string[], UpdateQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new UpdateQueryImpl(\n this.#tableName,\n this.#table,\n this.#scope,\n this.#setValues,\n this.ctx,\n this.#whereCallbacks,\n columns,\n newRowFields,\n ) as unknown as UpdateQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const setParams = buildParamValues(\n this.#setValues,\n this.#table,\n this.#tableName,\n 'update',\n this.ctx,\n );\n\n const whereExpr = combineWhereExprs(\n this.#whereCallbacks.map((cb) =>\n evaluateWhere(cb, this.#scope, this.ctx.queryOperationTypes),\n ),\n );\n\n let ast = UpdateAst.table(TableSource.named(this.#tableName))\n .withSet(setParams)\n .withWhere(whereExpr);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n\nexport class DeleteQueryImpl<\n QC extends QueryContext = QueryContext,\n AvailableScope extends Scope = Scope,\n RowType extends Record<string, ScopeField> = Record<string, ScopeField>,\n >\n extends BuilderBase<QC['capabilities']>\n implements DeleteQuery<QC, AvailableScope, RowType>\n{\n readonly #tableName: string;\n readonly #scope: Scope;\n readonly #whereCallbacks: readonly WhereCallback[];\n readonly #returningColumns: string[];\n readonly #rowFields: Record<string, ScopeField>;\n\n constructor(\n tableName: string,\n scope: Scope,\n ctx: BuilderContext,\n whereCallbacks: readonly WhereCallback[] = [],\n returningColumns: string[] = [],\n rowFields: Record<string, ScopeField> = {},\n ) {\n super(ctx);\n this.#tableName = tableName;\n this.#scope = scope;\n this.#whereCallbacks = whereCallbacks;\n this.#returningColumns = returningColumns;\n this.#rowFields = rowFields;\n }\n\n where(expr: ExpressionBuilder<AvailableScope, QC>): DeleteQuery<QC, AvailableScope, RowType> {\n return new DeleteQueryImpl(\n this.#tableName,\n this.#scope,\n this.ctx,\n [...this.#whereCallbacks, expr as unknown as WhereCallback],\n this.#returningColumns,\n this.#rowFields,\n );\n }\n\n returning = this._gate<ReturningCapability, string[], DeleteQuery<QC, AvailableScope, never>>(\n { sql: { returning: true } },\n 'returning',\n (...columns: string[]) => {\n const newRowFields: Record<string, ScopeField> = {};\n for (const col of columns) {\n const field = this.#scope.topLevel[col];\n if (!field) throw new Error(`Column \"${col}\" not found in scope`);\n newRowFields[col] = field;\n }\n return new DeleteQueryImpl(\n this.#tableName,\n this.#scope,\n this.ctx,\n this.#whereCallbacks,\n columns,\n newRowFields,\n ) as unknown as DeleteQuery<QC, AvailableScope, never>;\n },\n );\n\n build(): SqlQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>> {\n const whereExpr = combineWhereExprs(\n this.#whereCallbacks.map((cb) =>\n evaluateWhere(cb, this.#scope, this.ctx.queryOperationTypes),\n ),\n );\n\n let ast = DeleteAst.from(TableSource.named(this.#tableName)).withWhere(whereExpr);\n\n if (this.#returningColumns.length > 0) {\n ast = ast.withReturning(\n buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields),\n );\n }\n\n return buildQueryPlan<ResolveRow<RowType, QC['codecTypes'], QC['resolvedColumnOutputTypes']>>(\n ast,\n this.ctx,\n );\n }\n}\n","import type { StorageTable } from '@prisma-next/sql-contract/types';\nimport { type AnyFromSource, TableSource } from '@prisma-next/sql-relational-core/ast';\nimport type {\n AggregateFunctions,\n Expression,\n ExpressionBuilder,\n ExtractScopeFields,\n FieldProxy,\n WithField,\n WithFields,\n} from '../expression';\nimport type {\n EmptyRow,\n Expand,\n JoinOuterScope,\n JoinSource,\n MergeScopes,\n NullableScope,\n QueryContext,\n RebindScope,\n Scope,\n ScopeField,\n ScopeTable,\n StorageTableToScopeTable,\n Subquery,\n} from '../scope';\nimport type { TableProxyContract } from '../types/db';\nimport type { JoinedTables } from '../types/joined-tables';\nimport type { DeleteQuery, InsertQuery, UpdateQuery } from '../types/mutation-query';\nimport type { SelectQuery } from '../types/select-query';\nimport type { LateralBuilder } from '../types/shared';\nimport type { TableProxy } from '../types/table-proxy';\nimport { BuilderBase, type BuilderContext, emptyState, tableToScope } from './builder-base';\nimport { JoinedTablesImpl } from './joined-tables-impl';\nimport { DeleteQueryImpl, InsertQueryImpl, UpdateQueryImpl } from './mutation-impl';\nimport { SelectQueryImpl } from './query-impl';\n\nexport class TableProxyImpl<\n C extends TableProxyContract,\n Name extends string & keyof C['storage']['tables'],\n Alias extends string,\n AvailableScope extends Scope,\n QC extends QueryContext,\n >\n extends BuilderBase<C['capabilities']>\n implements TableProxy<C, Name, Alias, AvailableScope, QC>\n{\n declare readonly [JoinOuterScope]: JoinSource<\n StorageTableToScopeTable<C['storage']['tables'][Name]>,\n Alias\n >[typeof JoinOuterScope];\n\n readonly #tableName: string;\n readonly #table: StorageTable;\n readonly #fromSource: TableSource;\n readonly #scope: Scope;\n\n constructor(tableName: string, table: StorageTable, alias: string, ctx: BuilderContext) {\n super(ctx);\n this.#tableName = tableName;\n this.#table = table;\n this.#scope = tableToScope(alias, table);\n this.#fromSource = TableSource.named(tableName, alias !== tableName ? alias : undefined);\n }\n\n lateralJoin = this._gate(\n { sql: { lateral: true } },\n 'lateralJoin',\n <LAlias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: LAlias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<AvailableScope, { topLevel: LateralRow; namespaces: Record<LAlias, LateralRow> }>\n > => {\n return this.#toJoined().lateralJoin(alias, builder);\n },\n ) as TableProxy<C, Name, Alias, AvailableScope, QC>['lateralJoin'];\n\n outerLateralJoin = this._gate(\n { sql: { lateral: true } },\n 'outerLateralJoin',\n <LAlias extends string, LateralRow extends Record<string, ScopeField>>(\n alias: LAlias,\n builder: (lateral: LateralBuilder<QC, AvailableScope>) => Subquery<LateralRow>,\n ): JoinedTables<\n QC,\n MergeScopes<\n AvailableScope,\n NullableScope<{ topLevel: LateralRow; namespaces: Record<LAlias, LateralRow> }>\n >\n > => {\n return this.#toJoined().outerLateralJoin(alias, builder);\n },\n ) as TableProxy<C, Name, Alias, AvailableScope, QC>['outerLateralJoin'];\n\n getJoinOuterScope(): Scope {\n return this.#scope;\n }\n\n buildAst(): AnyFromSource {\n return this.#fromSource;\n }\n\n as<NewAlias extends string>(\n newAlias: NewAlias,\n ): TableProxy<C, Name, NewAlias, RebindScope<AvailableScope, Alias, NewAlias>> {\n return new TableProxyImpl(this.#tableName, this.#table, newAlias, this.ctx);\n }\n\n select<Columns extends (keyof AvailableScope['topLevel'] & string)[]>(\n ...columns: Columns\n ): SelectQuery<QC, AvailableScope, WithFields<EmptyRow, AvailableScope['topLevel'], Columns>>;\n select<LAlias extends string, Field extends ScopeField>(\n alias: LAlias,\n expr: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Expression<Field>,\n ): SelectQuery<QC, AvailableScope, WithField<EmptyRow, Field, LAlias>>;\n select<Result extends Record<string, Expression<ScopeField>>>(\n callback: (fields: FieldProxy<AvailableScope>, fns: AggregateFunctions<QC>) => Result,\n ): SelectQuery<QC, AvailableScope, Expand<ExtractScopeFields<Result>>>;\n select(...args: unknown[]): unknown {\n return new SelectQueryImpl(emptyState(this.#fromSource, this.#scope), this.ctx).select(\n ...(args as string[]),\n );\n }\n\n innerJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>> {\n return this.#toJoined().innerJoin(other, on);\n }\n\n outerLeftJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<AvailableScope, NullableScope<Other[typeof JoinOuterScope]>>> {\n return this.#toJoined().outerLeftJoin(other, on);\n }\n\n outerRightJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<QC, MergeScopes<NullableScope<AvailableScope>, Other[typeof JoinOuterScope]>> {\n return this.#toJoined().outerRightJoin(other, on);\n }\n\n outerFullJoin<Other extends JoinSource<ScopeTable, string | never>>(\n other: Other,\n on: ExpressionBuilder<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>, QC>,\n ): JoinedTables<\n QC,\n MergeScopes<NullableScope<AvailableScope>, NullableScope<Other[typeof JoinOuterScope]>>\n > {\n return this.#toJoined().outerFullJoin(other, on);\n }\n\n insert(values: Record<string, unknown>): InsertQuery<QC, AvailableScope, EmptyRow> {\n return new InsertQueryImpl(this.#tableName, this.#table, this.#scope, values, this.ctx);\n }\n\n update(set: Record<string, unknown>): UpdateQuery<QC, AvailableScope, EmptyRow> {\n return new UpdateQueryImpl(this.#tableName, this.#table, this.#scope, set, this.ctx);\n }\n\n delete(): DeleteQuery<QC, AvailableScope, EmptyRow> {\n return new DeleteQueryImpl(this.#tableName, this.#scope, this.ctx);\n }\n\n #toJoined(): JoinedTables<QC, AvailableScope> {\n return new JoinedTablesImpl(emptyState(this.#fromSource, this.#scope), this.ctx);\n }\n}\n","import type { Contract } from '@prisma-next/contract/types';\nimport type { SqlStorage } from '@prisma-next/sql-contract/types';\nimport type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';\nimport type { Db } from '../types/db';\nimport type { BuilderContext } from './builder-base';\nimport { TableProxyImpl } from './table-proxy-impl';\n\nexport interface SqlOptions<C extends Contract<SqlStorage>> {\n readonly context: ExecutionContext<C>;\n}\n\nexport function sql<C extends Contract<SqlStorage>>(options: SqlOptions<C>): Db<C> {\n const { context } = options;\n const ctx: BuilderContext = {\n capabilities: context.contract.capabilities,\n queryOperationTypes: context.queryOperations.entries(),\n target: context.contract.target ?? 'unknown',\n storageHash: context.contract.storage.storageHash ?? 'unknown',\n applyMutationDefaults: (options) => context.applyMutationDefaults(options),\n };\n\n return new Proxy({} as Db<C>, {\n get(_target, prop: string) {\n const tables = context.contract.storage.tables;\n const table = Object.hasOwn(tables, prop) ? tables[prop] : undefined;\n if (table) {\n return new TableProxyImpl(prop, table, prop, ctx);\n }\n return undefined;\n },\n });\n}\n"],"mappings":";;;;;;;;;AASA,IAAa,iBAAb,MAAwF;CACtF,AAAiB;CACjB,AAAS;CACT,AAAS;CAET,YACE,KACA,YACA,MACA;AACA,OAAK,MAAM;AACX,OAAK,aAAa;AAClB,OAAK,OAAO;;CAGd,WAA0B;AACxB,SAAO,KAAK;;;;;;;;;;AChBhB,SAASA,yBAAuB,OAAc,WAAuC;CACnF,IAAIC;AACJ,MAAK,MAAM,CAAC,WAAW,WAAW,OAAO,QAAQ,MAAM,WAAW,CAChE,KAAI,OAAO,OAAO,QAAQ,UAAU,EAAE;AACpC,MAAI,UAAU,OAAW,QAAO;AAChC,UAAQ;;AAGZ,QAAO;;AAGT,SAAgB,iBAAkC,OAAyB;AACzE,QAAO,IAAI,MAAM,EAAE,EAAmB,EACpC,IAAI,SAAS,MAAc;AACzB,MAAI,OAAO,OAAO,MAAM,UAAU,KAAK,EAAE;GACvC,MAAM,WAAW,MAAM,SAAS;AAChC,OAAI,UAAU;IACZ,MAAM,YAAYD,yBAAuB,OAAO,KAAK;IACrD,MAAM,OAAO,YAAY;KAAE,OAAO;KAAW,QAAQ;KAAM,GAAG;AAC9D,WAAO,IAAI,eAAe,cAAc,GAAG,KAAK,EAAE,UAAU,KAAK;;;AAIrE,MAAI,OAAO,OAAO,MAAM,YAAY,KAAK,EAAE;GACzC,MAAM,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AO,IAAI,gBAAgB,MAAKH,WAAY,MAAKG,OAAQ,KAAK,IAAI;;CAGpE,YAA8C;AAC5C,SAAO,IAAI,iBAAiB,WAAW,MAAKD,YAAa,MAAKC,MAAO,EAAE,KAAK,IAAI;;;;;;AC/JpF,SAAgB,IAAoC,SAA+B;CACjF,MAAM,EAAE,YAAY;CACpB,MAAME,MAAsB;EAC1B,cAAc,QAAQ,SAAS;EAC/B,qBAAqB,QAAQ,gBAAgB,SAAS;EACtD,QAAQ,QAAQ,SAAS,UAAU;EACnC,aAAa,QAAQ,SAAS,QAAQ,eAAe;EACrD,wBAAwB,cAAY,QAAQ,sBAAsBC,UAAQ;EAC3E;AAED,QAAO,IAAI,MAAM,EAAE,EAAW,EAC5B,IAAI,SAAS,MAAc;EACzB,MAAM,SAAS,QAAQ,SAAS,QAAQ;EACxC,MAAM,QAAQ,OAAO,OAAO,QAAQ,KAAK,GAAG,OAAO,QAAQ;AAC3D,MAAI,MACF,QAAO,IAAI,eAAe,MAAM,OAAO,MAAM,IAAI;IAItD,CAAC"}
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-builder",
3
- "version": "0.5.0-dev.60",
3
+ "version": "0.5.0-dev.61",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "SQL builder lane for Prisma Next",
8
8
  "dependencies": {
9
- "@prisma-next/framework-components": "0.5.0-dev.60",
10
- "@prisma-next/sql-relational-core": "0.5.0-dev.60",
11
- "@prisma-next/sql-operations": "0.5.0-dev.60"
9
+ "@prisma-next/sql-operations": "0.5.0-dev.61",
10
+ "@prisma-next/framework-components": "0.5.0-dev.61",
11
+ "@prisma-next/sql-relational-core": "0.5.0-dev.61"
12
12
  },
13
13
  "devDependencies": {
14
14
  "tsdown": "0.18.4",
15
15
  "typescript": "5.9.3",
16
16
  "vitest": "4.0.17",
17
- "@prisma-next/adapter-postgres": "0.5.0-dev.60",
18
- "@prisma-next/contract": "0.5.0-dev.60",
19
- "@prisma-next/extension-pgvector": "0.5.0-dev.60",
20
- "@prisma-next/sql-contract": "0.5.0-dev.60",
21
- "@prisma-next/sql-contract-ts": "0.5.0-dev.60",
22
- "@prisma-next/ids": "0.5.0-dev.60",
23
- "@prisma-next/target-postgres": "0.5.0-dev.60",
17
+ "@prisma-next/contract": "0.5.0-dev.61",
18
+ "@prisma-next/extension-pgvector": "0.5.0-dev.61",
19
+ "@prisma-next/ids": "0.5.0-dev.61",
20
+ "@prisma-next/adapter-postgres": "0.5.0-dev.61",
21
+ "@prisma-next/sql-contract": "0.5.0-dev.61",
22
+ "@prisma-next/sql-contract-ts": "0.5.0-dev.61",
24
23
  "@prisma-next/test-utils": "0.0.1",
25
24
  "@prisma-next/tsconfig": "0.0.0",
26
- "@prisma-next/utils": "0.5.0-dev.60",
25
+ "@prisma-next/target-postgres": "0.5.0-dev.61",
26
+ "@prisma-next/utils": "0.5.0-dev.61",
27
27
  "@prisma-next/tsdown": "0.0.0"
28
28
  },
29
29
  "files": [
@@ -110,6 +110,20 @@ export function combineWhereExprs(exprs: readonly AstExpression[]): AstExpressio
110
110
  return AndExpr.of(exprs);
111
111
  }
112
112
 
113
+ /**
114
+ * Same uniqueness rule as the field-proxy's `findUniqueNamespaceFor`: when exactly one namespace owns a top-level field, the binding is unambiguous. Used by `select('col', ...)` to attach `refs` metadata to the resulting `ProjectionItem` while keeping the AST as `IdentifierRef` (so SQL renders unchanged).
115
+ */
116
+ function findUniqueNamespaceFor(scope: Scope, fieldName: string): string | undefined {
117
+ let found: string | undefined;
118
+ for (const [namespace, fields] of Object.entries(scope.namespaces)) {
119
+ if (Object.hasOwn(fields, fieldName)) {
120
+ if (found !== undefined) return undefined;
121
+ found = namespace;
122
+ }
123
+ }
124
+ return found;
125
+ }
126
+
113
127
  export function buildSelectAst(state: BuilderState): SelectAst {
114
128
  const where = combineWhereExprs(state.where);
115
129
  return new SelectAst({
@@ -225,7 +239,9 @@ export function resolveSelectArgs(
225
239
  for (const colName of args as string[]) {
226
240
  const field = scope.topLevel[colName];
227
241
  if (!field) throw new Error(`Column "${colName}" not found in scope`);
228
- projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId));
242
+ const namespace = findUniqueNamespaceFor(scope, colName);
243
+ const refs = namespace ? { table: namespace, column: colName } : undefined;
244
+ projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codecId, refs));
229
245
  newRowFields[colName] = field;
230
246
  }
231
247
  return { projections, newRowFields };
@@ -3,17 +3,23 @@ import type { Expression } from '@prisma-next/sql-relational-core/expression';
3
3
  import type { ScopeField } from '../scope';
4
4
 
5
5
  /**
6
- * Runtime wrapper around a relational-core AST expression node.
7
- * Carries ScopeField metadata (codecId, nullable) so aggregate-like
8
- * combinators can propagate the input codec onto their result.
6
+ * Runtime wrapper around a relational-core AST expression node. Carries ScopeField metadata (codecId, nullable) so aggregate-like combinators can propagate the input codec onto their result.
7
+ *
8
+ * `refs` records the column-bound binding (`{ table, column }`) when known — the field-proxy populates it for both the namespaced form (`f.user.email` → `ColumnRef`) and the top-level shortcut (`f.email` → `IdentifierRef` + refs metadata). Encode-side dispatch and the `validateParamRefRefs` pass read it via `refsOf(expression)`.
9
9
  */
10
10
  export class ExpressionImpl<T extends ScopeField = ScopeField> implements Expression<T> {
11
11
  private readonly ast: AstExpression;
12
12
  readonly returnType: T;
13
+ readonly refs: { readonly table: string; readonly column: string } | undefined;
13
14
 
14
- constructor(ast: AstExpression, returnType: T) {
15
+ constructor(
16
+ ast: AstExpression,
17
+ returnType: T,
18
+ refs?: { readonly table: string; readonly column: string },
19
+ ) {
15
20
  this.ast = ast;
16
21
  this.returnType = returnType;
22
+ this.refs = refs;
17
23
  }
18
24
 
19
25
  buildAst(): AstExpression {
@@ -3,12 +3,31 @@ import type { FieldProxy } from '../expression';
3
3
  import type { Scope, ScopeTable } from '../scope';
4
4
  import { ExpressionImpl } from './expression-impl';
5
5
 
6
+ /**
7
+ * For a top-level field name, find the namespace (table alias) that contributed it. When exactly one namespace owns the field, the top-level binding is unambiguously column-bound and we record that `(table, column)` pair on the `ExpressionImpl` so encode-side dispatch (`forColumn`) and the `validateParamRefRefs` pass can find it. The AST stays as `IdentifierRef` to preserve SQL rendering — adapters render top-level
8
+ * identifiers without an explicit table qualifier — so this change is metadata-only and produces no SQL drift.
9
+ */
10
+ function findUniqueNamespaceFor(scope: Scope, fieldName: string): string | undefined {
11
+ let found: string | undefined;
12
+ for (const [namespace, fields] of Object.entries(scope.namespaces)) {
13
+ if (Object.hasOwn(fields, fieldName)) {
14
+ if (found !== undefined) return undefined;
15
+ found = namespace;
16
+ }
17
+ }
18
+ return found;
19
+ }
20
+
6
21
  export function createFieldProxy<S extends Scope>(scope: S): FieldProxy<S> {
7
22
  return new Proxy({} as FieldProxy<S>, {
8
23
  get(_target, prop: string) {
9
24
  if (Object.hasOwn(scope.topLevel, prop)) {
10
25
  const topField = scope.topLevel[prop];
11
- if (topField) return new ExpressionImpl(IdentifierRef.of(prop), topField);
26
+ if (topField) {
27
+ const namespace = findUniqueNamespaceFor(scope, prop);
28
+ const refs = namespace ? { table: namespace, column: prop } : undefined;
29
+ return new ExpressionImpl(IdentifierRef.of(prop), topField, refs);
30
+ }
12
31
  }
13
32
 
14
33
  if (Object.hasOwn(scope.namespaces, prop)) {
@@ -12,7 +12,7 @@ import {
12
12
  OrExpr,
13
13
  SubqueryExpr,
14
14
  } from '@prisma-next/sql-relational-core/ast';
15
- import { toExpr } from '@prisma-next/sql-relational-core/expression';
15
+ import { refsOf, toExpr } from '@prisma-next/sql-relational-core/expression';
16
16
  import type {
17
17
  AggregateFunctions,
18
18
  AggregateOnlyFunctions,
@@ -26,8 +26,7 @@ import type { QueryContext, ScopeField, Subquery } from '../scope';
26
26
  import { ExpressionImpl } from './expression-impl';
27
27
 
28
28
  type CodecTypes = Record<string, { readonly input: unknown }>;
29
- // Runtime-level ExprOrVal — accepts any codec, any nullability. Concrete codec
30
- // typing lives on the public BuiltinFunctions surface in `../expression`.
29
+ // Runtime-level ExprOrVal — accepts any codec, any nullability. Concrete codec typing lives on the public BuiltinFunctions surface in `../expression`.
31
30
  type ExprOrVal<CodecId extends string = string, N extends boolean = boolean> = CodecExpression<
32
31
  CodecId,
33
32
  N,
@@ -39,16 +38,44 @@ const BOOL_FIELD: BooleanCodecType = { codecId: 'pg/bool@1', nullable: false };
39
38
  const resolve = toExpr;
40
39
 
41
40
  /**
42
- * Resolves an Expression via `buildAst()`, or wraps a raw value as a
43
- * `LiteralExpr` — an SQL literal inlined into the query text, not a bound
44
- * parameter.
41
+ * Resolve a binary-comparison operand into an AST expression, threading the column-bound side's `codecId` + `refs` to the raw-value side.
45
42
  *
46
- * Used for `and` / `or` operands. The usual operand is an `Expression<bool>`
47
- * (e.g. the result of `fns.eq`), which this function passes through by calling
48
- * `buildAst()`. The only time the raw-value branch fires is when the caller
49
- * writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals
50
- * lets the SQL planner statically simplify `TRUE AND x` to `x`, which it
51
- * cannot do for an opaque `ParamRef`.
43
+ * For `fns.eq(f.email, 'alice@example.com')`, `f.email` is the column-bound expression carrying a `ColumnRef` AST and a `returnType.codecId` (`pg/varchar@1`); the raw string operand has no codec context. By deriving the codec context from the column-bound side and forwarding it via `toExpr(value, codecId, refs)`, the resulting `ParamRef` carries the column refs that encode-side `forColumn` dispatch needs (and that the
44
+ * validator pass requires for parameterized codec ids like `pg/varchar@1` with a length parameter).
45
+ */
46
+ function resolveOperand(
47
+ operand: ExprOrVal,
48
+ otherCodecId?: string,
49
+ otherRefs?: { table: string; column: string },
50
+ ): AstExpression {
51
+ if (isExpressionLike(operand)) return operand.buildAst();
52
+ return toExpr(operand, otherCodecId, otherRefs);
53
+ }
54
+
55
+ function isExpressionLike(
56
+ value: unknown,
57
+ ): value is { buildAst: () => AstExpression; returnType?: { codecId: string } } {
58
+ return (
59
+ typeof value === 'object' &&
60
+ value !== null &&
61
+ 'buildAst' in value &&
62
+ typeof (value as { buildAst: unknown }).buildAst === 'function'
63
+ );
64
+ }
65
+
66
+ function operandCodecId(operand: ExprOrVal): string | undefined {
67
+ if (!isExpressionLike(operand)) return undefined;
68
+ return (operand as { returnType?: { codecId: string } }).returnType?.codecId;
69
+ }
70
+
71
+ function operandRefs(operand: ExprOrVal): { table: string; column: string } | undefined {
72
+ return refsOf(operand);
73
+ }
74
+
75
+ /**
76
+ * Resolves an Expression via `buildAst()`, or wraps a raw value as a `LiteralExpr` — an SQL literal inlined into the query text, not a bound parameter.
77
+ *
78
+ * Used for `and` / `or` operands. The usual operand is an `Expression<bool>` (e.g. the result of `fns.eq`), which this function passes through by calling `buildAst()`. The only time the raw-value branch fires is when the caller writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals lets the SQL planner statically simplify `TRUE AND x` to `x`, which it cannot do for an opaque `ParamRef`.
52
79
  */
53
80
  function toLiteralExpr(value: unknown): AstExpression {
54
81
  if (
@@ -66,20 +93,34 @@ function boolExpr(astNode: AstExpression): ExpressionImpl<BooleanCodecType> {
66
93
  return new ExpressionImpl(astNode, BOOL_FIELD);
67
94
  }
68
95
 
96
+ function binaryWithSharedCodec(
97
+ a: ExprOrVal,
98
+ b: ExprOrVal,
99
+ build: (left: AstExpression, right: AstExpression) => AstExpression,
100
+ ): AstExpression {
101
+ const aCodecId = operandCodecId(a);
102
+ const bCodecId = operandCodecId(b);
103
+ const aRefs = operandRefs(a);
104
+ const bRefs = operandRefs(b);
105
+ const left = resolveOperand(a, bCodecId, bRefs);
106
+ const right = resolveOperand(b, aCodecId, aRefs);
107
+ return build(left, right);
108
+ }
109
+
69
110
  function eq(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {
70
111
  if (b === null) return boolExpr(NullCheckExpr.isNull(resolve(a)));
71
112
  if (a === null) return boolExpr(NullCheckExpr.isNull(resolve(b)));
72
- return boolExpr(new BinaryExpr('eq', resolve(a), resolve(b)));
113
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr('eq', l, r)));
73
114
  }
74
115
 
75
116
  function ne(a: ExprOrVal, b: ExprOrVal): ExpressionImpl<BooleanCodecType> {
76
117
  if (b === null) return boolExpr(NullCheckExpr.isNotNull(resolve(a)));
77
118
  if (a === null) return boolExpr(NullCheckExpr.isNotNull(resolve(b)));
78
- return boolExpr(new BinaryExpr('neq', resolve(a), resolve(b)));
119
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr('neq', l, r)));
79
120
  }
80
121
 
81
122
  function comparison(a: ExprOrVal, b: ExprOrVal, op: BinaryOp): ExpressionImpl<BooleanCodecType> {
82
- return boolExpr(new BinaryExpr(op, resolve(a), resolve(b)));
123
+ return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr(op, l, r)));
83
124
  }
84
125
 
85
126
  function inOrNotIn(
@@ -88,10 +129,12 @@ function inOrNotIn(
88
129
  op: 'in' | 'notIn',
89
130
  ): ExpressionImpl<BooleanCodecType> {
90
131
  const left = expr.buildAst();
132
+ const leftCodecId = expr.returnType.codecId;
133
+ const leftRefs = refsOf(expr);
91
134
  const binaryFn = op === 'in' ? BinaryExpr.in : BinaryExpr.notIn;
92
135
 
93
136
  if (Array.isArray(valuesOrSubquery)) {
94
- const refs = valuesOrSubquery.map((v) => resolve(v));
137
+ const refs = valuesOrSubquery.map((v) => resolveOperand(v, leftCodecId, leftRefs));
95
138
  return boolExpr(binaryFn(left, ListExpression.of(refs)));
96
139
  }
97
140
  return boolExpr(binaryFn(left, SubqueryExpr.of(valuesOrSubquery.buildAst())));
@@ -41,11 +41,19 @@ function buildParamValues(
41
41
  const params: Record<string, ParamRef> = {};
42
42
  for (const [col, value] of Object.entries(values)) {
43
43
  const column = table.columns[col];
44
- params[col] = ParamRef.of(value, column ? { codecId: column.codecId } : undefined);
44
+ params[col] = ParamRef.of(
45
+ value,
46
+ column ? { codecId: column.codecId, refs: { table: tableName, column: col } } : undefined,
47
+ );
45
48
  }
46
49
  for (const def of ctx.applyMutationDefaults({ op, table: tableName, values })) {
47
50
  const column = table.columns[def.column];
48
- params[def.column] = ParamRef.of(def.value, column ? { codecId: column.codecId } : undefined);
51
+ params[def.column] = ParamRef.of(
52
+ def.value,
53
+ column
54
+ ? { codecId: column.codecId, refs: { table: tableName, column: def.column } }
55
+ : undefined,
56
+ );
49
57
  }
50
58
  return params;
51
59
  }