@prisma-next/sql-runtime 0.5.0-dev.3 → 0.5.0-dev.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +29 -21
  2. package/dist/{exports-BQZSVXXt.mjs → exports-Cq_9ZrU4.mjs} +649 -275
  3. package/dist/exports-Cq_9ZrU4.mjs.map +1 -0
  4. package/dist/{index-yb51L_1h.d.mts → index-Df2GsLSH.d.mts} +65 -16
  5. package/dist/index-Df2GsLSH.d.mts.map +1 -0
  6. package/dist/index.d.mts +2 -2
  7. package/dist/index.mjs +2 -2
  8. package/dist/test/utils.d.mts +6 -5
  9. package/dist/test/utils.d.mts.map +1 -1
  10. package/dist/test/utils.mjs +11 -5
  11. package/dist/test/utils.mjs.map +1 -1
  12. package/package.json +10 -12
  13. package/src/codecs/decoding.ts +256 -173
  14. package/src/codecs/encoding.ts +123 -39
  15. package/src/exports/index.ts +11 -7
  16. package/src/fingerprint.ts +22 -0
  17. package/src/guardrails/raw.ts +165 -0
  18. package/src/lower-sql-plan.ts +3 -3
  19. package/src/marker.ts +75 -0
  20. package/src/middleware/before-compile-chain.ts +1 -0
  21. package/src/middleware/budgets.ts +26 -96
  22. package/src/middleware/lints.ts +3 -3
  23. package/src/middleware/sql-middleware.ts +6 -5
  24. package/src/runtime-spi.ts +44 -0
  25. package/src/sql-family-adapter.ts +3 -2
  26. package/src/sql-marker.ts +62 -47
  27. package/src/sql-runtime.ts +321 -111
  28. package/dist/exports-BQZSVXXt.mjs.map +0 -1
  29. package/dist/index-yb51L_1h.d.mts.map +0 -1
  30. package/test/async-iterable-result.test.ts +0 -141
  31. package/test/before-compile-chain.test.ts +0 -223
  32. package/test/budgets.test.ts +0 -431
  33. package/test/context.types.test-d.ts +0 -68
  34. package/test/execution-stack.test.ts +0 -161
  35. package/test/json-schema-validation.test.ts +0 -571
  36. package/test/lints.test.ts +0 -160
  37. package/test/mutation-default-generators.test.ts +0 -254
  38. package/test/parameterized-types.test.ts +0 -529
  39. package/test/sql-context.test.ts +0 -384
  40. package/test/sql-family-adapter.test.ts +0 -103
  41. package/test/sql-runtime.test.ts +0 -792
  42. package/test/utils.ts +0 -297
