@prisma-next/sql-relational-core 0.3.0-dev.3 → 0.3.0-dev.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +1 -1
  2. package/dist/ast/adapter-types.d.ts +28 -0
  3. package/dist/ast/adapter-types.d.ts.map +1 -0
  4. package/dist/{query-lane-context-BhOMmb_K.d.ts → ast/codec-types.d.ts} +14 -31
  5. package/dist/ast/codec-types.d.ts.map +1 -0
  6. package/dist/ast/common.d.ts +7 -0
  7. package/dist/ast/common.d.ts.map +1 -0
  8. package/dist/ast/delete.d.ts +8 -0
  9. package/dist/ast/delete.d.ts.map +1 -0
  10. package/dist/ast/driver-types.d.ts +20 -0
  11. package/dist/ast/driver-types.d.ts.map +1 -0
  12. package/dist/ast/insert.d.ts +8 -0
  13. package/dist/ast/insert.d.ts.map +1 -0
  14. package/dist/ast/join.d.ts +6 -0
  15. package/dist/ast/join.d.ts.map +1 -0
  16. package/dist/ast/order.d.ts +6 -0
  17. package/dist/ast/order.d.ts.map +1 -0
  18. package/dist/ast/predicate.d.ts +4 -0
  19. package/dist/ast/predicate.d.ts.map +1 -0
  20. package/dist/ast/select.d.ts +18 -0
  21. package/dist/ast/select.d.ts.map +1 -0
  22. package/dist/ast/types.d.ts +130 -0
  23. package/dist/ast/types.d.ts.map +1 -0
  24. package/dist/ast/update.d.ts +9 -0
  25. package/dist/ast/update.d.ts.map +1 -0
  26. package/dist/ast/util.d.ts +2 -0
  27. package/dist/ast/util.d.ts.map +1 -0
  28. package/dist/{chunk-MM74SVJ4.js → chunk-2BWK6XEY.js} +1 -1
  29. package/dist/chunk-2BWK6XEY.js.map +1 -0
  30. package/dist/{chunk-5N34PNVZ.js → chunk-2MAKNVCP.js} +25 -1
  31. package/dist/chunk-2MAKNVCP.js.map +1 -0
  32. package/dist/{chunk-M23L3JHG.js → chunk-3F4RFQIB.js} +45 -35
  33. package/dist/chunk-3F4RFQIB.js.map +1 -0
  34. package/dist/{chunk-WZBPVEZI.js → chunk-HV334QHG.js} +2 -2
  35. package/dist/chunk-HV334QHG.js.map +1 -0
  36. package/dist/{chunk-CBTYMOX2.js → chunk-YXD25S5I.js} +31 -12
  37. package/dist/chunk-YXD25S5I.js.map +1 -0
  38. package/dist/errors.d.ts +2 -0
  39. package/dist/errors.d.ts.map +1 -0
  40. package/dist/exports/ast.d.ts +14 -119
  41. package/dist/exports/ast.d.ts.map +1 -0
  42. package/dist/exports/ast.js +2 -2
  43. package/dist/exports/errors.d.ts +2 -1
  44. package/dist/exports/errors.d.ts.map +1 -0
  45. package/dist/exports/guards.d.ts +2 -63
  46. package/dist/exports/guards.d.ts.map +1 -0
  47. package/dist/exports/guards.js +13 -3
  48. package/dist/exports/operations-registry.d.ts +2 -13
  49. package/dist/exports/operations-registry.d.ts.map +1 -0
  50. package/dist/exports/operations-registry.js +2 -2
  51. package/dist/exports/param.d.ts +3 -14
  52. package/dist/exports/param.d.ts.map +1 -0
  53. package/dist/exports/plan.d.ts +2 -5
  54. package/dist/exports/plan.d.ts.map +1 -0
  55. package/dist/exports/query-lane-context.d.ts +2 -4
  56. package/dist/exports/query-lane-context.d.ts.map +1 -0
  57. package/dist/exports/schema.d.ts +3 -69
  58. package/dist/exports/schema.d.ts.map +1 -0
  59. package/dist/exports/schema.js +3 -4
  60. package/dist/exports/types.d.ts +2 -335
  61. package/dist/exports/types.d.ts.map +1 -0
  62. package/dist/exports/types.js +1 -1
  63. package/dist/index.d.ts +9 -13
  64. package/dist/index.d.ts.map +1 -0
  65. package/dist/index.js +5 -5
  66. package/dist/operations-registry.d.ts +5 -0
  67. package/dist/operations-registry.d.ts.map +1 -0
  68. package/dist/param.d.ts +4 -0
  69. package/dist/param.d.ts.map +1 -0
  70. package/dist/plan.d.ts +23 -0
  71. package/dist/plan.d.ts.map +1 -0
  72. package/dist/query-lane-context.d.ts +27 -0
  73. package/dist/query-lane-context.d.ts.map +1 -0
  74. package/dist/schema.d.ts +83 -0
  75. package/dist/schema.d.ts.map +1 -0
  76. package/dist/types.d.ts +371 -0
  77. package/dist/types.d.ts.map +1 -0
  78. package/dist/utils/guards.d.ts +82 -0
  79. package/dist/utils/guards.d.ts.map +1 -0
  80. package/package.json +19 -19
  81. package/src/ast/adapter-types.ts +36 -0
  82. package/src/ast/codec-types.ts +376 -0
  83. package/src/ast/common.ts +36 -0
  84. package/src/ast/delete.ts +17 -0
  85. package/src/ast/driver-types.ts +25 -0
  86. package/src/ast/insert.ts +17 -0
  87. package/src/ast/join.ts +54 -0
  88. package/src/ast/order.ts +11 -0
  89. package/src/ast/predicate.ts +22 -0
  90. package/src/ast/select.ts +39 -0
  91. package/src/ast/types.ts +147 -0
  92. package/src/ast/update.ts +19 -0
  93. package/src/ast/util.ts +9 -0
  94. package/src/errors.ts +1 -0
  95. package/src/exports/ast.ts +13 -0
  96. package/src/exports/errors.ts +1 -0
  97. package/src/exports/guards.ts +15 -0
  98. package/src/exports/operations-registry.ts +1 -0
  99. package/src/exports/param.ts +2 -0
  100. package/src/exports/plan.ts +1 -0
  101. package/src/exports/query-lane-context.ts +1 -0
  102. package/src/exports/schema.ts +6 -0
  103. package/src/exports/types.ts +1 -0
  104. package/src/index.ts +8 -0
  105. package/src/operations-registry.ts +276 -0
  106. package/src/param.ts +15 -0
  107. package/src/plan.ts +39 -0
  108. package/src/query-lane-context.ts +30 -0
  109. package/src/schema.ts +406 -0
  110. package/src/types.ts +598 -0
  111. package/src/utils/guards.ts +193 -0
  112. package/dist/chunk-5N34PNVZ.js.map +0 -1
  113. package/dist/chunk-CBTYMOX2.js.map +0 -1
  114. package/dist/chunk-M23L3JHG.js.map +0 -1
  115. package/dist/chunk-MM74SVJ4.js.map +0 -1
  116. package/dist/chunk-WZBPVEZI.js.map +0 -1
  117. package/dist/plan-D0OG5qzy.d.ts +0 -142
