@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
@@ -0,0 +1,444 @@
1
+ import { ifDefined } from '@prisma-next/utils/defined';
2
+ import type { Type } from 'arktype';
3
+ import type { O } from 'ts-toolbelt';
4
+
5
+ /**
6
+ * Descriptor for parameterized codecs that require type parameter validation.
7
+ * Shared between adapter (compile-time) and runtime layers to avoid duplication.
8
+ *
9
+ * @template TParams - The shape of the type parameters (e.g., `{ length: number }`)
10
+ * @template THelper - The type returned by the optional `init` hook
11
+ */
12
+ export interface CodecParamsDescriptor<TParams = Record<string, unknown>, THelper = unknown> {
13
+ /** The codec ID this descriptor applies to (e.g., 'pg/vector@1') */
14
+ readonly codecId: string;
15
+
16
+ /**
17
+ * Arktype schema for validating typeParams.
18
+ * Used to validate both storage.types entries and inline column typeParams.
19
+ */
20
+ readonly paramsSchema: Type<TParams>;
21
+
22
+ /**
23
+ * Optional init hook called during runtime context creation.
24
+ * Receives validated params and returns a helper object to be stored in context.types.
25
+ * If not provided, the validated params are stored directly.
26
+ */
27
+ readonly init?: (params: TParams) => THelper;
28
+ }
29
+
30
+ /**
31
+ * Codec metadata for database-specific type information.
32
+ * Used for schema introspection and verification.
33
+ */
34
+ export interface CodecMeta {
35
+ readonly db?: {
36
+ readonly sql?: {
37
+ readonly postgres?: {
38
+ readonly nativeType: string; // e.g. 'integer', 'text', 'vector', 'timestamp with time zone'
39
+ };
40
+ };
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Codec interface for encoding/decoding values between wire format and JavaScript types.
46
+ *
47
+ * Codecs are pure, synchronous functions with no side effects or IO.
48
+ * They provide deterministic conversion between database wire types and JS values.
49
+ */
50
+ export interface Codec<
51
+ Id extends string = string,
52
+ TWire = unknown,
53
+ TJs = unknown,
54
+ TParams = Record<string, unknown>,
55
+ THelper = unknown,
56
+ > {
57
+ /**
58
+ * Namespaced codec identifier in format 'namespace/name@version'
59
+ * Examples: 'pg/text@1', 'pg/uuid@1', 'pg/timestamptz@1'
60
+ */
61
+ readonly id: Id;
62
+
63
+ /**
64
+ * Contract scalar type IDs that this codec can handle.
65
+ * Examples: ['text'], ['int4', 'float8'], ['timestamp', 'timestamptz']
66
+ */
67
+ readonly targetTypes: readonly string[];
68
+
69
+ /**
70
+ * Optional metadata for database-specific type information.
71
+ * Used for schema introspection and verification.
72
+ */
73
+ readonly meta?: CodecMeta;
74
+
75
+ /**
76
+ * Optional params schema for parameterized codecs.
77
+ * If provided, typeParams are validated against this schema.
78
+ */
79
+ readonly paramsSchema?: Type<TParams>;
80
+
81
+ /**
82
+ * Optional init hook for building runtime helper state from validated params.
83
+ *
84
+ * Useful when parameterized types need derived data at runtime, for example:
85
+ * - normalize typeParams into a stable helper shape consumed by lanes/adapters
86
+ * - precompute reusable values once during context creation
87
+ * - avoid repeating typeParams parsing logic during query execution
88
+ *
89
+ * Example:
90
+ * { length: 255 } -> { kind: 'variable', maxLength: 255 }
91
+ *
92
+ * **Convention for JSON/JSONB codecs**: When the helper includes a `validate`
93
+ * property of type `JsonSchemaValidateFn`, the runtime will use it to enforce
94
+ * JSON Schema conformance during encoding and decoding. The property is
95
+ * discovered via duck typing (`helper?.validate`) for flexibility across
96
+ * different codec types.
97
+ */
98
+ readonly init?: (params: TParams) => THelper;
99
+
100
+ /**
101
+ * Decode a wire value (from database) to JavaScript type.
102
+ * Must be synchronous and pure (no side effects).
103
+ */
104
+ decode(wire: TWire): TJs;
105
+
106
+ /**
107
+ * Encode a JavaScript value to wire format (for database).
108
+ * Optional - if not provided, values pass through unchanged.
109
+ * Must be synchronous and pure (no side effects).
110
+ */
111
+ encode?(value: TJs): TWire;
112
+ }
113
+
114
+ /**
115
+ * Registry interface for codecs organized by ID and by contract scalar type.
116
+ *
117
+ * The registry allows looking up codecs by their namespaced ID or by the
118
+ * contract scalar types they handle. Multiple codecs may handle the same
119
+ * scalar type; ordering in byScalar reflects preference (adapter first,
120
+ * then packs, then app overrides).
121
+ */
122
+ export interface CodecRegistry {
123
+ get(id: string): Codec<string> | undefined;
124
+ has(id: string): boolean;
125
+ getByScalar(scalar: string): readonly Codec<string>[];
126
+ getDefaultCodec(scalar: string): Codec<string> | undefined;
127
+ register(codec: Codec<string>): void;
128
+ [Symbol.iterator](): Iterator<Codec<string>>;
129
+ values(): IterableIterator<Codec<string>>;
130
+ }
131
+
132
+ /**
133
+ * Implementation of CodecRegistry.
134
+ */
135
+ class CodecRegistryImpl implements CodecRegistry {
136
+ private readonly _byId = new Map<string, Codec<string>>();
137
+ private readonly _byScalar = new Map<string, Codec<string>[]>();
138
+
139
+ /**
140
+ * Map-like interface for codec lookup by ID.
141
+ * Example: registry.get('pg/text@1')
142
+ */
143
+ get(id: string): Codec<string> | undefined {
144
+ return this._byId.get(id);
145
+ }
146
+
147
+ /**
148
+ * Check if a codec with the given ID is registered.
149
+ */
150
+ has(id: string): boolean {
151
+ return this._byId.has(id);
152
+ }
153
+
154
+ /**
155
+ * Get all codecs that handle a given scalar type.
156
+ * Returns an empty frozen array if no codecs are found.
157
+ * Example: registry.getByScalar('text') → [codec1, codec2, ...]
158
+ */
159
+ getByScalar(scalar: string): readonly Codec<string>[] {
160
+ return this._byScalar.get(scalar) ?? Object.freeze([]);
161
+ }
162
+
163
+ /**
164
+ * Get the default codec for a scalar type (first registered codec).
165
+ * Returns undefined if no codec handles this scalar type.
166
+ */
167
+ getDefaultCodec(scalar: string): Codec<string> | undefined {
168
+ const _codecs = this._byScalar.get(scalar);
169
+ return _codecs?.[0];
170
+ }
171
+
172
+ /**
173
+ * Register a codec in the registry.
174
+ * Throws an error if a codec with the same ID is already registered.
175
+ *
176
+ * @param codec - The codec to register
177
+ * @throws Error if a codec with the same ID already exists
178
+ */
179
+ register(codec: Codec<string>): void {
180
+ if (this._byId.has(codec.id)) {
181
+ throw new Error(`Codec with ID '${codec.id}' is already registered`);
182
+ }
183
+
184
+ this._byId.set(codec.id, codec);
185
+
186
+ // Update byScalar mapping
187
+ for (const scalarType of codec.targetTypes) {
188
+ const existing = this._byScalar.get(scalarType);
189
+ if (existing) {
190
+ existing.push(codec);
191
+ } else {
192
+ this._byScalar.set(scalarType, [codec]);
193
+ }
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Returns an iterator over all registered codecs.
199
+ * Useful for iterating through codecs from another registry.
200
+ */
201
+ *[Symbol.iterator](): Iterator<Codec<string>> {
202
+ for (const codec of this._byId.values()) {
203
+ yield codec;
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Returns an iterable of all registered codecs.
209
+ */
210
+ values(): IterableIterator<Codec<string>> {
211
+ return this._byId.values();
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Codec factory - creates a codec with typeId and encode/decode functions.
217
+ */
218
+ export function codec<
219
+ Id extends string,
220
+ TWire,
221
+ TJs,
222
+ TParams = Record<string, unknown>,
223
+ THelper = unknown,
224
+ >(config: {
225
+ typeId: Id;
226
+ targetTypes: readonly string[];
227
+ encode: (value: TJs) => TWire;
228
+ decode: (wire: TWire) => TJs;
229
+ meta?: CodecMeta;
230
+ paramsSchema?: Type<TParams>;
231
+ init?: (params: TParams) => THelper;
232
+ }): Codec<Id, TWire, TJs, TParams, THelper> {
233
+ return {
234
+ id: config.typeId,
235
+ targetTypes: config.targetTypes,
236
+ ...ifDefined('meta', config.meta),
237
+ ...ifDefined('paramsSchema', config.paramsSchema),
238
+ ...ifDefined('init', config.init),
239
+ encode: config.encode,
240
+ decode: config.decode,
241
+ };
242
+ }
243
+
244
+ /**
245
+ * Type helpers to extract codec types.
246
+ */
247
+ export type CodecId<T> =
248
+ T extends Codec<infer Id, unknown, unknown>
249
+ ? Id
250
+ : T extends { readonly id: infer Id }
251
+ ? Id
252
+ : never;
253
+ export type CodecInput<T> = T extends Codec<string, unknown, infer JsT> ? JsT : never;
254
+ export type CodecOutput<T> = T extends Codec<string, unknown, infer JsT> ? JsT : never;
255
+
256
+ /**
257
+ * Type helper to extract codec types from builder instance.
258
+ */
259
+ export type ExtractCodecTypes<
260
+ ScalarNames extends { readonly [K in keyof ScalarNames]: Codec<string> } = Record<never, never>,
261
+ > = {
262
+ readonly [K in keyof ScalarNames as ScalarNames[K] extends Codec<infer Id, unknown, unknown>
263
+ ? Id
264
+ : never]: {
265
+ readonly input: CodecInput<ScalarNames[K]>;
266
+ readonly output: CodecOutput<ScalarNames[K]>;
267
+ };
268
+ };
269
+
270
+ /**
271
+ * Type helper to extract data type IDs from builder instance.
272
+ * Uses ExtractCodecTypes which preserves literal types as keys.
273
+ * Since ExtractCodecTypes<Record<K, ScalarNames[K]>> has exactly one key (the Id),
274
+ * we extract it by creating a mapped type that uses the Id as both key and value,
275
+ * then extract the value type. This preserves literal types.
276
+ */
277
+ export type ExtractDataTypes<
278
+ ScalarNames extends { readonly [K in keyof ScalarNames]: Codec<string> },
279
+ > = {
280
+ readonly [K in keyof ScalarNames]: {
281
+ readonly [Id in keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>]: Id;
282
+ }[keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>];
283
+ };
284
+
285
+ /**
286
+ * Builder interface for declaring codecs.
287
+ */
288
+ export interface CodecDefBuilder<
289
+ ScalarNames extends { readonly [K in keyof ScalarNames]: Codec<string> } = Record<never, never>,
290
+ > {
291
+ readonly CodecTypes: ExtractCodecTypes<ScalarNames>;
292
+
293
+ add<ScalarName extends string, CodecImpl extends Codec<string>>(
294
+ scalarName: ScalarName,
295
+ codecImpl: CodecImpl,
296
+ ): CodecDefBuilder<
297
+ O.Overwrite<ScalarNames, Record<ScalarName, CodecImpl>> & Record<ScalarName, CodecImpl>
298
+ >;
299
+
300
+ readonly codecDefinitions: {
301
+ readonly [K in keyof ScalarNames]: {
302
+ readonly typeId: ScalarNames[K] extends Codec<infer Id extends string, unknown, unknown>
303
+ ? Id
304
+ : never;
305
+ readonly scalar: K;
306
+ readonly codec: ScalarNames[K];
307
+ readonly input: CodecInput<ScalarNames[K]>;
308
+ readonly output: CodecOutput<ScalarNames[K]>;
309
+ readonly jsType: CodecOutput<ScalarNames[K]>;
310
+ };
311
+ };
312
+
313
+ readonly dataTypes: {
314
+ readonly [K in keyof ScalarNames]: {
315
+ readonly [Id in keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>]: Id;
316
+ }[keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>];
317
+ };
318
+ }
319
+
320
+ /**
321
+ * Implementation of CodecDefBuilder.
322
+ */
323
+ class CodecDefBuilderImpl<
324
+ ScalarNames extends { readonly [K in keyof ScalarNames]: Codec<string> } = Record<never, never>,
325
+ > implements CodecDefBuilder<ScalarNames>
326
+ {
327
+ private readonly _codecs: ScalarNames;
328
+
329
+ public readonly CodecTypes: ExtractCodecTypes<ScalarNames>;
330
+ public readonly dataTypes: {
331
+ readonly [K in keyof ScalarNames]: {
332
+ readonly [Id in keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>]: Id;
333
+ }[keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>];
334
+ };
335
+
336
+ constructor(codecs: ScalarNames) {
337
+ this._codecs = codecs;
338
+
339
+ // Populate CodecTypes from codecs
340
+ const codecTypes: Record<string, { readonly input: unknown; readonly output: unknown }> = {};
341
+ for (const [, codecImpl] of Object.entries(this._codecs)) {
342
+ const codecImplTyped = codecImpl as Codec<string>;
343
+ codecTypes[codecImplTyped.id] = {
344
+ input: undefined as unknown as CodecInput<typeof codecImplTyped>,
345
+ output: undefined as unknown as CodecOutput<typeof codecImplTyped>,
346
+ };
347
+ }
348
+ this.CodecTypes = codecTypes as ExtractCodecTypes<ScalarNames>;
349
+
350
+ // Populate dataTypes from codecs - extract id property from each codec
351
+ // Build object preserving keys from ScalarNames
352
+ // Type assertion is safe because we know ScalarNames structure matches the return type
353
+ // biome-ignore lint/suspicious/noExplicitAny: dynamic codec mapping requires any
354
+ const dataTypes = {} as any;
355
+ for (const key in this._codecs) {
356
+ if (Object.hasOwn(this._codecs, key)) {
357
+ const codec = this._codecs[key] as Codec<string>;
358
+ dataTypes[key] = codec.id;
359
+ }
360
+ }
361
+ this.dataTypes = dataTypes as {
362
+ readonly [K in keyof ScalarNames]: {
363
+ readonly [Id in keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>]: Id;
364
+ }[keyof ExtractCodecTypes<Record<K, ScalarNames[K]>>];
365
+ };
366
+ }
367
+
368
+ add<ScalarName extends string, CodecImpl extends Codec<string>>(
369
+ scalarName: ScalarName,
370
+ codecImpl: CodecImpl,
371
+ ): CodecDefBuilder<
372
+ O.Overwrite<ScalarNames, Record<ScalarName, CodecImpl>> & Record<ScalarName, CodecImpl>
373
+ > {
374
+ return new CodecDefBuilderImpl({
375
+ ...this._codecs,
376
+ [scalarName]: codecImpl,
377
+ } as O.Overwrite<ScalarNames, Record<ScalarName, CodecImpl>> & Record<ScalarName, CodecImpl>);
378
+ }
379
+
380
+ /**
381
+ * Derive codecDefinitions structure.
382
+ */
383
+ get codecDefinitions(): {
384
+ readonly [K in keyof ScalarNames]: {
385
+ readonly typeId: ScalarNames[K] extends Codec<infer Id, unknown, unknown> ? Id : never;
386
+ readonly scalar: K;
387
+ readonly codec: ScalarNames[K];
388
+ readonly input: CodecInput<ScalarNames[K]>;
389
+ readonly output: CodecOutput<ScalarNames[K]>;
390
+ readonly jsType: CodecOutput<ScalarNames[K]>;
391
+ };
392
+ } {
393
+ const result: Record<
394
+ string,
395
+ {
396
+ typeId: string;
397
+ scalar: string;
398
+ codec: Codec;
399
+ input: unknown;
400
+ output: unknown;
401
+ jsType: unknown;
402
+ }
403
+ > = {};
404
+
405
+ for (const [scalarName, codecImpl] of Object.entries(this._codecs)) {
406
+ const codec = codecImpl as Codec<string>;
407
+ result[scalarName] = {
408
+ typeId: codec.id,
409
+ scalar: scalarName,
410
+ codec: codec,
411
+ input: undefined as unknown as CodecInput<typeof codec>,
412
+ output: undefined as unknown as CodecOutput<typeof codec>,
413
+ jsType: undefined as unknown as CodecOutput<typeof codec>,
414
+ };
415
+ }
416
+
417
+ return result as {
418
+ readonly [K in keyof ScalarNames]: {
419
+ readonly typeId: ScalarNames[K] extends Codec<infer Id extends string, unknown, unknown>
420
+ ? Id
421
+ : never;
422
+ readonly scalar: K;
423
+ readonly codec: ScalarNames[K];
424
+ readonly input: CodecInput<ScalarNames[K]>;
425
+ readonly output: CodecOutput<ScalarNames[K]>;
426
+ readonly jsType: CodecOutput<ScalarNames[K]>;
427
+ };
428
+ };
429
+ }
430
+ }
431
+
432
+ /**
433
+ * Create a new codec registry.
434
+ */
435
+ export function createCodecRegistry(): CodecRegistry {
436
+ return new CodecRegistryImpl();
437
+ }
438
+
439
+ /**
440
+ * Create a new codec definition builder.
441
+ */
442
+ export function defineCodecs(): CodecDefBuilder<Record<never, never>> {
443
+ return new CodecDefBuilderImpl({});
444
+ }
@@ -0,0 +1,36 @@
1
+ import type { ColumnRef, LiteralExpr, OperationExpr, ParamRef, TableRef } from './types';
2
+ import { compact } from './util';
3
+
4
+ export function createTableRef(name: string): TableRef {
5
+ return {
6
+ kind: 'table',
7
+ name,
8
+ };
9
+ }
10
+
11
+ export function createColumnRef(table: string, column: string): ColumnRef {
12
+ return {
13
+ kind: 'col',
14
+ table,
15
+ column,
16
+ };
17
+ }
18
+
19
+ export function createParamRef(index: number, name?: string): ParamRef {
20
+ return compact({
21
+ kind: 'param',
22
+ index,
23
+ name,
24
+ }) as ParamRef;
25
+ }
26
+
27
+ export function createOperationExpr(operation: OperationExpr): OperationExpr {
28
+ return operation;
29
+ }
30
+
31
+ export function createLiteralExpr(value: unknown): LiteralExpr {
32
+ return {
33
+ kind: 'literal',
34
+ value,
35
+ };
36
+ }
@@ -0,0 +1,17 @@
1
+ import type { ColumnRef, DeleteAst, TableRef, WhereExpr } from './types';
2
+ import { compact } from './util';
3
+
4
+ export interface CreateDeleteAstOptions {
5
+ readonly table: TableRef;
6
+ readonly where: WhereExpr;
7
+ readonly returning?: ReadonlyArray<ColumnRef>;
8
+ }
9
+
10
+ export function createDeleteAst(options: CreateDeleteAstOptions): DeleteAst {
11
+ return compact({
12
+ kind: 'delete',
13
+ table: options.table,
14
+ where: options.where,
15
+ returning: options.returning,
16
+ }) as DeleteAst;
17
+ }
@@ -0,0 +1,42 @@
1
+ export interface SqlExecuteRequest {
2
+ readonly sql: string;
3
+ readonly params?: readonly unknown[];
4
+ }
5
+
6
+ export interface SqlQueryResult<Row = Record<string, unknown>> {
7
+ readonly rows: ReadonlyArray<Row>;
8
+ readonly rowCount?: number | null;
9
+ readonly [key: string]: unknown;
10
+ }
11
+
12
+ export interface SqlExplainResult<Row = Record<string, unknown>> {
13
+ readonly rows: ReadonlyArray<Row>;
14
+ }
15
+
16
+ export type SqlDriverState = 'unbound' | 'connected' | 'closed';
17
+
18
+ export interface SqlDriver<TBinding = void> extends SqlQueryable {
19
+ readonly state?: SqlDriverState;
20
+ connect(binding: TBinding): Promise<void>;
21
+ acquireConnection(): Promise<SqlConnection>;
22
+ close(): Promise<void>;
23
+ }
24
+
25
+ export interface SqlConnection extends SqlQueryable {
26
+ beginTransaction(): Promise<SqlTransaction>;
27
+ release(): Promise<void>;
28
+ }
29
+
30
+ export interface SqlTransaction extends SqlQueryable {
31
+ commit(): Promise<void>;
32
+ rollback(): Promise<void>;
33
+ }
34
+
35
+ export interface SqlQueryable {
36
+ execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row>;
37
+ explain?(request: SqlExecuteRequest): Promise<SqlExplainResult>;
38
+ query<Row = Record<string, unknown>>(
39
+ sql: string,
40
+ params?: readonly unknown[],
41
+ ): Promise<SqlQueryResult<Row>>;
42
+ }
@@ -0,0 +1,17 @@
1
+ import type { ColumnRef, InsertAst, ParamRef, TableRef } from './types';
2
+ import { compact } from './util';
3
+
4
+ export interface CreateInsertAstOptions {
5
+ readonly table: TableRef;
6
+ readonly values: Record<string, ColumnRef | ParamRef>;
7
+ readonly returning?: ReadonlyArray<ColumnRef>;
8
+ }
9
+
10
+ export function createInsertAst(options: CreateInsertAstOptions): InsertAst {
11
+ return compact({
12
+ kind: 'insert',
13
+ table: options.table,
14
+ values: options.values,
15
+ returning: options.returning,
16
+ }) as InsertAst;
17
+ }
@@ -0,0 +1,54 @@
1
+ import { planInvalid } from '@prisma-next/plan';
2
+ import type { AnyColumnBuilder, JoinOnBuilder, JoinOnPredicate } from '../types';
3
+ import { isColumnBuilder } from '../types';
4
+ import type { ColumnRef, JoinAst, JoinOnExpr, TableRef } from './types';
5
+
6
+ export function createJoin(
7
+ joinType: 'inner' | 'left' | 'right' | 'full',
8
+ table: TableRef,
9
+ on: JoinOnExpr,
10
+ ): JoinAst {
11
+ return {
12
+ kind: 'join',
13
+ joinType,
14
+ table,
15
+ on,
16
+ };
17
+ }
18
+
19
+ export function createJoinOnExpr(left: ColumnRef, right: ColumnRef): JoinOnExpr {
20
+ return {
21
+ kind: 'eqCol',
22
+ left,
23
+ right,
24
+ };
25
+ }
26
+
27
+ class JoinOnBuilderImpl implements JoinOnBuilder {
28
+ eqCol(left: AnyColumnBuilder, right: AnyColumnBuilder): JoinOnPredicate {
29
+ if (!left || !isColumnBuilder(left)) {
30
+ throw planInvalid('Join ON left operand must be a column');
31
+ }
32
+
33
+ if (!right || !isColumnBuilder(right)) {
34
+ throw planInvalid('Join ON right operand must be a column');
35
+ }
36
+
37
+ // TypeScript can't narrow ColumnBuilder properly, so we assert
38
+ const leftCol = left as unknown as { table: string; column: string };
39
+ const rightCol = right as unknown as { table: string; column: string };
40
+ if (leftCol.table === rightCol.table) {
41
+ throw planInvalid('Self-joins are not supported in MVP');
42
+ }
43
+
44
+ return {
45
+ kind: 'join-on',
46
+ left: left as AnyColumnBuilder,
47
+ right: right as AnyColumnBuilder,
48
+ };
49
+ }
50
+ }
51
+
52
+ export function createJoinOnBuilder(): JoinOnBuilder {
53
+ return new JoinOnBuilderImpl();
54
+ }
@@ -0,0 +1,11 @@
1
+ import type { ColumnRef, Direction, OperationExpr } from './types';
2
+
3
+ export function createOrderByItem(
4
+ expr: ColumnRef | OperationExpr,
5
+ dir: 'asc' | 'desc',
6
+ ): { expr: ColumnRef | OperationExpr; dir: Direction } {
7
+ return {
8
+ expr,
9
+ dir,
10
+ };
11
+ }
@@ -0,0 +1,40 @@
1
+ import type {
2
+ BinaryExpr,
3
+ BinaryOp,
4
+ ExistsExpr,
5
+ Expression,
6
+ ListLiteralExpr,
7
+ LiteralExpr,
8
+ NullCheckExpr,
9
+ ParamRef,
10
+ SelectAst,
11
+ } from './types';
12
+
13
+ export function createBinaryExpr(
14
+ op: BinaryOp,
15
+ left: Expression,
16
+ right: Expression | ParamRef | LiteralExpr | ListLiteralExpr,
17
+ ): BinaryExpr {
18
+ return {
19
+ kind: 'bin',
20
+ op,
21
+ left,
22
+ right,
23
+ };
24
+ }
25
+
26
+ export function createExistsExpr(not: boolean, subquery: SelectAst): ExistsExpr {
27
+ return {
28
+ kind: 'exists',
29
+ not,
30
+ subquery,
31
+ };
32
+ }
33
+
34
+ export function createNullCheckExpr(expr: Expression, isNull: boolean): NullCheckExpr {
35
+ return {
36
+ kind: 'nullCheck',
37
+ expr,
38
+ isNull,
39
+ };
40
+ }
@@ -0,0 +1,38 @@
1
+ import type {
2
+ ColumnRef,
3
+ Direction,
4
+ IncludeAst,
5
+ IncludeRef,
6
+ JoinAst,
7
+ OperationExpr,
8
+ SelectAst,
9
+ TableRef,
10
+ WhereExpr,
11
+ } from './types';
12
+ import { compact } from './util';
13
+
14
+ export interface CreateSelectAstOptions {
15
+ readonly from: TableRef;
16
+ readonly joins?: ReadonlyArray<JoinAst>;
17
+ readonly includes?: ReadonlyArray<IncludeAst>;
18
+ readonly project: ReadonlyArray<{
19
+ alias: string;
20
+ expr: ColumnRef | IncludeRef | OperationExpr;
21
+ }>;
22
+ readonly where?: WhereExpr;
23
+ readonly orderBy?: ReadonlyArray<{ expr: ColumnRef | OperationExpr; dir: Direction }>;
24
+ readonly limit?: number;
25
+ }
26
+
27
+ export function createSelectAst(options: CreateSelectAstOptions): SelectAst {
28
+ return compact({
29
+ kind: 'select',
30
+ from: options.from,
31
+ joins: options.joins,
32
+ includes: options.includes,
33
+ project: options.project,
34
+ where: options.where,
35
+ orderBy: options.orderBy,
36
+ limit: options.limit,
37
+ }) as SelectAst;
38
+ }