@@ -1,529 +0,0 @@
1
- import type { Contract } from '@prisma-next/contract/types';
2
- import { coreHash, profileHash } from '@prisma-next/contract/types';
3
- import type { SqlStorage, StorageTypeInstance } from '@prisma-next/sql-contract/types';
4
- import { codec, createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
5
- import { ifDefined } from '@prisma-next/utils/defined';
6
- import type { Type } from 'arktype';
7
- import { type as arktype } from 'arktype';
8
- import { describe, expect, it } from 'vitest';
9
- import type {
10
- RuntimeParameterizedCodecDescriptor,
11
- SqlRuntimeExtensionDescriptor,
12
- } from '../src/sql-context';
13
- import { createStubAdapter, createTestContext } from './utils';
14
-
15
- // =============================================================================
16
- // Test helpers
17
- // =============================================================================
18
-
19
- function createParamTypesTestContract(
20
- options?: Partial<{
21
- types: Record<string, StorageTypeInstance>;
22
- tableColumns: Record<
23
- string,
24
- {
25
- nativeType: string;
26
- codecId: string;
27
- nullable: boolean;
28
- typeParams?: Record<string, unknown>;
29
- typeRef?: string;
30
- }
31
- >;
32
- }>,
33
- ): Contract<SqlStorage> {
34
- return {
35
- targetFamily: 'sql',
36
- target: 'postgres',
37
- profileHash: profileHash('sha256:test'),
38
- models: {},
39
- roots: {},
40
- storage: {
41
- storageHash: coreHash('sha256:test'),
42
- tables: {
43
- test: {
44
- columns: options?.tableColumns ?? {
45
- id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
46
- },
47
- primaryKey: { columns: ['id'] },
48
- uniques: [],
49
- indexes: [],
50
- foreignKeys: [],
51
- },
52
- },
53
- ...ifDefined('types', options?.types),
54
- },
55
- extensionPacks: {},
56
- capabilities: {},
57
- meta: {},
58
- };
59
- }
60
-
61
- // =============================================================================
62
- // Tests: Parameterized type validation
63
- // =============================================================================
64
-
65
- describe('parameterized types', () => {
66
- describe('storage.types validation', () => {
67
- it('creates context with empty storage.types', () => {
68
- const contract = createParamTypesTestContract({ types: {} });
69
- const context = createTestContext(contract, createStubAdapter());
70
-
71
- expect(context.contract.storage.types).toEqual({});
72
- expect(context.types).toEqual({});
73
- });
74
-
75
- it('creates context with storage.types containing valid type instances', () => {
76
- const contract = createParamTypesTestContract({
77
- types: {
78
- Vector1536: {
79
- codecId: 'pg/vector@1',
80
- nativeType: 'vector',
81
- typeParams: { length: 1536 },
82
- },
83
- },
84
- });
85
- const context = createTestContext(contract, createStubAdapter());
86
-
87
- expect(context.contract.storage.types).toEqual({
88
- Vector1536: {
89
- codecId: 'pg/vector@1',
90
- nativeType: 'vector',
91
- typeParams: { length: 1536 },
92
- },
93
- });
94
- // types registry should contain the raw type instance (no init hook provided)
95
- expect(context.types['Vector1536']).toBeDefined();
96
- });
97
- });
98
-
99
- describe('typeParams validation with paramsSchema', () => {
100
- const vectorParamsSchema = arktype({
101
- length: 'number',
102
- });
103
-
104
- function createVectorExtensionDescriptor(options?: {
105
- paramsSchema?: Type<{ length: number }>;
106
- init?: (params: { length: number }) => { dimensions: number };
107
- }): SqlRuntimeExtensionDescriptor<'postgres'> {
108
- // biome-ignore lint/suspicious/noExplicitAny: test helper with flexible type params
109
- const parameterizedCodecs: RuntimeParameterizedCodecDescriptor<any, any>[] = [
110
- {
111
- codecId: 'pg/vector@1',
112
- paramsSchema: options?.paramsSchema ?? vectorParamsSchema,
113
- ...ifDefined('init', options?.init),
114
- },
115
- ];
116
-
117
- const registry = createCodecRegistry();
118
- registry.register(
119
- codec({
120
- typeId: 'pg/vector@1',
121
- targetTypes: ['vector'],
122
- encode: (v: number[]) => v,
123
- decode: (w: number[]) => w,
124
- }),
125
- );
126
-
127
- return {
128
- kind: 'extension' as const,
129
- id: 'pgvector',
130
- version: '0.0.1',
131
- familyId: 'sql' as const,
132
- targetId: 'postgres' as const,
133
- codecs: () => registry,
134
- parameterizedCodecs: () => parameterizedCodecs,
135
- create() {
136
- return {
137
- familyId: 'sql' as const,
138
- targetId: 'postgres' as const,
139
- };
140
- },
141
- };
142
- }
143
-
144
- it('validates typeParams against codec paramsSchema', () => {
145
- const contract = createParamTypesTestContract({
146
- types: {
147
- Vector1536: {
148
- codecId: 'pg/vector@1',
149
- nativeType: 'vector',
150
- typeParams: { length: 1536 },
151
- },
152
- },
153
- });
154
-
155
- const context = createTestContext(contract, createStubAdapter(), {
156
- extensionPacks: [createVectorExtensionDescriptor()],
157
- });
158
-
159
- expect(context.types['Vector1536']).toBeDefined();
160
- });
161
-
162
- it('rejects invalid typeParams with stable error code', () => {
163
- const contract = createParamTypesTestContract({
164
- types: {
165
- InvalidVector: {
166
- codecId: 'pg/vector@1',
167
- nativeType: 'vector',
168
- typeParams: { length: 'not-a-number' },
169
- },
170
- },
171
- });
172
-
173
- let thrownError: unknown;
174
- try {
175
- createTestContext(contract, createStubAdapter(), {
176
- extensionPacks: [createVectorExtensionDescriptor()],
177
- });
178
- } catch (e) {
179
- thrownError = e;
180
- }
181
-
182
- expect(thrownError).toBeDefined();
183
- expect(thrownError).toMatchObject({
184
- code: 'RUNTIME.TYPE_PARAMS_INVALID',
185
- category: 'RUNTIME',
186
- severity: 'error',
187
- details: {
188
- codecId: 'pg/vector@1',
189
- typeName: 'InvalidVector',
190
- },
191
- });
192
- });
193
-
194
- it('rejects missing required typeParams with stable error code', () => {
195
- const contract = createParamTypesTestContract({
196
- types: {
197
- InvalidVector: {
198
- codecId: 'pg/vector@1',
199
- nativeType: 'vector',
200
- typeParams: {},
201
- },
202
- },
203
- });
204
-
205
- let thrownError: unknown;
206
- try {
207
- createTestContext(contract, createStubAdapter(), {
208
- extensionPacks: [createVectorExtensionDescriptor()],
209
- });
210
- } catch (e) {
211
- thrownError = e;
212
- }
213
-
214
- expect(thrownError).toBeDefined();
215
- expect(thrownError).toMatchObject({
216
- code: 'RUNTIME.TYPE_PARAMS_INVALID',
217
- category: 'RUNTIME',
218
- severity: 'error',
219
- });
220
- });
221
- });
222
-
223
- describe('init hook for type helpers', () => {
224
- it('calls init hook and stores result in context.types', () => {
225
- const vectorParamsSchema = arktype({
226
- length: 'number',
227
- });
228
-
229
- const initFn = (params: { length: number }) => ({
230
- dimensions: params.length,
231
- isVector: true,
232
- });
233
-
234
- const registry = createCodecRegistry();
235
- registry.register(
236
- codec({
237
- typeId: 'pg/vector@1',
238
- targetTypes: ['vector'],
239
- encode: (v: number[]) => v,
240
- decode: (w: number[]) => w,
241
- }),
242
- );
243
-
244
- const parameterizedCodecs = [
245
- {
246
- codecId: 'pg/vector@1',
247
- paramsSchema: vectorParamsSchema,
248
- init: initFn,
249
- },
250
- ];
251
-
252
- const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
253
- kind: 'extension' as const,
254
- id: 'pgvector',
255
- version: '0.0.1',
256
- familyId: 'sql' as const,
257
- targetId: 'postgres' as const,
258
- codecs: () => registry,
259
- parameterizedCodecs: () => parameterizedCodecs,
260
- create() {
261
- return {
262
- familyId: 'sql' as const,
263
- targetId: 'postgres' as const,
264
- };
265
- },
266
- };
267
-
268
- const contract = createParamTypesTestContract({
269
- types: {
270
- Vector1536: {
271
- codecId: 'pg/vector@1',
272
- nativeType: 'vector',
273
- typeParams: { length: 1536 },
274
- },
275
- },
276
- });
277
-
278
- const context = createTestContext(contract, createStubAdapter(), {
279
- extensionPacks: [extensionDescriptor],
280
- });
281
-
282
- expect(context.types['Vector1536']).toEqual({
283
- dimensions: 1536,
284
- isVector: true,
285
- });
286
- });
287
-
288
- it('stores full type instance when no init hook is provided', () => {
289
- const vectorParamsSchema = arktype({
290
- length: 'number',
291
- });
292
-
293
- const registry = createCodecRegistry();
294
- registry.register(
295
- codec({
296
- typeId: 'pg/vector@1',
297
- targetTypes: ['vector'],
298
- encode: (v: number[]) => v,
299
- decode: (w: number[]) => w,
300
- }),
301
- );
302
-
303
- const parameterizedCodecs = [
304
- {
305
- codecId: 'pg/vector@1',
306
- paramsSchema: vectorParamsSchema,
307
- },
308
- ];
309
-
310
- const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
311
- kind: 'extension' as const,
312
- id: 'pgvector',
313
- version: '0.0.1',
314
- familyId: 'sql' as const,
315
- targetId: 'postgres' as const,
316
- codecs: () => registry,
317
- parameterizedCodecs: () => parameterizedCodecs,
318
- create() {
319
- return {
320
- familyId: 'sql' as const,
321
- targetId: 'postgres' as const,
322
- };
323
- },
324
- };
325
-
326
- const contract = createParamTypesTestContract({
327
- types: {
328
- Vector1536: {
329
- codecId: 'pg/vector@1',
330
- nativeType: 'vector',
331
- typeParams: { length: 1536 },
332
- },
333
- },
334
- });
335
-
336
- const context = createTestContext(contract, createStubAdapter(), {
337
- extensionPacks: [extensionDescriptor],
338
- });
339
-
340
- // Without init hook, stores the full type instance (matches contract typing)
341
- expect(context.types['Vector1536']).toEqual({
342
- codecId: 'pg/vector@1',
343
- nativeType: 'vector',
344
- typeParams: { length: 1536 },
345
- });
346
- });
347
- });
348
-
349
- describe('column typeParams validation', () => {
350
- it('validates inline column typeParams against codec paramsSchema', () => {
351
- const vectorParamsSchema = arktype({
352
- length: 'number',
353
- });
354
-
355
- const registry = createCodecRegistry();
356
- registry.register(
357
- codec({
358
- typeId: 'pg/vector@1',
359
- targetTypes: ['vector'],
360
- encode: (v: number[]) => v,
361
- decode: (w: number[]) => w,
362
- }),
363
- );
364
-
365
- const parameterizedCodecs = [
366
- {
367
- codecId: 'pg/vector@1',
368
- paramsSchema: vectorParamsSchema,
369
- },
370
- ];
371
-
372
- const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
373
- kind: 'extension' as const,
374
- id: 'pgvector',
375
- version: '0.0.1',
376
- familyId: 'sql' as const,
377
- targetId: 'postgres' as const,
378
- codecs: () => registry,
379
- parameterizedCodecs: () => parameterizedCodecs,
380
- create() {
381
- return {
382
- familyId: 'sql' as const,
383
- targetId: 'postgres' as const,
384
- };
385
- },
386
- };
387
-
388
- const contract = createParamTypesTestContract({
389
- tableColumns: {
390
- id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
391
- embedding: {
392
- nativeType: 'vector',
393
- codecId: 'pg/vector@1',
394
- nullable: false,
395
- typeParams: { length: 1536 },
396
- },
397
- },
398
- });
399
-
400
- // Should not throw - valid typeParams
401
- const context = createTestContext(contract, createStubAdapter(), {
402
- extensionPacks: [extensionDescriptor],
403
- });
404
-
405
- expect(context.contract).toBe(contract);
406
- });
407
-
408
- it('rejects invalid inline column typeParams with stable error code', () => {
409
- const vectorParamsSchema = arktype({
410
- length: 'number',
411
- });
412
-
413
- const registry = createCodecRegistry();
414
- registry.register(
415
- codec({
416
- typeId: 'pg/vector@1',
417
- targetTypes: ['vector'],
418
- encode: (v: number[]) => v,
419
- decode: (w: number[]) => w,
420
- }),
421
- );
422
-
423
- const parameterizedCodecs = [
424
- {
425
- codecId: 'pg/vector@1',
426
- paramsSchema: vectorParamsSchema,
427
- },
428
- ];
429
-
430
- const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
431
- kind: 'extension' as const,
432
- id: 'pgvector',
433
- version: '0.0.1',
434
- familyId: 'sql' as const,
435
- targetId: 'postgres' as const,
436
- codecs: () => registry,
437
- parameterizedCodecs: () => parameterizedCodecs,
438
- create() {
439
- return {
440
- familyId: 'sql' as const,
441
- targetId: 'postgres' as const,
442
- };
443
- },
444
- };
445
-
446
- const contract = createParamTypesTestContract({
447
- tableColumns: {
448
- id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
449
- embedding: {
450
- nativeType: 'vector',
451
- codecId: 'pg/vector@1',
452
- nullable: false,
453
- typeParams: { length: 'invalid' },
454
- },
455
- },
456
- });
457
-
458
- let thrownError: unknown;
459
- try {
460
- createTestContext(contract, createStubAdapter(), {
461
- extensionPacks: [extensionDescriptor],
462
- });
463
- } catch (e) {
464
- thrownError = e;
465
- }
466
-
467
- expect(thrownError).toBeDefined();
468
- expect(thrownError).toMatchObject({
469
- code: 'RUNTIME.TYPE_PARAMS_INVALID',
470
- category: 'RUNTIME',
471
- severity: 'error',
472
- details: {
473
- tableName: 'test',
474
- columnName: 'embedding',
475
- },
476
- });
477
- });
478
- });
479
-
480
- describe('duplicate codec descriptor detection', () => {
481
- it('throws RUNTIME.DUPLICATE_PARAMETERIZED_CODEC when multiple extensions provide same codecId', () => {
482
- const vectorParamsSchema = arktype({
483
- length: 'number',
484
- });
485
-
486
- function createVectorExtension(id: string): SqlRuntimeExtensionDescriptor<'postgres'> {
487
- const parameterizedCodecs = [
488
- {
489
- codecId: 'pg/vector@1',
490
- paramsSchema: vectorParamsSchema,
491
- },
492
- ];
493
-
494
- return {
495
- kind: 'extension' as const,
496
- id,
497
- version: '0.0.1',
498
- familyId: 'sql' as const,
499
- targetId: 'postgres' as const,
500
- codecs: () => createCodecRegistry(),
501
- parameterizedCodecs: () => parameterizedCodecs,
502
- create() {
503
- return {
504
- familyId: 'sql' as const,
505
- targetId: 'postgres' as const,
506
- };
507
- },
508
- };
509
- }
510
-
511
- const contract = createParamTypesTestContract();
512
-
513
- expect(() =>
514
- createTestContext(contract, createStubAdapter(), {
515
- extensionPacks: [createVectorExtension('ext-1'), createVectorExtension('ext-2')],
516
- }),
517
- ).toThrow(
518
- expect.objectContaining({
519
- code: 'RUNTIME.DUPLICATE_PARAMETERIZED_CODEC',
520
- category: 'RUNTIME',
521
- severity: 'error',
522
- details: {
523
- codecId: 'pg/vector@1',
524
- },
525
- }),
526
- );
527
- });
528
- });
529
- });