@prisma-next/sql-relational-core 0.3.0-dev.4 → 0.3.0-dev.41

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 (138) hide show
  1. package/README.md +5 -2
  2. package/dist/errors-7_V3El9I.mjs +3 -0
  3. package/dist/errors-DVufq9PT.d.mts +2 -0
  4. package/dist/exports/ast.d.mts +189 -0
  5. package/dist/exports/ast.d.mts.map +1 -0
  6. package/dist/exports/ast.mjs +360 -0
  7. package/dist/exports/ast.mjs.map +1 -0
  8. package/dist/exports/errors.d.mts +2 -0
  9. package/dist/exports/errors.mjs +3 -0
  10. package/dist/exports/guards.d.mts +5 -0
  11. package/dist/exports/guards.mjs +3 -0
  12. package/dist/exports/operations-registry.d.mts +5 -0
  13. package/dist/exports/operations-registry.mjs +3 -0
  14. package/dist/exports/param.d.mts +5 -0
  15. package/dist/exports/param.mjs +3 -0
  16. package/dist/exports/plan.d.mts +2 -0
  17. package/dist/exports/plan.mjs +17 -0
  18. package/dist/exports/plan.mjs.map +1 -0
  19. package/dist/exports/query-lane-context.d.mts +2 -0
  20. package/dist/exports/query-lane-context.mjs +1 -0
  21. package/dist/exports/schema.d.mts +5 -0
  22. package/dist/exports/schema.mjs +4 -0
  23. package/dist/exports/types.d.mts +4 -0
  24. package/dist/exports/types.mjs +3 -0
  25. package/dist/exports/utils/guards.d.mts +5 -0
  26. package/dist/exports/utils/guards.mjs +4 -0
  27. package/dist/guards-0ycyntYX.mjs +132 -0
  28. package/dist/guards-0ycyntYX.mjs.map +1 -0
  29. package/dist/guards-l1R3q8pA.d.mts +87 -0
  30. package/dist/guards-l1R3q8pA.d.mts.map +1 -0
  31. package/dist/index.d.mts +12 -0
  32. package/dist/index.mjs +12 -0
  33. package/dist/{exports/operations-registry.d.ts → operations-registry-0GuSkOCC.d.mts} +7 -11
  34. package/dist/operations-registry-0GuSkOCC.d.mts.map +1 -0
  35. package/dist/operations-registry-DPZ5aElH.mjs +152 -0
  36. package/dist/operations-registry-DPZ5aElH.mjs.map +1 -0
  37. package/dist/param-ChBZwVcw.d.mts +8 -0
  38. package/dist/param-ChBZwVcw.d.mts.map +1 -0
  39. package/dist/param-DMU3OWfI.mjs +14 -0
  40. package/dist/param-DMU3OWfI.mjs.map +1 -0
  41. package/dist/plan-cjDF_yFX.d.mts +182 -0
  42. package/dist/plan-cjDF_yFX.d.mts.map +1 -0
  43. package/dist/query-lane-context-CgkPuKaR.d.mts +259 -0
  44. package/dist/query-lane-context-CgkPuKaR.d.mts.map +1 -0
  45. package/dist/schema-Bi5p4aAc.mjs +175 -0
  46. package/dist/schema-Bi5p4aAc.mjs.map +1 -0
  47. package/dist/schema-CEpzzWNY.d.mts +86 -0
  48. package/dist/schema-CEpzzWNY.d.mts.map +1 -0
  49. package/dist/types-Cdi4Whda.mjs +18 -0
  50. package/dist/types-Cdi4Whda.mjs.map +1 -0
  51. package/dist/types-DicmXfBm.d.mts +447 -0
  52. package/dist/types-DicmXfBm.d.mts.map +1 -0
  53. package/package.json +41 -57
  54. package/src/ast/adapter-types.ts +36 -0
  55. package/src/ast/codec-types.ts +444 -0
  56. package/src/ast/common.ts +36 -0
  57. package/src/ast/delete.ts +17 -0
  58. package/src/ast/driver-types.ts +42 -0
  59. package/src/ast/insert.ts +17 -0
  60. package/src/ast/join.ts +54 -0
  61. package/src/ast/order.ts +11 -0
  62. package/src/ast/predicate.ts +40 -0
  63. package/src/ast/select.ts +38 -0
  64. package/src/ast/sql-codecs.ts +67 -0
  65. package/src/ast/types.ts +187 -0
  66. package/src/ast/update.ts +19 -0
  67. package/src/ast/util.ts +9 -0
  68. package/src/exports/ast.ts +14 -0
  69. package/src/exports/errors.ts +1 -0
  70. package/src/exports/guards.ts +15 -0
  71. package/src/exports/operations-registry.ts +1 -0
  72. package/src/exports/param.ts +2 -0
  73. package/src/exports/plan.ts +1 -0
  74. package/src/exports/query-lane-context.ts +1 -0
  75. package/src/exports/schema.ts +6 -0
  76. package/src/exports/types.ts +1 -0
  77. package/src/exports/utils/guards.ts +1 -0
  78. package/src/index.ts +8 -0
  79. package/src/operations-registry.ts +276 -0
  80. package/src/param.ts +15 -0
  81. package/src/plan.ts +39 -0
  82. package/src/query-lane-context.ts +94 -0
  83. package/src/schema.ts +431 -0
  84. package/src/types.ts +799 -0
  85. package/src/utils/guards.ts +193 -0
  86. package/dist/chunk-2F7DSEOU.js +0 -8
  87. package/dist/chunk-2F7DSEOU.js.map +0 -1
  88. package/dist/chunk-36WJWNHT.js +0 -1
  89. package/dist/chunk-36WJWNHT.js.map +0 -1
  90. package/dist/chunk-5N34PNVZ.js +0 -62
  91. package/dist/chunk-5N34PNVZ.js.map +0 -1
  92. package/dist/chunk-7I3EMQID.js +0 -16
  93. package/dist/chunk-7I3EMQID.js.map +0 -1
  94. package/dist/chunk-CBTYMOX2.js +0 -152
  95. package/dist/chunk-CBTYMOX2.js.map +0 -1
  96. package/dist/chunk-G52ENULI.js +0 -1
  97. package/dist/chunk-G52ENULI.js.map +0 -1
  98. package/dist/chunk-KYSP7L5C.js +0 -16
  99. package/dist/chunk-KYSP7L5C.js.map +0 -1
  100. package/dist/chunk-M23L3JHG.js +0 -159
  101. package/dist/chunk-M23L3JHG.js.map +0 -1
  102. package/dist/chunk-MM74SVJ4.js +0 -13
  103. package/dist/chunk-MM74SVJ4.js.map +0 -1
  104. package/dist/chunk-U7AXAUJA.js +0 -1
  105. package/dist/chunk-U7AXAUJA.js.map +0 -1
  106. package/dist/chunk-WZBPVEZI.js +0 -320
  107. package/dist/chunk-WZBPVEZI.js.map +0 -1
  108. package/dist/exports/ast.d.ts +0 -119
  109. package/dist/exports/ast.js +0 -46
  110. package/dist/exports/ast.js.map +0 -1
  111. package/dist/exports/errors.js +0 -9
  112. package/dist/exports/errors.js.map +0 -1
  113. package/dist/exports/guards.d.ts +0 -63
  114. package/dist/exports/guards.js +0 -21
  115. package/dist/exports/guards.js.map +0 -1
  116. package/dist/exports/operations-registry.js +0 -9
  117. package/dist/exports/operations-registry.js.map +0 -1
  118. package/dist/exports/param.d.ts +0 -14
  119. package/dist/exports/param.js +0 -7
  120. package/dist/exports/param.js.map +0 -1
  121. package/dist/exports/plan.d.ts +0 -5
  122. package/dist/exports/plan.js +0 -7
  123. package/dist/exports/plan.js.map +0 -1
  124. package/dist/exports/query-lane-context.d.ts +0 -4
  125. package/dist/exports/query-lane-context.js +0 -2
  126. package/dist/exports/query-lane-context.js.map +0 -1
  127. package/dist/exports/schema.d.ts +0 -69
  128. package/dist/exports/schema.js +0 -14
  129. package/dist/exports/schema.js.map +0 -1
  130. package/dist/exports/types.d.ts +0 -335
  131. package/dist/exports/types.js +0 -10
  132. package/dist/exports/types.js.map +0 -1
  133. package/dist/index.d.ts +0 -13
  134. package/dist/index.js +0 -81
  135. package/dist/index.js.map +0 -1
  136. package/dist/plan-D0OG5qzy.d.ts +0 -142
  137. package/dist/query-lane-context-BhOMmb_K.d.ts +0 -158
  138. /package/{dist/exports/errors.d.ts → src/errors.ts} +0 -0
