@prisma-next/adapter-postgres 0.3.0-dev.4 → 0.3.0-dev.40

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 (75) hide show
  1. package/README.md +64 -2
  2. package/dist/adapter-DB1CK2jM.mjs +265 -0
  3. package/dist/adapter-DB1CK2jM.mjs.map +1 -0
  4. package/dist/adapter.d.mts +23 -0
  5. package/dist/adapter.d.mts.map +1 -0
  6. package/dist/adapter.mjs +3 -0
  7. package/dist/codec-ids-Bsm9c7ns.mjs +29 -0
  8. package/dist/codec-ids-Bsm9c7ns.mjs.map +1 -0
  9. package/dist/codec-types.d.mts +141 -0
  10. package/dist/codec-types.d.mts.map +1 -0
  11. package/dist/codec-types.mjs +3 -0
  12. package/dist/codecs-DcC1nPzh.mjs +206 -0
  13. package/dist/codecs-DcC1nPzh.mjs.map +1 -0
  14. package/dist/column-types.d.mts +110 -0
  15. package/dist/column-types.d.mts.map +1 -0
  16. package/dist/column-types.mjs +180 -0
  17. package/dist/column-types.mjs.map +1 -0
  18. package/dist/control.d.mts +111 -0
  19. package/dist/control.d.mts.map +1 -0
  20. package/dist/control.mjs +405 -0
  21. package/dist/control.mjs.map +1 -0
  22. package/dist/descriptor-meta-D7pxo-wo.mjs +996 -0
  23. package/dist/descriptor-meta-D7pxo-wo.mjs.map +1 -0
  24. package/dist/runtime.d.mts +19 -0
  25. package/dist/runtime.d.mts.map +1 -0
  26. package/dist/runtime.mjs +85 -0
  27. package/dist/runtime.mjs.map +1 -0
  28. package/dist/types-BY395pUv.d.mts +19 -0
  29. package/dist/types-BY395pUv.d.mts.map +1 -0
  30. package/dist/types.d.mts +2 -0
  31. package/dist/types.mjs +1 -0
  32. package/package.json +39 -47
  33. package/src/core/adapter.ts +513 -0
  34. package/src/core/codec-ids.ts +28 -0
  35. package/src/core/codecs.ts +491 -0
  36. package/src/core/control-adapter.ts +536 -0
  37. package/src/core/default-normalizer.ts +77 -0
  38. package/src/core/descriptor-meta.ts +253 -0
  39. package/src/core/enum-control-hooks.ts +735 -0
  40. package/src/core/json-schema-type-expression.ts +131 -0
  41. package/src/core/json-schema-validator.ts +53 -0
  42. package/src/core/parameterized-types.ts +118 -0
  43. package/src/core/sql-utils.ts +111 -0
  44. package/src/core/standard-schema.ts +71 -0
  45. package/src/core/types.ts +53 -0
  46. package/src/exports/adapter.ts +1 -0
  47. package/src/exports/codec-types.ts +83 -0
  48. package/src/exports/column-types.ts +277 -0
  49. package/src/exports/control.ts +27 -0
  50. package/src/exports/runtime.ts +75 -0
  51. package/src/exports/types.ts +14 -0
  52. package/dist/exports/adapter.d.ts +0 -21
  53. package/dist/exports/adapter.js +0 -8
  54. package/dist/exports/adapter.js.map +0 -1
  55. package/dist/exports/chunk-B5SU5BVC.js +0 -47
  56. package/dist/exports/chunk-B5SU5BVC.js.map +0 -1
  57. package/dist/exports/chunk-CPAKRHXM.js +0 -162
  58. package/dist/exports/chunk-CPAKRHXM.js.map +0 -1
  59. package/dist/exports/chunk-ZHJOVBWT.js +0 -301
  60. package/dist/exports/chunk-ZHJOVBWT.js.map +0 -1
  61. package/dist/exports/codec-types.d.ts +0 -38
  62. package/dist/exports/codec-types.js +0 -7
  63. package/dist/exports/codec-types.js.map +0 -1
  64. package/dist/exports/column-types.d.ts +0 -20
  65. package/dist/exports/column-types.js +0 -49
  66. package/dist/exports/column-types.js.map +0 -1
  67. package/dist/exports/control.d.ts +0 -9
  68. package/dist/exports/control.js +0 -279
  69. package/dist/exports/control.js.map +0 -1
  70. package/dist/exports/runtime.d.ts +0 -17
  71. package/dist/exports/runtime.js +0 -20
  72. package/dist/exports/runtime.js.map +0 -1
  73. package/dist/exports/types.d.ts +0 -19
  74. package/dist/exports/types.js +0 -1
  75. package/dist/exports/types.js.map +0 -1
