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

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/schema.ts ADDED
@@ -0,0 +1,406 @@
1
+ import type { OperationRegistry } from '@prisma-next/operations';
2
+ import { planInvalid } from '@prisma-next/plan';
3
+ import type {
4
+ ExtractCodecTypes,
5
+ ExtractOperationTypes,
6
+ SqlContract,
7
+ SqlStorage,
8
+ StorageColumn,
9
+ } from '@prisma-next/sql-contract/types';
10
+ import type { BinaryOp, ColumnRef, ExpressionSource, TableRef } from './ast/types';
11
+ import { attachOperationsToColumnBuilder } from './operations-registry';
12
+ import type { QueryLaneContext } from './query-lane-context';
13
+ import type {
14
+ BinaryBuilder,
15
+ CodecTypes as CodecTypesType,
16
+ ColumnBuilder,
17
+ ComputeColumnJsType,
18
+ OperationTypeSignature,
19
+ OperationTypes,
20
+ OrderBuilder,
21
+ ParamPlaceholder,
22
+ } from './types';
23
+
24
+ type TableColumns<Table extends { columns: Record<string, StorageColumn> }> = Table['columns'];
25
+
26
+ type ColumnBuilders<
27
+ Contract extends SqlContract<SqlStorage>,
28
+ TableName extends string,
29
+ Columns extends Record<string, StorageColumn>,
30
+ CodecTypes extends CodecTypesType,
31
+ Operations extends OperationTypes,
32
+ > = {
33
+ readonly [K in keyof Columns]: ColumnBuilder<
34
+ K & string,
35
+ Columns[K],
36
+ ComputeColumnJsType<Contract, TableName, K & string, Columns[K], CodecTypes>,
37
+ Operations
38
+ >;
39
+ };
40
+
41
+ export class ColumnBuilderImpl<
42
+ ColumnName extends string,
43
+ ColumnMeta extends StorageColumn,
44
+ JsType = unknown,
45
+ > implements ExpressionSource
46
+ {
47
+ readonly kind = 'column' as const;
48
+
49
+ constructor(
50
+ readonly table: string,
51
+ readonly column: ColumnName,
52
+ private readonly storageColumn: ColumnMeta,
53
+ ) {}
54
+
55
+ get columnMeta(): ColumnMeta {
56
+ return this.storageColumn;
57
+ }
58
+
59
+ // Type-level helper property (not used at runtime)
60
+ get __jsType(): JsType {
61
+ return undefined as unknown as JsType;
62
+ }
63
+
64
+ /**
65
+ * Converts this column builder to a ColumnRef expression.
66
+ * This is the canonical way to get an AST node from a builder.
67
+ */
68
+ toExpr(): ColumnRef {
69
+ return Object.freeze({
70
+ kind: 'col' as const,
71
+ table: this.table,
72
+ column: this.column,
73
+ });
74
+ }
75
+
76
+ private createBinaryBuilder(
77
+ op: BinaryOp,
78
+ value: ParamPlaceholder | ExpressionSource,
79
+ ): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
80
+ if (value == null) {
81
+ throw planInvalid(
82
+ 'Parameter placeholder or expression source required for column comparison',
83
+ );
84
+ }
85
+ // Check for ExpressionSource first (has toExpr method)
86
+ if ('toExpr' in value && typeof value.toExpr === 'function') {
87
+ return Object.freeze({
88
+ kind: 'binary' as const,
89
+ op,
90
+ left: this.toExpr(),
91
+ right: value,
92
+ }) as BinaryBuilder<ColumnName, ColumnMeta, JsType>;
93
+ }
94
+ // Must be a ParamPlaceholder
95
+ if ('kind' in value && value.kind === 'param-placeholder') {
96
+ return Object.freeze({
97
+ kind: 'binary' as const,
98
+ op,
99
+ left: this.toExpr(),
100
+ right: value,
101
+ }) as BinaryBuilder<ColumnName, ColumnMeta, JsType>;
102
+ }
103
+ throw planInvalid('Parameter placeholder or expression source required for column comparison');
104
+ }
105
+
106
+ eq(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
107
+ return this.createBinaryBuilder('eq', value);
108
+ }
109
+
110
+ neq(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
111
+ return this.createBinaryBuilder('neq', value);
112
+ }
113
+
114
+ gt(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
115
+ return this.createBinaryBuilder('gt', value);
116
+ }
117
+
118
+ lt(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
119
+ return this.createBinaryBuilder('lt', value);
120
+ }
121
+
122
+ gte(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
123
+ return this.createBinaryBuilder('gte', value);
124
+ }
125
+
126
+ lte(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
127
+ return this.createBinaryBuilder('lte', value);
128
+ }
129
+
130
+ asc(): OrderBuilder<ColumnName, ColumnMeta, JsType> {
131
+ return Object.freeze({
132
+ kind: 'order' as const,
133
+ expr: this.toExpr(),
134
+ dir: 'asc' as const,
135
+ }) as OrderBuilder<ColumnName, ColumnMeta, JsType>;
136
+ }
137
+
138
+ desc(): OrderBuilder<ColumnName, ColumnMeta, JsType> {
139
+ return Object.freeze({
140
+ kind: 'order' as const,
141
+ expr: this.toExpr(),
142
+ dir: 'desc' as const,
143
+ }) as OrderBuilder<ColumnName, ColumnMeta, JsType>;
144
+ }
145
+ }
146
+
147
+ export class TableBuilderImpl<
148
+ Contract extends SqlContract<SqlStorage>,
149
+ TableName extends string,
150
+ Columns extends Record<string, StorageColumn>,
151
+ CodecTypes extends CodecTypesType,
152
+ Operations extends OperationTypes,
153
+ > implements TableRef
154
+ {
155
+ readonly kind = 'table' as const;
156
+ readonly columns: ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>;
157
+ private readonly _name: TableName;
158
+
159
+ constructor(
160
+ name: TableName,
161
+ columns: ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>,
162
+ ) {
163
+ this._name = name;
164
+ this.columns = columns;
165
+ }
166
+
167
+ get name(): string {
168
+ return this._name;
169
+ }
170
+ }
171
+
172
+ function buildColumns<
173
+ Contract extends SqlContract<SqlStorage>,
174
+ TableName extends keyof Contract['storage']['tables'] & string,
175
+ CodecTypes extends CodecTypesType,
176
+ Operations extends OperationTypes,
177
+ >(
178
+ tableName: TableName,
179
+ storage: SqlStorage,
180
+ _contract: Contract,
181
+ operationRegistry?: OperationRegistry,
182
+ contractCapabilities?: Record<string, Record<string, boolean>>,
183
+ ): ColumnBuilders<
184
+ Contract,
185
+ TableName,
186
+ Contract['storage']['tables'][TableName]['columns'],
187
+ CodecTypes,
188
+ Operations
189
+ > {
190
+ const table = storage.tables[tableName];
191
+
192
+ if (!table) {
193
+ throw planInvalid(`Unknown table ${tableName}`);
194
+ }
195
+
196
+ type Columns = Contract['storage']['tables'][TableName]['columns'];
197
+ const tableColumns = table.columns as Columns;
198
+
199
+ const result = {} as {
200
+ [K in keyof Columns]: ColumnBuilder<
201
+ K & string,
202
+ Columns[K],
203
+ ComputeColumnJsType<Contract, TableName, K & string, Columns[K], CodecTypes>,
204
+ Operations
205
+ >;
206
+ };
207
+
208
+ const assignColumn = <ColumnKey extends keyof Columns & string>(
209
+ columnName: ColumnKey,
210
+ columnDef: Columns[ColumnKey],
211
+ ) => {
212
+ type JsType = ComputeColumnJsType<
213
+ Contract,
214
+ TableName,
215
+ ColumnKey,
216
+ Columns[ColumnKey],
217
+ CodecTypes
218
+ >;
219
+
220
+ const columnBuilder = new ColumnBuilderImpl<ColumnKey, Columns[ColumnKey], JsType>(
221
+ tableName,
222
+ columnName,
223
+ columnDef,
224
+ );
225
+
226
+ const builderWithOps = attachOperationsToColumnBuilder<
227
+ ColumnKey,
228
+ Columns[ColumnKey],
229
+ JsType,
230
+ Operations
231
+ >(
232
+ columnBuilder as unknown as ColumnBuilder<
233
+ ColumnKey,
234
+ Columns[ColumnKey],
235
+ JsType,
236
+ Record<string, never>
237
+ >,
238
+ columnDef,
239
+ operationRegistry,
240
+ contractCapabilities,
241
+ );
242
+
243
+ (result as Record<string, unknown>)[columnName] = builderWithOps;
244
+ };
245
+
246
+ for (const columnName of Object.keys(tableColumns) as Array<keyof Columns & string>) {
247
+ const columnDef = tableColumns[columnName];
248
+ if (!columnDef) continue;
249
+ assignColumn(columnName, columnDef);
250
+ }
251
+
252
+ return result as ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>;
253
+ }
254
+
255
+ /**
256
+ * Creates a Proxy that enables accessing table columns directly on the table object,
257
+ * in addition to the standard `table.columns.columnName` syntax.
258
+ *
259
+ * This allows both access patterns:
260
+ * - `tables.user.columns.id` (standard access)
261
+ * - `tables.user.id` (convenience access via proxy)
262
+ *
263
+ * The proxy intercepts property access and routes column name lookups to
264
+ * `table.columns[prop]`, while preserving direct access to table properties
265
+ * like `name`, `kind`, and `columns`.
266
+ */
267
+ function createTableProxy<
268
+ Contract extends SqlContract<SqlStorage>,
269
+ TableName extends string,
270
+ Columns extends Record<string, StorageColumn>,
271
+ CodecTypes extends CodecTypesType,
272
+ Operations extends OperationTypes,
273
+ >(
274
+ table: TableBuilderImpl<Contract, TableName, Columns, CodecTypes, Operations>,
275
+ ): TableBuilderImpl<Contract, TableName, Columns, CodecTypes, Operations> {
276
+ return new Proxy(table, {
277
+ get(target, prop) {
278
+ if (prop === 'name' || prop === 'kind' || prop === 'columns') {
279
+ return Reflect.get(target, prop);
280
+ }
281
+ if (typeof prop === 'string' && prop in target.columns) {
282
+ return target.columns[prop as keyof typeof target.columns];
283
+ }
284
+ return undefined;
285
+ },
286
+ });
287
+ }
288
+
289
+ type ExtractSchemaTables<
290
+ Contract extends SqlContract<SqlStorage>,
291
+ CodecTypes extends CodecTypesType,
292
+ Operations extends OperationTypes,
293
+ > = {
294
+ readonly [TableName in keyof Contract['storage']['tables']]: TableBuilderImpl<
295
+ Contract,
296
+ TableName & string,
297
+ TableColumns<Contract['storage']['tables'][TableName]>,
298
+ CodecTypes,
299
+ Operations
300
+ > &
301
+ TableRef;
302
+ };
303
+
304
+ /**
305
+ * Extracts the types registry shape from a contract.
306
+ * Each key is a type name from storage.types, and the value preserves the
307
+ * literal type from the contract (including codecId, nativeType, and typeParams).
308
+ * Returns an empty object type {} when storage.types is undefined.
309
+ */
310
+ type ExtractSchemaTypes<Contract extends SqlContract<SqlStorage>> =
311
+ Contract['storage']['types'] extends infer Types
312
+ ? Types extends Record<string, unknown>
313
+ ? { readonly [TypeName in keyof Types]: Types[TypeName] }
314
+ : Record<string, never>
315
+ : Record<string, never>;
316
+
317
+ export type SchemaHandle<
318
+ Contract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
319
+ CodecTypes extends CodecTypesType = CodecTypesType,
320
+ Operations extends OperationTypes = Record<string, never>,
321
+ > = {
322
+ readonly tables: ExtractSchemaTables<Contract, CodecTypes, Operations>;
323
+ /**
324
+ * Initialized type helpers from storage.types.
325
+ * Each entry corresponds to a named type instance in the contract's storage.types.
326
+ */
327
+ readonly types: ExtractSchemaTypes<Contract>;
328
+ };
329
+
330
+ type SchemaReturnType<Contract extends SqlContract<SqlStorage>> = SchemaHandle<
331
+ Contract,
332
+ ExtractCodecTypes<Contract>,
333
+ ToOperationTypes<ExtractOperationTypes<Contract>>
334
+ >;
335
+
336
+ type NormalizeOperationTypes<T> = {
337
+ [TypeId in keyof T]: {
338
+ [Method in keyof T[TypeId]]: T[TypeId][Method] extends OperationTypeSignature
339
+ ? T[TypeId][Method]
340
+ : OperationTypeSignature;
341
+ };
342
+ };
343
+
344
+ type ToOperationTypes<T> = T extends OperationTypes ? T : NormalizeOperationTypes<T>;
345
+
346
+ /**
347
+ * Creates a schema handle for building SQL queries.
348
+ *
349
+ * @param context - Query lane context containing contract, codec and operation registries
350
+ * @returns A schema handle with typed table builders and type helpers
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * const schemaHandle = schema<Contract>(context);
355
+ * const userTable = schemaHandle.tables.user;
356
+ * const vectorType = schemaHandle.types.Vector1536;
357
+ * ```
358
+ */
359
+ export function schema<Contract extends SqlContract<SqlStorage>>(
360
+ context: QueryLaneContext<Contract>,
361
+ ): SchemaReturnType<Contract> {
362
+ const contract = context.contract;
363
+ const storage = contract.storage;
364
+ type CodecTypes = ExtractCodecTypes<Contract>;
365
+ type Operations = ToOperationTypes<ExtractOperationTypes<Contract>>;
366
+ const tables = {} as ExtractSchemaTables<Contract, CodecTypes, Operations>;
367
+ const contractCapabilities = contract.capabilities;
368
+
369
+ const operationRegistry = context.operations;
370
+
371
+ for (const tableName of Object.keys(storage.tables) as Array<
372
+ keyof Contract['storage']['tables'] & string
373
+ >) {
374
+ const columns = buildColumns<Contract, typeof tableName, CodecTypes, Operations>(
375
+ tableName,
376
+ storage,
377
+ contract,
378
+ operationRegistry,
379
+ contractCapabilities,
380
+ );
381
+ const table = new TableBuilderImpl<
382
+ Contract,
383
+ typeof tableName & string,
384
+ Contract['storage']['tables'][typeof tableName]['columns'],
385
+ CodecTypes,
386
+ Operations
387
+ >(tableName, columns);
388
+ const proxiedTable = createTableProxy<
389
+ Contract,
390
+ typeof tableName & string,
391
+ Contract['storage']['tables'][typeof tableName]['columns'],
392
+ CodecTypes,
393
+ Operations
394
+ >(table);
395
+ (tables as Record<string, unknown>)[tableName] = Object.freeze(
396
+ proxiedTable,
397
+ ) as ExtractSchemaTables<Contract, CodecTypes, Operations>[typeof tableName];
398
+ }
399
+
400
+ // Get type helpers from context (populated by createRuntimeContext)
401
+ const types = (context.types ?? {}) as ExtractSchemaTypes<Contract>;
402
+
403
+ return Object.freeze({ tables, types }) as SchemaReturnType<Contract>;
404
+ }
405
+
406
+ export type { ColumnBuilderImpl as Column, TableBuilderImpl as Table };