package/src/types.ts ADDED
@@ -0,0 +1,598 @@
1
+ import type {
2
+ ResultType as CoreResultType,
3
+ ExecutionPlan,
4
+ PlanRefs,
5
+ } from '@prisma-next/contract/types';
6
+ import type { ArgSpec, ReturnSpec } from '@prisma-next/operations';
7
+ import type { SqlContract, SqlStorage, StorageColumn } from '@prisma-next/sql-contract/types';
8
+ import type { SqlLoweringSpec } from '@prisma-next/sql-operations';
9
+ import type {
10
+ BinaryOp,
11
+ ColumnRef,
12
+ Direction,
13
+ Expression,
14
+ ExpressionSource,
15
+ OperationExpr,
16
+ ParamRef,
17
+ QueryAst,
18
+ } from './ast/types';
19
+ import type { SqlQueryPlan } from './plan';
20
+ import type { QueryLaneContext } from './query-lane-context';
21
+
22
+ export interface ParamPlaceholder {
23
+ readonly kind: 'param-placeholder';
24
+ readonly name: string;
25
+ }
26
+
27
+ /**
28
+ * ValueSource represents any value that can appear in a comparison or as an argument.
29
+ * This includes:
30
+ * - ParamPlaceholder: A parameter placeholder (e.g., `param('userId')`)
31
+ * - ExpressionSource: Something that can be converted to an Expression (ColumnBuilder, ExpressionBuilder)
32
+ */
33
+ export type ValueSource = ParamPlaceholder | ExpressionSource;
34
+
35
+ export interface OrderBuilder<
36
+ _ColumnName extends string = string,
37
+ _ColumnMeta extends StorageColumn = StorageColumn,
38
+ _JsType = unknown,
39
+ > {
40
+ readonly kind: 'order';
41
+ readonly expr: Expression;
42
+ readonly dir: Direction;
43
+ }
44
+
45
+ /**
46
+ * Creates an OrderBuilder for use in orderBy clauses.
47
+ */
48
+ export function createOrderBuilder(
49
+ expr: AnyColumnBuilder | OperationExpr,
50
+ dir: Direction,
51
+ ): AnyOrderBuilder {
52
+ return { kind: 'order', expr, dir } as AnyOrderBuilder;
53
+ }
54
+
55
+ /**
56
+ * ColumnBuilder with optional operation methods based on the column's typeId.
57
+ * When Operations is provided and the column's typeId matches, operation methods are included.
58
+ * Implements ExpressionSource to provide type-safe conversion to ColumnRef.
59
+ */
60
+ export type ColumnBuilder<
61
+ ColumnName extends string = string,
62
+ ColumnMeta extends StorageColumn = StorageColumn,
63
+ JsType = unknown,
64
+ Operations extends OperationTypes = Record<string, never>,
65
+ > = {
66
+ readonly kind: 'column';
67
+ readonly table: string;
68
+ readonly column: ColumnName;
69
+ readonly columnMeta: ColumnMeta;
70
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
71
+ eq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
72
+ neq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
73
+ gt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
74
+ lt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
75
+ gte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
76
+ lte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
77
+ asc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;
78
+ desc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;
79
+ /** Converts this column builder to a ColumnRef expression */
80
+ toExpr(): ColumnRef;
81
+ // Helper property for type extraction (not used at runtime)
82
+ readonly __jsType: JsType;
83
+ } & (ColumnMeta['codecId'] extends string
84
+ ? ColumnMeta['codecId'] extends keyof Operations
85
+ ? OperationMethods<
86
+ OperationsForTypeId<ColumnMeta['codecId'] & string, Operations>,
87
+ ColumnName,
88
+ StorageColumn,
89
+ JsType
90
+ >
91
+ : Record<string, never>
92
+ : Record<string, never>);
93
+
94
+ export interface BinaryBuilder<
95
+ _ColumnName extends string = string,
96
+ _ColumnMeta extends StorageColumn = StorageColumn,
97
+ _JsType = unknown,
98
+ > {
99
+ readonly kind: 'binary';
100
+ readonly op: BinaryOp;
101
+ readonly left: Expression;
102
+ readonly right: ValueSource;
103
+ }
104
+
105
+ // Forward declare AnyBinaryBuilder and AnyOrderBuilder for use in ExpressionBuilder
106
+ export type AnyBinaryBuilder = BinaryBuilder;
107
+ export type AnyOrderBuilder = OrderBuilder;
108
+
109
+ /**
110
+ * ExpressionBuilder represents the result of an operation (e.g., col.distance(...)).
111
+ * Unlike ColumnBuilder (which represents a column), ExpressionBuilder represents
112
+ * an operation expression and provides the same DSL methods for chaining.
113
+ *
114
+ * Implements ExpressionSource to provide type-safe conversion to OperationExpr.
115
+ */
116
+ export interface ExpressionBuilder<JsType = unknown> extends ExpressionSource {
117
+ readonly kind: 'expression';
118
+ readonly expr: OperationExpr;
119
+ readonly columnMeta: StorageColumn;
120
+
121
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
122
+ eq(value: ValueSource): AnyBinaryBuilder;
123
+ neq(value: ValueSource): AnyBinaryBuilder;
124
+ gt(value: ValueSource): AnyBinaryBuilder;
125
+ lt(value: ValueSource): AnyBinaryBuilder;
126
+ gte(value: ValueSource): AnyBinaryBuilder;
127
+ lte(value: ValueSource): AnyBinaryBuilder;
128
+ asc(): AnyOrderBuilder;
129
+ desc(): AnyOrderBuilder;
130
+
131
+ /** Converts this expression builder to the underlying OperationExpr */
132
+ toExpr(): OperationExpr;
133
+
134
+ // Helper property for type extraction (not used at runtime)
135
+ readonly __jsType: JsType;
136
+ }
137
+
138
+ // Helper aliases for usage sites where the specific column parameters are irrelevant
139
+ // Accepts any ColumnBuilder regardless of its Operations parameter
140
+ // Note: We use `any` here because TypeScript's variance rules don't allow us to express
141
+ // "any type that extends OperationTypes" in a way that works for assignment.
142
+ // Contract-specific OperationTypes (e.g., PgVectorOperationTypes) are not assignable
143
+ // to the base OperationTypes in generic parameter position, even though they extend it structurally.
144
+ // Helper type that accepts any ColumnBuilder regardless of its generic parameters
145
+ // This is needed because conditional types in ColumnBuilder create incompatible intersection types
146
+ // when Operations differs, even though structurally they're compatible
147
+ export type AnyColumnBuilderBase = {
148
+ readonly kind: 'column';
149
+ readonly table: string;
150
+ readonly column: string;
151
+ readonly columnMeta: StorageColumn;
152
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
153
+ eq(value: ValueSource): AnyBinaryBuilder;
154
+ neq(value: ValueSource): AnyBinaryBuilder;
155
+ gt(value: ValueSource): AnyBinaryBuilder;
156
+ lt(value: ValueSource): AnyBinaryBuilder;
157
+ gte(value: ValueSource): AnyBinaryBuilder;
158
+ lte(value: ValueSource): AnyBinaryBuilder;
159
+ asc(): AnyOrderBuilder;
160
+ desc(): AnyOrderBuilder;
161
+ toExpr(): ColumnRef;
162
+ readonly __jsType: unknown;
163
+ // Allow any operation methods (from conditional type)
164
+ readonly [key: string]: unknown;
165
+ };
166
+
167
+ export type AnyColumnBuilder =
168
+ | ColumnBuilder<
169
+ string,
170
+ StorageColumn,
171
+ unknown,
172
+ // biome-ignore lint/suspicious/noExplicitAny: AnyColumnBuilder must accept column builders with any operation types
173
+ any
174
+ >
175
+ | AnyColumnBuilderBase;
176
+
177
+ /**
178
+ * Union type for any builder that can produce an Expression.
179
+ * Used in DSL method signatures where either a column or operation result can be passed.
180
+ */
181
+ export type AnyExpressionSource = AnyColumnBuilder | ExpressionBuilder;
182
+
183
+ export function isColumnBuilder(value: unknown): value is AnyColumnBuilder {
184
+ return (
185
+ typeof value === 'object' &&
186
+ value !== null &&
187
+ 'kind' in value &&
188
+ (value as { kind: unknown }).kind === 'column'
189
+ );
190
+ }
191
+
192
+ export interface JoinOnBuilder {
193
+ eqCol(left: AnyColumnBuilder, right: AnyColumnBuilder): JoinOnPredicate;
194
+ }
195
+
196
+ export interface JoinOnPredicate {
197
+ readonly kind: 'join-on';
198
+ readonly left: AnyColumnBuilder;
199
+ readonly right: AnyColumnBuilder;
200
+ }
201
+
202
+ export type Expr = ColumnRef | ParamRef;
203
+
204
+ /**
205
+ * Helper type to extract codec output type from CodecTypes.
206
+ * Returns never if the codecId is not found in CodecTypes.
207
+ */
208
+ type ExtractCodecOutputType<
209
+ CodecId extends string,
210
+ CodecTypes extends Record<string, { readonly output: unknown }>,
211
+ > = CodecId extends keyof CodecTypes
212
+ ? CodecTypes[CodecId] extends { readonly output: infer Output }
213
+ ? Output
214
+ : never
215
+ : never;
216
+
217
+ /**
218
+ * Type-level operation signature.
219
+ * Represents an operation at the type level, similar to OperationSignature at runtime.
220
+ */
221
+ export type OperationTypeSignature = {
222
+ readonly args: ReadonlyArray<ArgSpec>;
223
+ readonly returns: ReturnSpec;
224
+ readonly lowering: SqlLoweringSpec;
225
+ readonly capabilities?: ReadonlyArray<string>;
226
+ };
227
+
228
+ /**
229
+ * Type-level operation registry.
230
+ * Maps typeId → operations, where operations is a record of method name → operation signature.
231
+ *
232
+ * Example:
233
+ * ```typescript
234
+ * type MyOperations: OperationTypes = {
235
+ * 'pg/vector@1': {
236
+ * cosineDistance: {
237
+ * args: [{ kind: 'typeId'; type: 'pg/vector@1' }];
238
+ * returns: { kind: 'builtin'; type: 'number' };
239
+ * lowering: { targetFamily: 'sql'; strategy: 'function'; template: '...' };
240
+ * };
241
+ * };
242
+ * };
243
+ * ```
244
+ */
245
+ export type OperationTypes = Record<string, Record<string, OperationTypeSignature>>;
246
+
247
+ /**
248
+ * CodecTypes represents a map of typeId to codec definitions.
249
+ * Each codec definition must have an `output` property indicating the JavaScript type.
250
+ *
251
+ * Example:
252
+ * ```typescript
253
+ * type MyCodecTypes: CodecTypes = {
254
+ * 'pg/int4@1': { output: number };
255
+ * 'pg/text@1': { output: string };
256
+ * };
257
+ * ```
258
+ */
259
+ export type CodecTypes = Record<string, { readonly output: unknown }>;
260
+
261
+ /**
262
+ * Extracts operations for a given typeId from the operation registry.
263
+ * Returns an empty record if the typeId is not found.
264
+ *
265
+ * @example
266
+ * ```typescript
267
+ * type Ops = OperationsForTypeId<'pg/vector@1', MyOperations>;
268
+ * // Ops = { cosineDistance: { ... }, l2Distance: { ... } }
269
+ * ```
270
+ */
271
+ export type OperationsForTypeId<
272
+ TypeId extends string,
273
+ Operations extends OperationTypes,
274
+ > = Operations extends Record<string, never>
275
+ ? Record<string, never>
276
+ : TypeId extends keyof Operations
277
+ ? Operations[TypeId]
278
+ : Record<string, never>;
279
+
280
+ /**
281
+ * Maps operation signatures to method signatures on ColumnBuilder.
282
+ * Each operation becomes a method that returns a ColumnBuilder or BinaryBuilder
283
+ * based on the return type.
284
+ */
285
+ type OperationMethods<
286
+ Ops extends Record<string, OperationTypeSignature>,
287
+ ColumnName extends string,
288
+ ColumnMeta extends StorageColumn,
289
+ JsType,
290
+ > = {
291
+ [K in keyof Ops]: Ops[K] extends OperationTypeSignature
292
+ ? (
293
+ ...args: OperationArgs<Ops[K]['args']>
294
+ ) => OperationReturn<Ops[K]['returns'], ColumnName, ColumnMeta, JsType>
295
+ : never;
296
+ };
297
+
298
+ /**
299
+ * Maps operation argument specs to TypeScript argument types.
300
+ * - typeId args: ColumnBuilder (accepts base columns or operation results)
301
+ * - param args: ParamPlaceholder
302
+ * - literal args: unknown (could be more specific in future)
303
+ */
304
+ type OperationArgs<Args extends ReadonlyArray<ArgSpec>> = Args extends readonly [
305
+ infer First,
306
+ ...infer Rest,
307
+ ]
308
+ ? First extends ArgSpec
309
+ ? [ArgToType<First>, ...(Rest extends ReadonlyArray<ArgSpec> ? OperationArgs<Rest> : [])]
310
+ : []
311
+ : [];
312
+
313
+ type ArgToType<Arg extends ArgSpec> = Arg extends { kind: 'typeId' }
314
+ ? AnyExpressionSource
315
+ : Arg extends { kind: 'param' }
316
+ ? ParamPlaceholder
317
+ : Arg extends { kind: 'literal' }
318
+ ? unknown
319
+ : never;
320
+
321
+ /**
322
+ * Maps operation return spec to return type.
323
+ * Operations return ExpressionBuilder, not ColumnBuilder, because the result
324
+ * represents an expression (OperationExpr) rather than a column reference.
325
+ */
326
+ type OperationReturn<
327
+ Returns extends ReturnSpec,
328
+ _ColumnName extends string,
329
+ _ColumnMeta extends StorageColumn,
330
+ _JsType,
331
+ > = Returns extends { kind: 'builtin'; type: infer T }
332
+ ? T extends 'number'
333
+ ? ExpressionBuilder<number>
334
+ : T extends 'boolean'
335
+ ? ExpressionBuilder<boolean>
336
+ : T extends 'string'
337
+ ? ExpressionBuilder<string>
338
+ : ExpressionBuilder<unknown>
339
+ : Returns extends { kind: 'typeId' }
340
+ ? ExpressionBuilder<unknown>
341
+ : ExpressionBuilder<unknown>;
342
+
343
+ /**
344
+ * Computes JavaScript type for a column at column creation time.
345
+ *
346
+ * Type inference:
347
+ * - Read columnMeta.codecId as typeId string literal
348
+ * - Look up CodecTypes[typeId].output
349
+ * - Apply nullability: nullable ? Output | null : Output
350
+ */
351
+ type ColumnMetaTypeId<ColumnMeta> = ColumnMeta extends { codecId: infer CodecId extends string }
352
+ ? CodecId
353
+ : ColumnMeta extends { type: infer TypeId extends string }
354
+ ? TypeId
355
+ : never;
356
+
357
+ export type ComputeColumnJsType<
358
+ _Contract extends SqlContract<SqlStorage>,
359
+ _TableName extends string,
360
+ _ColumnName extends string,
361
+ ColumnMeta extends StorageColumn,
362
+ CodecTypes extends Record<string, { readonly output: unknown }>,
363
+ > = ColumnMeta extends { nullable: infer Nullable }
364
+ ? ColumnMetaTypeId<ColumnMeta> extends infer TypeId
365
+ ? TypeId extends string
366
+ ? ExtractCodecOutputType<TypeId, CodecTypes> extends infer CodecOutput
367
+ ? [CodecOutput] extends [never]
368
+ ? unknown // Codec not found in CodecTypes
369
+ : Nullable extends true
370
+ ? CodecOutput | null
371
+ : CodecOutput
372
+ : unknown
373
+ : unknown
374
+ : unknown
375
+ : unknown;
376
+
377
+ /**
378
+ * Infers Row type from a projection object.
379
+ * Maps Record<string, ColumnBuilder> to Record<string, JSType>
380
+ *
381
+ * Extracts the pre-computed JsType from each ColumnBuilder in the projection.
382
+ */
383
+ /**
384
+ * Extracts the inferred JsType carried by a ColumnBuilder.
385
+ */
386
+ type ExtractJsTypeFromColumnBuilder<CB extends AnyColumnBuilder> =
387
+ CB extends ColumnBuilder<
388
+ infer _ColumnName extends string,
389
+ infer _ColumnMeta extends StorageColumn,
390
+ infer JsType,
391
+ infer _Ops
392
+ >
393
+ ? JsType
394
+ : never;
395
+
396
+ export type InferProjectionRow<P extends Record<string, AnyColumnBuilder>> = {
397
+ [K in keyof P]: ExtractJsTypeFromColumnBuilder<P[K]>;
398
+ };
399
+
400
+ /**
401
+ * Nested projection type - allows recursive nesting of ColumnBuilder, ExpressionBuilder, or nested objects.
402
+ */
403
+ export type NestedProjection = Record<
404
+ string,
405
+ | AnyExpressionSource
406
+ | Record<
407
+ string,
408
+ | AnyExpressionSource
409
+ | Record<
410
+ string,
411
+ | AnyExpressionSource
412
+ | Record<string, AnyExpressionSource | Record<string, AnyExpressionSource>>
413
+ >
414
+ >
415
+ >;
416
+
417
+ /**
418
+ * Helper type to extract include type from Includes map.
419
+ * Returns the value type if K is a key of Includes, otherwise returns unknown.
420
+ */
421
+ type ExtractIncludeType<
422
+ K extends string,
423
+ Includes extends Record<string, unknown>,
424
+ > = K extends keyof Includes ? Includes[K] : unknown;
425
+
426
+ /**
427
+ * Infers Row type from a nested projection object.
428
+ * Recursively maps Record<string, ColumnBuilder | boolean | NestedProjection> to nested object types.
429
+ *
430
+ * Extracts the pre-computed JsType from each ColumnBuilder at leaves.
431
+ * When a value is `true`, it represents an include reference and infers `Array<ChildShape>`
432
+ * by looking up the include alias in the Includes type map.
433
+ */
434
+ export type InferNestedProjectionRow<
435
+ P extends Record<string, AnyExpressionSource | boolean | NestedProjection>,
436
+ CodecTypes extends Record<string, { readonly output: unknown }> = Record<string, never>,
437
+ Includes extends Record<string, unknown> = Record<string, never>,
438
+ > = {
439
+ [K in keyof P]: P[K] extends ExpressionBuilder<infer JsType>
440
+ ? JsType
441
+ : P[K] extends AnyColumnBuilder
442
+ ? ExtractJsTypeFromColumnBuilder<P[K]>
443
+ : P[K] extends true
444
+ ? Array<ExtractIncludeType<K & string, Includes>> // Include reference - infers Array<ChildShape> from Includes map
445
+ : P[K] extends NestedProjection
446
+ ? InferNestedProjectionRow<P[K], CodecTypes, Includes>
447
+ : never;
448
+ };
449
+
450
+ /**
451
+ * Infers Row type from a tuple of ColumnBuilders used in returning() clause.
452
+ * Extracts column name and JsType from each ColumnBuilder and creates a Record.
453
+ */
454
+ export type InferReturningRow<Columns extends readonly AnyColumnBuilder[]> =
455
+ Columns extends readonly [infer First, ...infer Rest]
456
+ ? First extends ColumnBuilder<
457
+ infer Name,
458
+ infer _Meta,
459
+ infer JsType,
460
+ infer _Ops extends OperationTypes
461
+ >
462
+ ? Name extends string
463
+ ? Rest extends readonly AnyColumnBuilder[]
464
+ ? { [K in Name]: JsType } & InferReturningRow<Rest>
465
+ : { [K in Name]: JsType }
466
+ : never
467
+ : never
468
+ : Record<string, never>;
469
+
470
+ /**
471
+ * Utility type to check if a contract has the required capabilities for includeMany.
472
+ * Requires both `lateral` and `jsonAgg` to be `true` in the contract's capabilities for the target.
473
+ * Capabilities are nested by target: `{ [target]: { lateral: true, jsonAgg: true } }`
474
+ */
475
+ export type HasIncludeManyCapabilities<TContract extends SqlContract<SqlStorage>> =
476
+ TContract extends { capabilities: infer C; target: infer T }
477
+ ? T extends string
478
+ ? C extends Record<string, Record<string, boolean>>
479
+ ? C extends { [K in T]: infer TargetCaps }
480
+ ? TargetCaps extends { lateral: true; jsonAgg: true }
481
+ ? true
482
+ : false
483
+ : false
484
+ : false
485
+ : false
486
+ : false;
487
+
488
+ /**
489
+ * SQL-specific Plan type that refines the ast field to use QueryAst.
490
+ * This is the type used by SQL query builders.
491
+ */
492
+ export type SqlPlan<Row = unknown> = ExecutionPlan<Row, QueryAst>;
493
+
494
+ /**
495
+ * Helper types for extracting contract structure.
496
+ */
497
+ export type TablesOf<TContract> = TContract extends {
498
+ storage: { tables: infer U };
499
+ }
500
+ ? U
501
+ : never;
502
+
503
+ export type TableKey<TContract> = Extract<keyof TablesOf<TContract>, string>;
504
+
505
+ // Common types for contract.d.ts generation (SQL-specific)
506
+ // These types are used by emitted contract.d.ts files to provide type-safe DSL/ORM types
507
+
508
+ /**
509
+ * Unique symbol for metadata property to avoid collisions with user-defined properties
510
+ */
511
+ export declare const META: unique symbol;
512
+
513
+ /**
514
+ * Extracts metadata from a type that has a META property
515
+ */
516
+ export type Meta<T extends { [META]: unknown }> = T[typeof META];
517
+
518
+ /**
519
+ * Metadata interface for table definitions
520
+ */
521
+ export interface TableMetadata<Name extends string> {
522
+ name: Name;
523
+ }
524
+
525
+ /**
526
+ * Metadata interface for model definitions
527
+ */
528
+ export interface ModelMetadata<Name extends string> {
529
+ name: Name;
530
+ }
531
+
532
+ /**
533
+ * Base interface for table definitions with metadata
534
+ * Used in contract.d.ts to define storage-level table types
535
+ */
536
+ export interface TableDef<Name extends string> {
537
+ readonly [META]: TableMetadata<Name>;
538
+ }
539
+
540
+ /**
541
+ * Base interface for model definitions with metadata
542
+ * Used in contract.d.ts to define application-level model types
543
+ */
544
+ export interface ModelDef<Name extends string> {
545
+ readonly [META]: ModelMetadata<Name>;
546
+ }
547
+
548
+ export type ColumnsOf<
549
+ TContract,
550
+ K extends TableKey<TContract>,
551
+ > = K extends keyof TablesOf<TContract>
552
+ ? TablesOf<TContract>[K] extends { columns: infer C }
553
+ ? C
554
+ : never
555
+ : never;
556
+
557
+ export interface RawTemplateOptions {
558
+ readonly refs?: PlanRefs;
559
+ readonly annotations?: Record<string, unknown>;
560
+ readonly projection?: ReadonlyArray<string>;
561
+ }
562
+
563
+ export interface RawFunctionOptions extends RawTemplateOptions {
564
+ readonly params: ReadonlyArray<unknown>;
565
+ }
566
+
567
+ export type RawTemplateFactory = (
568
+ strings: TemplateStringsArray,
569
+ ...values: readonly unknown[]
570
+ ) => ExecutionPlan;
571
+
572
+ export interface RawFactory extends RawTemplateFactory {
573
+ (text: string, options: RawFunctionOptions): ExecutionPlan;
574
+ with(options: RawTemplateOptions): RawTemplateFactory;
575
+ }
576
+
577
+ export type { RuntimeError } from '@prisma-next/plan';
578
+
579
+ export interface BuildParamsMap {
580
+ readonly [name: string]: unknown;
581
+ }
582
+
583
+ export interface BuildOptions {
584
+ readonly params?: BuildParamsMap;
585
+ }
586
+
587
+ export interface SqlBuilderOptions<
588
+ TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
589
+ > {
590
+ readonly context: QueryLaneContext<TContract>;
591
+ }
592
+
593
+ /**
594
+ * SQL-specific ResultType that works with both Plan and SqlQueryPlan.
595
+ * This extends the core ResultType to also handle SqlQueryPlan.
596
+ * Example: `type Row = ResultType<typeof plan>`
597
+ */
598
+ export type ResultType<P> = P extends SqlQueryPlan<infer R> ? R : CoreResultType<P>;