package/src/types.ts ADDED
@@ -0,0 +1,799 @@
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 { ExecutionContext } 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
+ * For nullable columns (ColumnMeta['nullable'] extends true), includes isNull() and isNotNull() methods.
61
+ */
62
+ export type ColumnBuilder<
63
+ ColumnName extends string = string,
64
+ ColumnMeta extends StorageColumn = StorageColumn,
65
+ JsType = unknown,
66
+ Operations extends OperationTypes = Record<string, never>,
67
+ > = {
68
+ readonly kind: 'column';
69
+ readonly table: string;
70
+ readonly column: ColumnName;
71
+ readonly columnMeta: ColumnMeta;
72
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
73
+ eq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
74
+ neq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
75
+ gt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
76
+ lt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
77
+ gte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
78
+ lte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;
79
+ asc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;
80
+ desc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;
81
+ /** Converts this column builder to a ColumnRef expression */
82
+ toExpr(): ColumnRef;
83
+ // Helper property for type extraction (not used at runtime)
84
+ readonly __jsType: JsType;
85
+ } & (ColumnMeta['codecId'] extends string
86
+ ? ColumnMeta['codecId'] extends keyof Operations
87
+ ? OperationMethods<
88
+ OperationsForTypeId<ColumnMeta['codecId'] & string, Operations>,
89
+ ColumnName,
90
+ StorageColumn,
91
+ JsType
92
+ >
93
+ : Record<string, never>
94
+ : Record<string, never>) &
95
+ (ColumnMeta['nullable'] extends true
96
+ ? NullableMethods<ColumnName, ColumnMeta, JsType>
97
+ : Record<string, never>);
98
+
99
+ export interface BinaryBuilder<
100
+ _ColumnName extends string = string,
101
+ _ColumnMeta extends StorageColumn = StorageColumn,
102
+ _JsType = unknown,
103
+ > {
104
+ readonly kind: 'binary';
105
+ readonly op: BinaryOp;
106
+ readonly left: Expression;
107
+ readonly right: ValueSource;
108
+ }
109
+
110
+ /**
111
+ * Builder for IS NULL / IS NOT NULL checks.
112
+ * Used to build unary null check expressions in WHERE clauses.
113
+ */
114
+ export interface NullCheckBuilder<
115
+ _ColumnName extends string = string,
116
+ _ColumnMeta extends StorageColumn = StorageColumn,
117
+ _JsType = unknown,
118
+ > {
119
+ readonly kind: 'nullCheck';
120
+ readonly expr: Expression;
121
+ readonly isNull: boolean;
122
+ }
123
+
124
+ /**
125
+ * Union type for unary builders (currently just NullCheckBuilder).
126
+ * Extensible for future unary operators.
127
+ */
128
+ export type UnaryBuilder = NullCheckBuilder;
129
+
130
+ // Forward declare AnyBinaryBuilder and AnyOrderBuilder for use in ExpressionBuilder
131
+ export type AnyBinaryBuilder = BinaryBuilder;
132
+ export type AnyOrderBuilder = OrderBuilder;
133
+ export type AnyUnaryBuilder = UnaryBuilder;
134
+
135
+ /**
136
+ * Methods available only on nullable columns.
137
+ * These are conditionally added to ColumnBuilder when ColumnMeta['nullable'] is true.
138
+ * Note: Index signature is required for compatibility with AnyColumnBuilderBase's index signature.
139
+ */
140
+ export interface NullableMethods<
141
+ ColumnName extends string = string,
142
+ ColumnMeta extends StorageColumn = StorageColumn,
143
+ JsType = unknown,
144
+ > {
145
+ /** Creates an IS NULL check for this column */
146
+ isNull(): NullCheckBuilder<ColumnName, ColumnMeta, JsType>;
147
+ /** Creates an IS NOT NULL check for this column */
148
+ isNotNull(): NullCheckBuilder<ColumnName, ColumnMeta, JsType>;
149
+ /** Index signature for compatibility with AnyColumnBuilderBase */
150
+ readonly [key: string]: unknown;
151
+ }
152
+
153
+ /**
154
+ * ExpressionBuilder represents the result of an operation (e.g., col.distance(...)).
155
+ * Unlike ColumnBuilder (which represents a column), ExpressionBuilder represents
156
+ * an operation expression and provides the same DSL methods for chaining.
157
+ *
158
+ * Implements ExpressionSource to provide type-safe conversion to OperationExpr.
159
+ */
160
+ export interface ExpressionBuilder<JsType = unknown> extends ExpressionSource {
161
+ readonly kind: 'expression';
162
+ readonly expr: OperationExpr;
163
+ readonly columnMeta: StorageColumn;
164
+
165
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
166
+ eq(value: ValueSource): AnyBinaryBuilder;
167
+ neq(value: ValueSource): AnyBinaryBuilder;
168
+ gt(value: ValueSource): AnyBinaryBuilder;
169
+ lt(value: ValueSource): AnyBinaryBuilder;
170
+ gte(value: ValueSource): AnyBinaryBuilder;
171
+ lte(value: ValueSource): AnyBinaryBuilder;
172
+ asc(): AnyOrderBuilder;
173
+ desc(): AnyOrderBuilder;
174
+
175
+ /** Converts this expression builder to the underlying OperationExpr */
176
+ toExpr(): OperationExpr;
177
+
178
+ // Helper property for type extraction (not used at runtime)
179
+ readonly __jsType: JsType;
180
+ }
181
+
182
+ // Helper aliases for usage sites where the specific column parameters are irrelevant
183
+ // Accepts any ColumnBuilder regardless of its Operations parameter
184
+ // Note: We use `any` here because TypeScript's variance rules don't allow us to express
185
+ // "any type that extends OperationTypes" in a way that works for assignment.
186
+ // Contract-specific OperationTypes (e.g., PgVectorOperationTypes) are not assignable
187
+ // to the base OperationTypes in generic parameter position, even though they extend it structurally.
188
+ // Helper type that accepts any ColumnBuilder regardless of its generic parameters
189
+ // This is needed because conditional types in ColumnBuilder create incompatible intersection types
190
+ // when Operations differs, even though structurally they're compatible
191
+ export type AnyColumnBuilderBase = {
192
+ readonly kind: 'column';
193
+ readonly table: string;
194
+ readonly column: string;
195
+ readonly columnMeta: StorageColumn;
196
+ // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)
197
+ eq(value: ValueSource): AnyBinaryBuilder;
198
+ neq(value: ValueSource): AnyBinaryBuilder;
199
+ gt(value: ValueSource): AnyBinaryBuilder;
200
+ lt(value: ValueSource): AnyBinaryBuilder;
201
+ gte(value: ValueSource): AnyBinaryBuilder;
202
+ lte(value: ValueSource): AnyBinaryBuilder;
203
+ asc(): AnyOrderBuilder;
204
+ desc(): AnyOrderBuilder;
205
+ toExpr(): ColumnRef;
206
+ readonly __jsType: unknown;
207
+ // Optional nullable methods (present when columnMeta.nullable is true)
208
+ isNull?(): AnyUnaryBuilder;
209
+ isNotNull?(): AnyUnaryBuilder;
210
+ // Allow any operation methods (from conditional type)
211
+ readonly [key: string]: unknown;
212
+ };
213
+
214
+ export type AnyColumnBuilder =
215
+ | ColumnBuilder<
216
+ string,
217
+ StorageColumn,
218
+ unknown,
219
+ // biome-ignore lint/suspicious/noExplicitAny: AnyColumnBuilder must accept column builders with any operation types
220
+ any
221
+ >
222
+ | AnyColumnBuilderBase;
223
+
224
+ /**
225
+ * Union type for any builder that can produce an Expression.
226
+ * Used in DSL method signatures where either a column or operation result can be passed.
227
+ */
228
+ export type AnyExpressionSource = AnyColumnBuilder | ExpressionBuilder;
229
+
230
+ export function isColumnBuilder(value: unknown): value is AnyColumnBuilder {
231
+ return (
232
+ typeof value === 'object' &&
233
+ value !== null &&
234
+ 'kind' in value &&
235
+ (value as { kind: unknown }).kind === 'column'
236
+ );
237
+ }
238
+
239
+ export interface JoinOnBuilder {
240
+ eqCol(left: AnyColumnBuilder, right: AnyColumnBuilder): JoinOnPredicate;
241
+ }
242
+
243
+ export interface JoinOnPredicate {
244
+ readonly kind: 'join-on';
245
+ readonly left: AnyColumnBuilder;
246
+ readonly right: AnyColumnBuilder;
247
+ }
248
+
249
+ export type Expr = ColumnRef | ParamRef;
250
+
251
+ /**
252
+ * Helper type to extract codec output type from CodecTypes.
253
+ * Returns never if the codecId is not found in CodecTypes.
254
+ */
255
+ type ExtractCodecOutputType<
256
+ CodecId extends string,
257
+ CodecTypes extends Record<string, { readonly output: unknown }>,
258
+ > = CodecId extends keyof CodecTypes
259
+ ? CodecTypes[CodecId] extends { readonly output: infer Output }
260
+ ? Output
261
+ : never
262
+ : never;
263
+
264
+ /**
265
+ * Extracts the model name for a given table from the contract mappings.
266
+ */
267
+ type ExtractTableToModel<
268
+ Contract extends SqlContract<SqlStorage>,
269
+ TableName extends string,
270
+ > = Contract['mappings'] extends {
271
+ readonly tableToModel: infer TableToModel;
272
+ }
273
+ ? TableToModel extends Record<string, string>
274
+ ? TableName extends keyof TableToModel
275
+ ? TableToModel[TableName]
276
+ : never
277
+ : never
278
+ : never;
279
+
280
+ /**
281
+ * Extracts the field name for a given table column from the contract mappings.
282
+ */
283
+ type ExtractColumnToField<
284
+ Contract extends SqlContract<SqlStorage>,
285
+ TableName extends string,
286
+ ColumnName extends string,
287
+ > = Contract['mappings'] extends {
288
+ readonly columnToField: infer ColumnToField;
289
+ }
290
+ ? ColumnToField extends Record<string, Record<string, string>>
291
+ ? TableName extends keyof ColumnToField
292
+ ? ColumnName extends keyof ColumnToField[TableName]
293
+ ? ColumnToField[TableName][ColumnName]
294
+ : never
295
+ : never
296
+ : never
297
+ : never;
298
+
299
+ /**
300
+ * Extracts the field value type from a model's fields.
301
+ */
302
+ type ExtractFieldValue<
303
+ Contract extends SqlContract<SqlStorage>,
304
+ ModelName extends string,
305
+ FieldName extends string,
306
+ > = Contract['models'] extends infer Models
307
+ ? Models extends Record<string, unknown>
308
+ ? ModelName extends keyof Models
309
+ ? Models[ModelName] extends { readonly fields: infer Fields }
310
+ ? Fields extends Record<string, unknown>
311
+ ? FieldName extends keyof Fields
312
+ ? Fields[FieldName]
313
+ : never
314
+ : never
315
+ : never
316
+ : never
317
+ : never
318
+ : never;
319
+
320
+ /**
321
+ * Extracts the JavaScript type for a column from model mappings if available.
322
+ * Returns `never` if the column maps to a ModelField object (which indicates
323
+ * a relation that should fall through to codec-based type resolution).
324
+ *
325
+ * The check for ModelField uses `Exclude<keyof FieldValue, 'column'> extends never`
326
+ * to ensure we only skip pure `{ column: string }` marker objects, not richer
327
+ * object types that happen to include a `column` property.
328
+ */
329
+ type ExtractColumnJsTypeFromModels<
330
+ Contract extends SqlContract<SqlStorage>,
331
+ TableName extends string,
332
+ ColumnName extends string,
333
+ > = ExtractTableToModel<Contract, TableName> extends infer ModelName
334
+ ? ModelName extends string
335
+ ? ExtractColumnToField<Contract, TableName, ColumnName> extends infer FieldName
336
+ ? FieldName extends string
337
+ ? ExtractFieldValue<Contract, ModelName, FieldName> extends infer FieldValue
338
+ ? FieldValue extends { readonly column: string }
339
+ ? Exclude<keyof FieldValue, 'column'> extends never
340
+ ? never
341
+ : FieldValue
342
+ : FieldValue
343
+ : never
344
+ : never
345
+ : never
346
+ : never
347
+ : never;
348
+
349
+ /**
350
+ * Resolves type params for a column from either:
351
+ * - inline `columnMeta.typeParams`, or
352
+ * - `columnMeta.typeRef` (resolving into `contract.storage.types[typeRef].typeParams`).
353
+ */
354
+ type ResolveColumnTypeParams<
355
+ Contract extends SqlContract<SqlStorage>,
356
+ ColumnMeta extends StorageColumn,
357
+ > = ColumnMeta extends { typeParams: infer Params }
358
+ ? Params extends object
359
+ ? Params
360
+ : undefined
361
+ : ColumnMeta extends { typeRef: infer TypeRef extends string }
362
+ ? Contract['storage'] extends { types: infer Types }
363
+ ? Types extends Record<string, unknown>
364
+ ? TypeRef extends keyof Types
365
+ ? Types[TypeRef] extends { typeParams: infer Params }
366
+ ? Params extends object
367
+ ? Params
368
+ : undefined
369
+ : undefined
370
+ : undefined
371
+ : undefined
372
+ : undefined
373
+ : undefined;
374
+
375
+ /**
376
+ * If a codec entry exposes a type-level parameterized output surface, compute the output type
377
+ * for a specific params object. Falls back to `never` if not supported.
378
+ *
379
+ * This enables lane typing to incorporate `columnMeta.typeParams` without branching on codec IDs
380
+ * in core lane code.
381
+ */
382
+ type ExtractParameterizedCodecOutputType<
383
+ CodecId extends string,
384
+ Params,
385
+ CodecTypes extends Record<string, { readonly output: unknown }>,
386
+ > = CodecId extends keyof CodecTypes
387
+ ? CodecTypes[CodecId] extends { readonly parameterizedOutput: infer Fn }
388
+ ? Fn extends (params: Params) => infer Out
389
+ ? Out
390
+ : never
391
+ : never
392
+ : never;
393
+
394
+ /**
395
+ * Type-level operation signature.
396
+ * Represents an operation at the type level, similar to OperationSignature at runtime.
397
+ */
398
+ export type OperationTypeSignature = {
399
+ readonly args: ReadonlyArray<ArgSpec>;
400
+ readonly returns: ReturnSpec;
401
+ readonly lowering: SqlLoweringSpec;
402
+ readonly capabilities?: ReadonlyArray<string>;
403
+ };
404
+
405
+ /**
406
+ * Type-level operation registry.
407
+ * Maps typeId → operations, where operations is a record of method name → operation signature.
408
+ *
409
+ * Example:
410
+ * ```typescript
411
+ * type MyOperations: OperationTypes = {
412
+ * 'pg/vector@1': {
413
+ * cosineDistance: {
414
+ * args: [{ kind: 'typeId'; type: 'pg/vector@1' }];
415
+ * returns: { kind: 'builtin'; type: 'number' };
416
+ * lowering: { targetFamily: 'sql'; strategy: 'function'; template: '...' };
417
+ * };
418
+ * };
419
+ * };
420
+ * ```
421
+ */
422
+ export type OperationTypes = Record<string, Record<string, OperationTypeSignature>>;
423
+
424
+ /**
425
+ * CodecTypes represents a map of typeId to codec definitions.
426
+ * Each codec definition must have an `output` property indicating the JavaScript type.
427
+ *
428
+ * Example:
429
+ * ```typescript
430
+ * type MyCodecTypes: CodecTypes = {
431
+ * 'pg/int4@1': { output: number };
432
+ * 'pg/text@1': { output: string };
433
+ * };
434
+ * ```
435
+ */
436
+ export type CodecTypes = Record<string, { readonly output: unknown }>;
437
+
438
+ /**
439
+ * Extracts operations for a given typeId from the operation registry.
440
+ * Returns an empty record if the typeId is not found.
441
+ *
442
+ * @example
443
+ * ```typescript
444
+ * type Ops = OperationsForTypeId<'pg/vector@1', MyOperations>;
445
+ * // Ops = { cosineDistance: { ... }, l2Distance: { ... } }
446
+ * ```
447
+ */
448
+ export type OperationsForTypeId<
449
+ TypeId extends string,
450
+ Operations extends OperationTypes,
451
+ > = Operations extends Record<string, never>
452
+ ? Record<string, never>
453
+ : TypeId extends keyof Operations
454
+ ? Operations[TypeId]
455
+ : Record<string, never>;
456
+
457
+ /**
458
+ * Maps operation signatures to method signatures on ColumnBuilder.
459
+ * Each operation becomes a method that returns a ColumnBuilder or BinaryBuilder
460
+ * based on the return type.
461
+ */
462
+ type OperationMethods<
463
+ Ops extends Record<string, OperationTypeSignature>,
464
+ ColumnName extends string,
465
+ ColumnMeta extends StorageColumn,
466
+ JsType,
467
+ > = {
468
+ [K in keyof Ops]: Ops[K] extends OperationTypeSignature
469
+ ? (
470
+ ...args: OperationArgs<Ops[K]['args']>
471
+ ) => OperationReturn<Ops[K]['returns'], ColumnName, ColumnMeta, JsType>
472
+ : never;
473
+ };
474
+
475
+ /**
476
+ * Maps operation argument specs to TypeScript argument types.
477
+ * - typeId args: ColumnBuilder (accepts base columns or operation results)
478
+ * - param args: ParamPlaceholder
479
+ * - literal args: unknown (could be more specific in future)
480
+ */
481
+ type OperationArgs<Args extends ReadonlyArray<ArgSpec>> = Args extends readonly [
482
+ infer First,
483
+ ...infer Rest,
484
+ ]
485
+ ? First extends ArgSpec
486
+ ? [ArgToType<First>, ...(Rest extends ReadonlyArray<ArgSpec> ? OperationArgs<Rest> : [])]
487
+ : []
488
+ : [];
489
+
490
+ type ArgToType<Arg extends ArgSpec> = Arg extends { kind: 'typeId' }
491
+ ? AnyExpressionSource
492
+ : Arg extends { kind: 'param' }
493
+ ? ParamPlaceholder
494
+ : Arg extends { kind: 'literal' }
495
+ ? unknown
496
+ : never;
497
+
498
+ /**
499
+ * Maps operation return spec to return type.
500
+ * Operations return ExpressionBuilder, not ColumnBuilder, because the result
501
+ * represents an expression (OperationExpr) rather than a column reference.
502
+ */
503
+ type OperationReturn<
504
+ Returns extends ReturnSpec,
505
+ _ColumnName extends string,
506
+ _ColumnMeta extends StorageColumn,
507
+ _JsType,
508
+ > = Returns extends { kind: 'builtin'; type: infer T }
509
+ ? T extends 'number'
510
+ ? ExpressionBuilder<number>
511
+ : T extends 'boolean'
512
+ ? ExpressionBuilder<boolean>
513
+ : T extends 'string'
514
+ ? ExpressionBuilder<string>
515
+ : ExpressionBuilder<unknown>
516
+ : Returns extends { kind: 'typeId' }
517
+ ? ExpressionBuilder<unknown>
518
+ : ExpressionBuilder<unknown>;
519
+
520
+ /**
521
+ * Computes JavaScript type for a column at column creation time.
522
+ *
523
+ * Type inference:
524
+ * - Read columnMeta.codecId as typeId string literal
525
+ * - Look up CodecTypes[typeId].output
526
+ * - Apply nullability: nullable ? Output | null : Output
527
+ */
528
+ type ColumnMetaTypeId<ColumnMeta> = ColumnMeta extends { codecId: infer CodecId extends string }
529
+ ? CodecId
530
+ : ColumnMeta extends { type: infer TypeId extends string }
531
+ ? TypeId
532
+ : never;
533
+
534
+ export type ComputeColumnJsType<
535
+ Contract extends SqlContract<SqlStorage>,
536
+ TableName extends string,
537
+ ColumnName extends string,
538
+ ColumnMeta extends StorageColumn,
539
+ CodecTypes extends Record<string, { readonly output: unknown }>,
540
+ > = ExtractColumnJsTypeFromModels<Contract, TableName, ColumnName> extends infer FromModels
541
+ ? [FromModels] extends [never]
542
+ ? ColumnMeta extends { nullable: infer Nullable }
543
+ ? ColumnMetaTypeId<ColumnMeta> extends infer TypeId
544
+ ? TypeId extends string
545
+ ? ResolveColumnTypeParams<Contract, ColumnMeta> extends infer Params
546
+ ? Params extends object
547
+ ? ExtractParameterizedCodecOutputType<
548
+ TypeId,
549
+ Params,
550
+ CodecTypes
551
+ > extends infer ParamOutput
552
+ ? [ParamOutput] extends [never]
553
+ ? ExtractCodecOutputType<TypeId, CodecTypes> extends infer CodecOutput
554
+ ? [CodecOutput] extends [never]
555
+ ? unknown // Codec not found in CodecTypes
556
+ : Nullable extends true
557
+ ? CodecOutput | null
558
+ : CodecOutput
559
+ : unknown
560
+ : Nullable extends true
561
+ ? ParamOutput | null
562
+ : ParamOutput
563
+ : unknown
564
+ : ExtractCodecOutputType<TypeId, CodecTypes> extends infer CodecOutput
565
+ ? [CodecOutput] extends [never]
566
+ ? unknown // Codec not found in CodecTypes
567
+ : Nullable extends true
568
+ ? CodecOutput | null
569
+ : CodecOutput
570
+ : unknown
571
+ : unknown
572
+ : unknown
573
+ : unknown
574
+ : unknown
575
+ : FromModels
576
+ : unknown;
577
+
578
+ /**
579
+ * Infers Row type from a projection object.
580
+ * Maps Record<string, ColumnBuilder> to Record<string, JSType>
581
+ *
582
+ * Extracts the pre-computed JsType from each ColumnBuilder in the projection.
583
+ */
584
+ /**
585
+ * Extracts the inferred JsType carried by a ColumnBuilder.
586
+ */
587
+ type ExtractJsTypeFromColumnBuilder<CB extends AnyColumnBuilder> =
588
+ CB extends ColumnBuilder<
589
+ infer _ColumnName extends string,
590
+ infer _ColumnMeta extends StorageColumn,
591
+ infer JsType,
592
+ infer _Ops
593
+ >
594
+ ? JsType
595
+ : never;
596
+
597
+ export type InferProjectionRow<P extends Record<string, AnyColumnBuilder>> = {
598
+ [K in keyof P]: ExtractJsTypeFromColumnBuilder<P[K]>;
599
+ };
600
+
601
+ /**
602
+ * Nested projection type - allows recursive nesting of ColumnBuilder, ExpressionBuilder, or nested objects.
603
+ */
604
+ export type NestedProjection = Record<
605
+ string,
606
+ | AnyExpressionSource
607
+ | Record<
608
+ string,
609
+ | AnyExpressionSource
610
+ | Record<
611
+ string,
612
+ | AnyExpressionSource
613
+ | Record<string, AnyExpressionSource | Record<string, AnyExpressionSource>>
614
+ >
615
+ >
616
+ >;
617
+
618
+ /**
619
+ * Helper type to extract include type from Includes map.
620
+ * Returns the value type if K is a key of Includes, otherwise returns unknown.
621
+ */
622
+ type ExtractIncludeType<
623
+ K extends string,
624
+ Includes extends Record<string, unknown>,
625
+ > = K extends keyof Includes ? Includes[K] : unknown;
626
+
627
+ /**
628
+ * Infers Row type from a nested projection object.
629
+ * Recursively maps Record<string, ColumnBuilder | boolean | NestedProjection> to nested object types.
630
+ *
631
+ * Extracts the pre-computed JsType from each ColumnBuilder at leaves.
632
+ * When a value is `true`, it represents an include reference and infers `Array<ChildShape>`
633
+ * by looking up the include alias in the Includes type map.
634
+ */
635
+ export type InferNestedProjectionRow<
636
+ P extends Record<string, AnyExpressionSource | boolean | NestedProjection>,
637
+ CodecTypes extends Record<string, { readonly output: unknown }> = Record<string, never>,
638
+ Includes extends Record<string, unknown> = Record<string, never>,
639
+ > = {
640
+ [K in keyof P]: P[K] extends ExpressionBuilder<infer JsType>
641
+ ? JsType
642
+ : P[K] extends AnyColumnBuilder
643
+ ? ExtractJsTypeFromColumnBuilder<P[K]>
644
+ : P[K] extends true
645
+ ? Array<ExtractIncludeType<K & string, Includes>> // Include reference - infers Array<ChildShape> from Includes map
646
+ : P[K] extends NestedProjection
647
+ ? InferNestedProjectionRow<P[K], CodecTypes, Includes>
648
+ : never;
649
+ };
650
+
651
+ /**
652
+ * Infers Row type from a tuple of ColumnBuilders used in returning() clause.
653
+ * Extracts column name and JsType from each ColumnBuilder and creates a Record.
654
+ */
655
+ export type InferReturningRow<Columns extends readonly AnyColumnBuilder[]> =
656
+ Columns extends readonly [infer First, ...infer Rest]
657
+ ? First extends ColumnBuilder<
658
+ infer Name,
659
+ infer _Meta,
660
+ infer JsType,
661
+ infer _Ops extends OperationTypes
662
+ >
663
+ ? Name extends string
664
+ ? Rest extends readonly AnyColumnBuilder[]
665
+ ? { [K in Name]: JsType } & InferReturningRow<Rest>
666
+ : { [K in Name]: JsType }
667
+ : never
668
+ : never
669
+ : Record<string, never>;
670
+
671
+ /**
672
+ * Utility type to check if a contract has the required capabilities for includeMany.
673
+ * Requires both `lateral` and `jsonAgg` to be `true` in the contract's capabilities for the target.
674
+ * Capabilities are nested by target: `{ [target]: { lateral: true, jsonAgg: true } }`
675
+ */
676
+ export type HasIncludeManyCapabilities<TContract extends SqlContract<SqlStorage>> =
677
+ TContract extends { capabilities: infer C; target: infer T }
678
+ ? T extends string
679
+ ? C extends Record<string, Record<string, boolean>>
680
+ ? C extends { [K in T]: infer TargetCaps }
681
+ ? TargetCaps extends { lateral: true; jsonAgg: true }
682
+ ? true
683
+ : false
684
+ : false
685
+ : false
686
+ : false
687
+ : false;
688
+
689
+ /**
690
+ * SQL-specific Plan type that refines the ast field to use QueryAst.
691
+ * This is the type used by SQL query builders.
692
+ */
693
+ export type SqlPlan<Row = unknown> = ExecutionPlan<Row, QueryAst>;
694
+
695
+ /**
696
+ * Helper types for extracting contract structure.
697
+ */
698
+ export type TablesOf<TContract> = TContract extends {
699
+ storage: { tables: infer U };
700
+ }
701
+ ? U
702
+ : never;
703
+
704
+ export type TableKey<TContract> = Extract<keyof TablesOf<TContract>, string>;
705
+
706
+ // Common types for contract.d.ts generation (SQL-specific)
707
+ // These types are used by emitted contract.d.ts files to provide type-safe DSL/ORM types
708
+
709
+ /**
710
+ * Unique symbol for metadata property to avoid collisions with user-defined properties
711
+ */
712
+ export declare const META: unique symbol;
713
+
714
+ /**
715
+ * Extracts metadata from a type that has a META property
716
+ */
717
+ export type Meta<T extends { [META]: unknown }> = T[typeof META];
718
+
719
+ /**
720
+ * Metadata interface for table definitions
721
+ */
722
+ export interface TableMetadata<Name extends string> {
723
+ name: Name;
724
+ }
725
+
726
+ /**
727
+ * Metadata interface for model definitions
728
+ */
729
+ export interface ModelMetadata<Name extends string> {
730
+ name: Name;
731
+ }
732
+
733
+ /**
734
+ * Base interface for table definitions with metadata
735
+ * Used in contract.d.ts to define storage-level table types
736
+ */
737
+ export interface TableDef<Name extends string> {
738
+ readonly [META]: TableMetadata<Name>;
739
+ }
740
+
741
+ /**
742
+ * Base interface for model definitions with metadata
743
+ * Used in contract.d.ts to define application-level model types
744
+ */
745
+ export interface ModelDef<Name extends string> {
746
+ readonly [META]: ModelMetadata<Name>;
747
+ }
748
+
749
+ export type ColumnsOf<
750
+ TContract,
751
+ K extends TableKey<TContract>,
752
+ > = K extends keyof TablesOf<TContract>
753
+ ? TablesOf<TContract>[K] extends { columns: infer C }
754
+ ? C
755
+ : never
756
+ : never;
757
+
758
+ export interface RawTemplateOptions {
759
+ readonly refs?: PlanRefs;
760
+ readonly annotations?: Record<string, unknown>;
761
+ readonly projection?: ReadonlyArray<string>;
762
+ }
763
+
764
+ export interface RawFunctionOptions extends RawTemplateOptions {
765
+ readonly params: ReadonlyArray<unknown>;
766
+ }
767
+
768
+ export type RawTemplateFactory = (
769
+ strings: TemplateStringsArray,
770
+ ...values: readonly unknown[]
771
+ ) => ExecutionPlan;
772
+
773
+ export interface RawFactory extends RawTemplateFactory {
774
+ (text: string, options: RawFunctionOptions): ExecutionPlan;
775
+ with(options: RawTemplateOptions): RawTemplateFactory;
776
+ }
777
+
778
+ export type { RuntimeError } from '@prisma-next/plan';
779
+
780
+ export interface BuildParamsMap {
781
+ readonly [name: string]: unknown;
782
+ }
783
+
784
+ export interface BuildOptions {
785
+ readonly params?: BuildParamsMap;
786
+ }
787
+
788
+ export interface SqlBuilderOptions<
789
+ TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
790
+ > {
791
+ readonly context: ExecutionContext<TContract>;
792
+ }
793
+
794
+ /**
795
+ * SQL-specific ResultType that works with both Plan and SqlQueryPlan.
796
+ * This extends the core ResultType to also handle SqlQueryPlan.
797
+ * Example: `type Row = ResultType<typeof plan>`
798
+ */
799
+ export type ResultType<P> = P extends SqlQueryPlan<infer R> ? R : CoreResultType<P>;