@@ -0,0 +1,491 @@
1
+ /**
2
+ * Unified codec definitions for Postgres adapter.
3
+ *
4
+ * This file contains a single source of truth for all codec information:
5
+ * - Scalar names
6
+ * - Type IDs
7
+ * - Codec implementations (runtime)
8
+ * - Type information (compile-time)
9
+ *
10
+ * This structure is used both at runtime (to populate the registry) and
11
+ * at compile time (to derive CodecTypes).
12
+ */
13
+
14
+ import type { Codec, CodecMeta } from '@prisma-next/sql-relational-core/ast';
15
+ import { codec, defineCodecs, sqlCodecDefinitions } from '@prisma-next/sql-relational-core/ast';
16
+ import { ifDefined } from '@prisma-next/utils/defined';
17
+ import { type as arktype } from 'arktype';
18
+ import {
19
+ PG_BIT_CODEC_ID,
20
+ PG_BOOL_CODEC_ID,
21
+ PG_CHAR_CODEC_ID,
22
+ PG_ENUM_CODEC_ID,
23
+ PG_FLOAT_CODEC_ID,
24
+ PG_FLOAT4_CODEC_ID,
25
+ PG_FLOAT8_CODEC_ID,
26
+ PG_INT_CODEC_ID,
27
+ PG_INT2_CODEC_ID,
28
+ PG_INT4_CODEC_ID,
29
+ PG_INT8_CODEC_ID,
30
+ PG_INTERVAL_CODEC_ID,
31
+ PG_JSON_CODEC_ID,
32
+ PG_JSONB_CODEC_ID,
33
+ PG_NUMERIC_CODEC_ID,
34
+ PG_TEXT_CODEC_ID,
35
+ PG_TIME_CODEC_ID,
36
+ PG_TIMESTAMP_CODEC_ID,
37
+ PG_TIMESTAMPTZ_CODEC_ID,
38
+ PG_TIMETZ_CODEC_ID,
39
+ PG_VARBIT_CODEC_ID,
40
+ PG_VARCHAR_CODEC_ID,
41
+ } from './codec-ids';
42
+
43
+ const lengthParamsSchema = arktype({
44
+ length: 'number.integer > 0',
45
+ });
46
+
47
+ const numericParamsSchema = arktype({
48
+ precision: 'number.integer > 0 & number.integer <= 1000',
49
+ 'scale?': 'number.integer >= 0',
50
+ });
51
+
52
+ const precisionParamsSchema = arktype({
53
+ 'precision?': 'number.integer >= 0 & number.integer <= 6',
54
+ });
55
+
56
+ function aliasCodec<
57
+ Id extends string,
58
+ TWire,
59
+ TJs,
60
+ TParams = Record<string, unknown>,
61
+ THelper = unknown,
62
+ >(
63
+ base: Codec<string, TWire, TJs, TParams, THelper>,
64
+ options: {
65
+ readonly typeId: Id;
66
+ readonly targetTypes: readonly string[];
67
+ readonly meta?: CodecMeta;
68
+ },
69
+ ): Codec<Id, TWire, TJs, TParams, THelper> {
70
+ return {
71
+ id: options.typeId,
72
+ targetTypes: options.targetTypes,
73
+ ...ifDefined('meta', options.meta),
74
+ ...ifDefined('paramsSchema', base.paramsSchema),
75
+ ...ifDefined('init', base.init),
76
+ ...ifDefined('encode', base.encode),
77
+ decode: base.decode,
78
+ };
79
+ }
80
+
81
+ const sqlCharCodec = sqlCodecDefinitions.char.codec;
82
+ const sqlVarcharCodec = sqlCodecDefinitions.varchar.codec;
83
+ const sqlIntCodec = sqlCodecDefinitions.int.codec;
84
+ const sqlFloatCodec = sqlCodecDefinitions.float.codec;
85
+
86
+ export type JsonValue =
87
+ | string
88
+ | number
89
+ | boolean
90
+ | null
91
+ | { readonly [key: string]: JsonValue }
92
+ | readonly JsonValue[];
93
+
94
+ // Create individual codec instances
95
+ const pgTextCodec = codec({
96
+ typeId: PG_TEXT_CODEC_ID,
97
+ targetTypes: ['text'],
98
+ encode: (value: string): string => value,
99
+ decode: (wire: string): string => wire,
100
+ meta: {
101
+ db: {
102
+ sql: {
103
+ postgres: {
104
+ nativeType: 'text',
105
+ },
106
+ },
107
+ },
108
+ },
109
+ });
110
+
111
+ const pgCharCodec = aliasCodec(sqlCharCodec, {
112
+ typeId: PG_CHAR_CODEC_ID,
113
+ targetTypes: ['character'],
114
+ meta: {
115
+ db: {
116
+ sql: {
117
+ postgres: {
118
+ nativeType: 'character',
119
+ },
120
+ },
121
+ },
122
+ },
123
+ });
124
+
125
+ const pgVarcharCodec = aliasCodec(sqlVarcharCodec, {
126
+ typeId: PG_VARCHAR_CODEC_ID,
127
+ targetTypes: ['character varying'],
128
+ meta: {
129
+ db: {
130
+ sql: {
131
+ postgres: {
132
+ nativeType: 'character varying',
133
+ },
134
+ },
135
+ },
136
+ },
137
+ });
138
+
139
+ const pgIntCodec = aliasCodec(sqlIntCodec, {
140
+ typeId: PG_INT_CODEC_ID,
141
+ targetTypes: ['int4'],
142
+ meta: {
143
+ db: {
144
+ sql: {
145
+ postgres: {
146
+ nativeType: 'integer',
147
+ },
148
+ },
149
+ },
150
+ },
151
+ });
152
+
153
+ const pgFloatCodec = aliasCodec(sqlFloatCodec, {
154
+ typeId: PG_FLOAT_CODEC_ID,
155
+ targetTypes: ['float8'],
156
+ meta: {
157
+ db: {
158
+ sql: {
159
+ postgres: {
160
+ nativeType: 'double precision',
161
+ },
162
+ },
163
+ },
164
+ },
165
+ });
166
+
167
+ const pgInt4Codec = codec<typeof PG_INT4_CODEC_ID, number, number>({
168
+ typeId: PG_INT4_CODEC_ID,
169
+ targetTypes: ['int4'],
170
+ encode: (value) => value,
171
+ decode: (wire) => wire,
172
+ meta: {
173
+ db: {
174
+ sql: {
175
+ postgres: {
176
+ nativeType: 'integer',
177
+ },
178
+ },
179
+ },
180
+ },
181
+ });
182
+
183
+ const pgNumericCodec = codec<typeof PG_NUMERIC_CODEC_ID, string, string>({
184
+ typeId: PG_NUMERIC_CODEC_ID,
185
+ targetTypes: ['numeric', 'decimal'],
186
+ encode: (value: string): string => value,
187
+ decode: (wire: string | number): string => {
188
+ if (typeof wire === 'number') return String(wire);
189
+ return wire;
190
+ },
191
+ paramsSchema: numericParamsSchema,
192
+ meta: {
193
+ db: {
194
+ sql: {
195
+ postgres: {
196
+ nativeType: 'numeric',
197
+ },
198
+ },
199
+ },
200
+ },
201
+ });
202
+
203
+ const pgInt2Codec = codec<typeof PG_INT2_CODEC_ID, number, number>({
204
+ typeId: PG_INT2_CODEC_ID,
205
+ targetTypes: ['int2'],
206
+ encode: (value) => value,
207
+ decode: (wire) => wire,
208
+ meta: {
209
+ db: {
210
+ sql: {
211
+ postgres: {
212
+ nativeType: 'smallint',
213
+ },
214
+ },
215
+ },
216
+ },
217
+ });
218
+
219
+ const pgInt8Codec = codec<typeof PG_INT8_CODEC_ID, number, number>({
220
+ typeId: PG_INT8_CODEC_ID,
221
+ targetTypes: ['int8'],
222
+ encode: (value) => value,
223
+ decode: (wire) => wire,
224
+ meta: {
225
+ db: {
226
+ sql: {
227
+ postgres: {
228
+ nativeType: 'bigint',
229
+ },
230
+ },
231
+ },
232
+ },
233
+ });
234
+
235
+ const pgFloat4Codec = codec<typeof PG_FLOAT4_CODEC_ID, number, number>({
236
+ typeId: PG_FLOAT4_CODEC_ID,
237
+ targetTypes: ['float4'],
238
+ encode: (value) => value,
239
+ decode: (wire) => wire,
240
+ meta: {
241
+ db: {
242
+ sql: {
243
+ postgres: {
244
+ nativeType: 'real',
245
+ },
246
+ },
247
+ },
248
+ },
249
+ });
250
+
251
+ const pgFloat8Codec = codec<typeof PG_FLOAT8_CODEC_ID, number, number>({
252
+ typeId: PG_FLOAT8_CODEC_ID,
253
+ targetTypes: ['float8'],
254
+ encode: (value) => value,
255
+ decode: (wire) => wire,
256
+ meta: {
257
+ db: {
258
+ sql: {
259
+ postgres: {
260
+ nativeType: 'double precision',
261
+ },
262
+ },
263
+ },
264
+ },
265
+ });
266
+
267
+ const pgTimestampCodec = codec<typeof PG_TIMESTAMP_CODEC_ID, string | Date, string>({
268
+ typeId: PG_TIMESTAMP_CODEC_ID,
269
+ targetTypes: ['timestamp'],
270
+ encode: (value: string | Date): string => {
271
+ if (value instanceof Date) return value.toISOString();
272
+ if (typeof value === 'string') return value;
273
+ return String(value);
274
+ },
275
+ decode: (wire: string | Date): string => {
276
+ if (typeof wire === 'string') return wire;
277
+ if (wire instanceof Date) return wire.toISOString();
278
+ return String(wire);
279
+ },
280
+ paramsSchema: precisionParamsSchema,
281
+ meta: {
282
+ db: {
283
+ sql: {
284
+ postgres: {
285
+ nativeType: 'timestamp without time zone',
286
+ },
287
+ },
288
+ },
289
+ },
290
+ });
291
+
292
+ const pgTimestamptzCodec = codec<typeof PG_TIMESTAMPTZ_CODEC_ID, string | Date, string>({
293
+ typeId: PG_TIMESTAMPTZ_CODEC_ID,
294
+ targetTypes: ['timestamptz'],
295
+ encode: (value: string | Date): string => {
296
+ if (value instanceof Date) return value.toISOString();
297
+ if (typeof value === 'string') return value;
298
+ return String(value);
299
+ },
300
+ decode: (wire: string | Date): string => {
301
+ if (typeof wire === 'string') return wire;
302
+ if (wire instanceof Date) return wire.toISOString();
303
+ return String(wire);
304
+ },
305
+ paramsSchema: precisionParamsSchema,
306
+ meta: {
307
+ db: {
308
+ sql: {
309
+ postgres: {
310
+ nativeType: 'timestamp with time zone',
311
+ },
312
+ },
313
+ },
314
+ },
315
+ });
316
+
317
+ const pgTimeCodec = codec<typeof PG_TIME_CODEC_ID, string, string>({
318
+ typeId: PG_TIME_CODEC_ID,
319
+ targetTypes: ['time'],
320
+ encode: (value: string): string => value,
321
+ decode: (wire: string): string => wire,
322
+ paramsSchema: precisionParamsSchema,
323
+ meta: {
324
+ db: {
325
+ sql: {
326
+ postgres: {
327
+ nativeType: 'time',
328
+ },
329
+ },
330
+ },
331
+ },
332
+ });
333
+
334
+ const pgTimetzCodec = codec<typeof PG_TIMETZ_CODEC_ID, string, string>({
335
+ typeId: PG_TIMETZ_CODEC_ID,
336
+ targetTypes: ['timetz'],
337
+ encode: (value: string): string => value,
338
+ decode: (wire: string): string => wire,
339
+ paramsSchema: precisionParamsSchema,
340
+ meta: {
341
+ db: {
342
+ sql: {
343
+ postgres: {
344
+ nativeType: 'timetz',
345
+ },
346
+ },
347
+ },
348
+ },
349
+ });
350
+
351
+ const pgBoolCodec = codec<typeof PG_BOOL_CODEC_ID, boolean, boolean>({
352
+ typeId: PG_BOOL_CODEC_ID,
353
+ targetTypes: ['bool'],
354
+ encode: (value) => value,
355
+ decode: (wire) => wire,
356
+ meta: {
357
+ db: {
358
+ sql: {
359
+ postgres: {
360
+ nativeType: 'boolean',
361
+ },
362
+ },
363
+ },
364
+ },
365
+ });
366
+
367
+ const pgBitCodec = codec<typeof PG_BIT_CODEC_ID, string, string>({
368
+ typeId: PG_BIT_CODEC_ID,
369
+ targetTypes: ['bit'],
370
+ encode: (value: string): string => value,
371
+ decode: (wire: string): string => wire,
372
+ paramsSchema: lengthParamsSchema,
373
+ meta: {
374
+ db: {
375
+ sql: {
376
+ postgres: {
377
+ nativeType: 'bit',
378
+ },
379
+ },
380
+ },
381
+ },
382
+ });
383
+
384
+ const pgVarbitCodec = codec<typeof PG_VARBIT_CODEC_ID, string, string>({
385
+ typeId: PG_VARBIT_CODEC_ID,
386
+ targetTypes: ['bit varying'],
387
+ encode: (value: string): string => value,
388
+ decode: (wire: string): string => wire,
389
+ paramsSchema: lengthParamsSchema,
390
+ meta: {
391
+ db: {
392
+ sql: {
393
+ postgres: {
394
+ nativeType: 'bit varying',
395
+ },
396
+ },
397
+ },
398
+ },
399
+ });
400
+
401
+ const pgEnumCodec = codec<typeof PG_ENUM_CODEC_ID, string, string>({
402
+ typeId: PG_ENUM_CODEC_ID,
403
+ targetTypes: ['enum'],
404
+ encode: (value) => value,
405
+ decode: (wire) => wire,
406
+ });
407
+
408
+ const pgIntervalCodec = codec<typeof PG_INTERVAL_CODEC_ID, string, string>({
409
+ typeId: PG_INTERVAL_CODEC_ID,
410
+ targetTypes: ['interval'],
411
+ encode: (value: string): string => value,
412
+ decode: (wire: string): string => wire,
413
+ paramsSchema: precisionParamsSchema,
414
+ meta: {
415
+ db: {
416
+ sql: {
417
+ postgres: {
418
+ nativeType: 'interval',
419
+ },
420
+ },
421
+ },
422
+ },
423
+ });
424
+
425
+ const pgJsonCodec = codec<typeof PG_JSON_CODEC_ID, string | JsonValue, JsonValue>({
426
+ typeId: PG_JSON_CODEC_ID,
427
+ targetTypes: ['json'],
428
+ encode: (value) => JSON.stringify(value),
429
+ decode: (wire) => (typeof wire === 'string' ? JSON.parse(wire) : wire),
430
+ meta: {
431
+ db: {
432
+ sql: {
433
+ postgres: {
434
+ nativeType: 'json',
435
+ },
436
+ },
437
+ },
438
+ },
439
+ });
440
+
441
+ const pgJsonbCodec = codec<typeof PG_JSONB_CODEC_ID, string | JsonValue, JsonValue>({
442
+ typeId: PG_JSONB_CODEC_ID,
443
+ targetTypes: ['jsonb'],
444
+ encode: (value) => JSON.stringify(value),
445
+ decode: (wire) => (typeof wire === 'string' ? JSON.parse(wire) : wire),
446
+ meta: {
447
+ db: {
448
+ sql: {
449
+ postgres: {
450
+ nativeType: 'jsonb',
451
+ },
452
+ },
453
+ },
454
+ },
455
+ });
456
+
457
+ // Build codec definitions using the builder DSL
458
+ const codecs = defineCodecs()
459
+ .add('char', sqlCharCodec)
460
+ .add('varchar', sqlVarcharCodec)
461
+ .add('int', sqlIntCodec)
462
+ .add('float', sqlFloatCodec)
463
+ .add('text', pgTextCodec)
464
+ .add('character', pgCharCodec)
465
+ .add('character varying', pgVarcharCodec)
466
+ .add('integer', pgIntCodec)
467
+ .add('double precision', pgFloatCodec)
468
+ .add('int4', pgInt4Codec)
469
+ .add('int2', pgInt2Codec)
470
+ .add('int8', pgInt8Codec)
471
+ .add('float4', pgFloat4Codec)
472
+ .add('float8', pgFloat8Codec)
473
+ .add('numeric', pgNumericCodec)
474
+ .add('timestamp', pgTimestampCodec)
475
+ .add('timestamptz', pgTimestamptzCodec)
476
+ .add('time', pgTimeCodec)
477
+ .add('timetz', pgTimetzCodec)
478
+ .add('bool', pgBoolCodec)
479
+ .add('bit', pgBitCodec)
480
+ .add('bit varying', pgVarbitCodec)
481
+ .add('interval', pgIntervalCodec)
482
+ .add('enum', pgEnumCodec)
483
+ .add('json', pgJsonCodec)
484
+ .add('jsonb', pgJsonbCodec);
485
+
486
+ // Export derived structures directly from codecs builder
487
+ export const codecDefinitions = codecs.codecDefinitions;
488
+ export const dataTypes = codecs.dataTypes;
489
+
490
+ // Export types derived from codecs builder
491
+ export type CodecTypes = typeof codecs.CodecTypes;