@prisma-next/sql-runtime 0.3.0-dev.6 → 0.3.0-dev.63
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.
- package/LICENSE +201 -0
- package/README.md +137 -26
- package/dist/exports-BhZqJPVb.mjs +771 -0
- package/dist/exports-BhZqJPVb.mjs.map +1 -0
- package/dist/index-D59jqEKF.d.mts +159 -0
- package/dist/index-D59jqEKF.d.mts.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +3 -0
- package/dist/test/utils.d.mts +82 -0
- package/dist/test/utils.d.mts.map +1 -0
- package/dist/test/utils.mjs +212 -0
- package/dist/test/utils.mjs.map +1 -0
- package/package.json +32 -25
- package/src/codecs/decoding.ts +84 -3
- package/src/codecs/encoding.ts +15 -2
- package/src/codecs/json-schema-validation.ts +61 -0
- package/src/exports/index.ts +14 -6
- package/src/lower-sql-plan.ts +8 -8
- package/src/plugins/lints.ts +204 -0
- package/src/sql-context.ts +385 -98
- package/src/sql-family-adapter.ts +9 -5
- package/src/sql-marker.ts +2 -2
- package/src/sql-runtime.ts +131 -31
- package/test/async-iterable-result.test.ts +42 -34
- package/test/context.types.test-d.ts +68 -0
- package/test/execution-stack.test.ts +166 -0
- package/test/json-schema-validation.test.ts +653 -0
- package/test/lints.test.ts +330 -0
- package/test/parameterized-types.test.ts +539 -0
- package/test/sql-context.test.ts +292 -117
- package/test/sql-family-adapter.test.ts +7 -6
- package/test/sql-runtime.test.ts +218 -30
- package/test/utils.ts +80 -51
- package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js +0 -137863
- package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js.map +0 -1
- package/dist/amcheck-24VY6X5V.js +0 -13
- package/dist/amcheck-24VY6X5V.js.map +0 -1
- package/dist/bloom-VS74NLHT.js +0 -13
- package/dist/bloom-VS74NLHT.js.map +0 -1
- package/dist/btree_gin-WBC4EAAI.js +0 -13
- package/dist/btree_gin-WBC4EAAI.js.map +0 -1
- package/dist/btree_gist-UNC6QD3M.js +0 -13
- package/dist/btree_gist-UNC6QD3M.js.map +0 -1
- package/dist/chunk-3KTOEDFX.js +0 -49
- package/dist/chunk-3KTOEDFX.js.map +0 -1
- package/dist/chunk-47DZBRQC.js +0 -1280
- package/dist/chunk-47DZBRQC.js.map +0 -1
- package/dist/chunk-52N6AFZM.js +0 -133
- package/dist/chunk-52N6AFZM.js.map +0 -1
- package/dist/chunk-7D4SUZUM.js +0 -38
- package/dist/chunk-7D4SUZUM.js.map +0 -1
- package/dist/chunk-C6I3V3DM.js +0 -455
- package/dist/chunk-C6I3V3DM.js.map +0 -1
- package/dist/chunk-ECWIHLAT.js +0 -37
- package/dist/chunk-ECWIHLAT.js.map +0 -1
- package/dist/chunk-EI626SDC.js +0 -105
- package/dist/chunk-EI626SDC.js.map +0 -1
- package/dist/chunk-UKKOYUGL.js +0 -578
- package/dist/chunk-UKKOYUGL.js.map +0 -1
- package/dist/chunk-XPLNMXQV.js +0 -1537
- package/dist/chunk-XPLNMXQV.js.map +0 -1
- package/dist/citext-T7MXGUY7.js +0 -13
- package/dist/citext-T7MXGUY7.js.map +0 -1
- package/dist/client-5FENX6AW.js +0 -299
- package/dist/client-5FENX6AW.js.map +0 -1
- package/dist/cube-TFDQBZCI.js +0 -13
- package/dist/cube-TFDQBZCI.js.map +0 -1
- package/dist/dict_int-AEUOPGWP.js +0 -13
- package/dist/dict_int-AEUOPGWP.js.map +0 -1
- package/dist/dict_xsyn-DAAYX3FL.js +0 -13
- package/dist/dict_xsyn-DAAYX3FL.js.map +0 -1
- package/dist/dist-AQ3LWXOX.js +0 -570
- package/dist/dist-AQ3LWXOX.js.map +0 -1
- package/dist/dist-LBVX6BJW.js +0 -189
- package/dist/dist-LBVX6BJW.js.map +0 -1
- package/dist/dist-WLKUVDN2.js +0 -5127
- package/dist/dist-WLKUVDN2.js.map +0 -1
- package/dist/earthdistance-KIGTF4LE.js +0 -13
- package/dist/earthdistance-KIGTF4LE.js.map +0 -1
- package/dist/file_fdw-5N55UP6I.js +0 -13
- package/dist/file_fdw-5N55UP6I.js.map +0 -1
- package/dist/fuzzystrmatch-KN3YWBFP.js +0 -13
- package/dist/fuzzystrmatch-KN3YWBFP.js.map +0 -1
- package/dist/hstore-YX726NKN.js +0 -13
- package/dist/hstore-YX726NKN.js.map +0 -1
- package/dist/http-exception-FZY2H4OF.js +0 -8
- package/dist/http-exception-FZY2H4OF.js.map +0 -1
- package/dist/index.js +0 -30
- package/dist/index.js.map +0 -1
- package/dist/intarray-NKVXNO2D.js +0 -13
- package/dist/intarray-NKVXNO2D.js.map +0 -1
- package/dist/isn-FTEMJGEV.js +0 -13
- package/dist/isn-FTEMJGEV.js.map +0 -1
- package/dist/lo-DB7L4NGI.js +0 -13
- package/dist/lo-DB7L4NGI.js.map +0 -1
- package/dist/logger-WQ7SHNDD.js +0 -68
- package/dist/logger-WQ7SHNDD.js.map +0 -1
- package/dist/ltree-Z32TZT6W.js +0 -13
- package/dist/ltree-Z32TZT6W.js.map +0 -1
- package/dist/nodefs-NM46ACH7.js +0 -31
- package/dist/nodefs-NM46ACH7.js.map +0 -1
- package/dist/opfs-ahp-NJO33LVZ.js +0 -332
- package/dist/opfs-ahp-NJO33LVZ.js.map +0 -1
- package/dist/pageinspect-YP3IZR4X.js +0 -13
- package/dist/pageinspect-YP3IZR4X.js.map +0 -1
- package/dist/pg_buffercache-7TD5J2FB.js +0 -13
- package/dist/pg_buffercache-7TD5J2FB.js.map +0 -1
- package/dist/pg_dump-SG4KYBUB.js +0 -2492
- package/dist/pg_dump-SG4KYBUB.js.map +0 -1
- package/dist/pg_freespacemap-DZDNCPZK.js +0 -13
- package/dist/pg_freespacemap-DZDNCPZK.js.map +0 -1
- package/dist/pg_surgery-J2MUEWEP.js +0 -13
- package/dist/pg_surgery-J2MUEWEP.js.map +0 -1
- package/dist/pg_trgm-7VNQOYS6.js +0 -13
- package/dist/pg_trgm-7VNQOYS6.js.map +0 -1
- package/dist/pg_visibility-TTSIPHFL.js +0 -13
- package/dist/pg_visibility-TTSIPHFL.js.map +0 -1
- package/dist/pg_walinspect-KPFHSHRJ.js +0 -13
- package/dist/pg_walinspect-KPFHSHRJ.js.map +0 -1
- package/dist/proxy-signals-GUDAMDHV.js +0 -39
- package/dist/proxy-signals-GUDAMDHV.js.map +0 -1
- package/dist/seg-IYVDLE4O.js +0 -13
- package/dist/seg-IYVDLE4O.js.map +0 -1
- package/dist/src/codecs/decoding.d.ts +0 -4
- package/dist/src/codecs/decoding.d.ts.map +0 -1
- package/dist/src/codecs/encoding.d.ts +0 -5
- package/dist/src/codecs/encoding.d.ts.map +0 -1
- package/dist/src/codecs/validation.d.ts +0 -6
- package/dist/src/codecs/validation.d.ts.map +0 -1
- package/dist/src/exports/index.d.ts +0 -11
- package/dist/src/exports/index.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/lower-sql-plan.d.ts +0 -15
- package/dist/src/lower-sql-plan.d.ts.map +0 -1
- package/dist/src/sql-context.d.ts +0 -65
- package/dist/src/sql-context.d.ts.map +0 -1
- package/dist/src/sql-family-adapter.d.ts +0 -10
- package/dist/src/sql-family-adapter.d.ts.map +0 -1
- package/dist/src/sql-marker.d.ts +0 -22
- package/dist/src/sql-marker.d.ts.map +0 -1
- package/dist/src/sql-runtime.d.ts +0 -25
- package/dist/src/sql-runtime.d.ts.map +0 -1
- package/dist/tablefunc-EF4RCS7S.js +0 -13
- package/dist/tablefunc-EF4RCS7S.js.map +0 -1
- package/dist/tcn-3VT5BQYW.js +0 -13
- package/dist/tcn-3VT5BQYW.js.map +0 -1
- package/dist/test/utils.d.ts +0 -59
- package/dist/test/utils.d.ts.map +0 -1
- package/dist/test/utils.js +0 -24634
- package/dist/test/utils.js.map +0 -1
- package/dist/tiny-CW6F4GX6.js +0 -10
- package/dist/tiny-CW6F4GX6.js.map +0 -1
- package/dist/tsm_system_rows-ES7KNUQH.js +0 -13
- package/dist/tsm_system_rows-ES7KNUQH.js.map +0 -1
- package/dist/tsm_system_time-76WEIMBG.js +0 -13
- package/dist/tsm_system_time-76WEIMBG.js.map +0 -1
- package/dist/unaccent-7RYF3R64.js +0 -13
- package/dist/unaccent-7RYF3R64.js.map +0 -1
- package/dist/utility-Q5A254LJ-J4HTKZPT.js +0 -347
- package/dist/utility-Q5A254LJ-J4HTKZPT.js.map +0 -1
- package/dist/uuid_ossp-4ETE4FPE.js +0 -13
- package/dist/uuid_ossp-4ETE4FPE.js.map +0 -1
- package/dist/vector-74GPNV7V.js +0 -13
- package/dist/vector-74GPNV7V.js.map +0 -1
- package/src/index.ts +0 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { coreHash } from '@prisma-next/contract/types';
|
|
2
|
+
import type { SqlContract, SqlStorage, StorageTypeInstance } from '@prisma-next/sql-contract/types';
|
|
3
|
+
import { codec, createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
4
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
5
|
+
import type { Type } from 'arktype';
|
|
6
|
+
import { type as arktype } from 'arktype';
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import type {
|
|
9
|
+
RuntimeParameterizedCodecDescriptor,
|
|
10
|
+
SqlRuntimeExtensionDescriptor,
|
|
11
|
+
} from '../src/sql-context';
|
|
12
|
+
import { createStubAdapter, createTestContext } from './utils';
|
|
13
|
+
|
|
14
|
+
// =============================================================================
|
|
15
|
+
// Test helpers
|
|
16
|
+
// =============================================================================
|
|
17
|
+
|
|
18
|
+
function createParamTypesTestContract(
|
|
19
|
+
options?: Partial<{
|
|
20
|
+
types: Record<string, StorageTypeInstance>;
|
|
21
|
+
tableColumns: Record<
|
|
22
|
+
string,
|
|
23
|
+
{
|
|
24
|
+
nativeType: string;
|
|
25
|
+
codecId: string;
|
|
26
|
+
nullable: boolean;
|
|
27
|
+
typeParams?: Record<string, unknown>;
|
|
28
|
+
typeRef?: string;
|
|
29
|
+
}
|
|
30
|
+
>;
|
|
31
|
+
}>,
|
|
32
|
+
): SqlContract<SqlStorage> {
|
|
33
|
+
return {
|
|
34
|
+
schemaVersion: '1',
|
|
35
|
+
targetFamily: 'sql',
|
|
36
|
+
target: 'postgres',
|
|
37
|
+
storageHash: coreHash('sha256:test'),
|
|
38
|
+
models: {},
|
|
39
|
+
relations: {},
|
|
40
|
+
storage: {
|
|
41
|
+
tables: {
|
|
42
|
+
test: {
|
|
43
|
+
columns: options?.tableColumns ?? {
|
|
44
|
+
id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
|
|
45
|
+
},
|
|
46
|
+
primaryKey: { columns: ['id'] },
|
|
47
|
+
uniques: [],
|
|
48
|
+
indexes: [],
|
|
49
|
+
foreignKeys: [],
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
...ifDefined('types', options?.types),
|
|
53
|
+
},
|
|
54
|
+
extensionPacks: {},
|
|
55
|
+
capabilities: {},
|
|
56
|
+
meta: {},
|
|
57
|
+
sources: {},
|
|
58
|
+
mappings: {
|
|
59
|
+
codecTypes: {},
|
|
60
|
+
operationTypes: {},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// =============================================================================
|
|
66
|
+
// Tests: Parameterized type validation
|
|
67
|
+
// =============================================================================
|
|
68
|
+
|
|
69
|
+
describe('parameterized types', () => {
|
|
70
|
+
describe('storage.types validation', () => {
|
|
71
|
+
it('creates context with empty storage.types', () => {
|
|
72
|
+
const contract = createParamTypesTestContract({ types: {} });
|
|
73
|
+
const context = createTestContext(contract, createStubAdapter());
|
|
74
|
+
|
|
75
|
+
expect(context.contract.storage.types).toEqual({});
|
|
76
|
+
expect(context.types).toEqual({});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('creates context with storage.types containing valid type instances', () => {
|
|
80
|
+
const contract = createParamTypesTestContract({
|
|
81
|
+
types: {
|
|
82
|
+
Vector1536: {
|
|
83
|
+
codecId: 'pg/vector@1',
|
|
84
|
+
nativeType: 'vector',
|
|
85
|
+
typeParams: { length: 1536 },
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const context = createTestContext(contract, createStubAdapter());
|
|
90
|
+
|
|
91
|
+
expect(context.contract.storage.types).toEqual({
|
|
92
|
+
Vector1536: {
|
|
93
|
+
codecId: 'pg/vector@1',
|
|
94
|
+
nativeType: 'vector',
|
|
95
|
+
typeParams: { length: 1536 },
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
// types registry should contain the raw type instance (no init hook provided)
|
|
99
|
+
expect(context.types['Vector1536']).toBeDefined();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('typeParams validation with paramsSchema', () => {
|
|
104
|
+
const vectorParamsSchema = arktype({
|
|
105
|
+
length: 'number',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
function createVectorExtensionDescriptor(options?: {
|
|
109
|
+
paramsSchema?: Type<{ length: number }>;
|
|
110
|
+
init?: (params: { length: number }) => { dimensions: number };
|
|
111
|
+
}): SqlRuntimeExtensionDescriptor<'postgres'> {
|
|
112
|
+
// biome-ignore lint/suspicious/noExplicitAny: test helper with flexible type params
|
|
113
|
+
const parameterizedCodecs: RuntimeParameterizedCodecDescriptor<any, any>[] = [
|
|
114
|
+
{
|
|
115
|
+
codecId: 'pg/vector@1',
|
|
116
|
+
paramsSchema: options?.paramsSchema ?? vectorParamsSchema,
|
|
117
|
+
...ifDefined('init', options?.init),
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
const registry = createCodecRegistry();
|
|
122
|
+
registry.register(
|
|
123
|
+
codec({
|
|
124
|
+
typeId: 'pg/vector@1',
|
|
125
|
+
targetTypes: ['vector'],
|
|
126
|
+
encode: (v: number[]) => v,
|
|
127
|
+
decode: (w: number[]) => w,
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
kind: 'extension' as const,
|
|
133
|
+
id: 'pgvector',
|
|
134
|
+
version: '0.0.1',
|
|
135
|
+
familyId: 'sql' as const,
|
|
136
|
+
targetId: 'postgres' as const,
|
|
137
|
+
codecs: () => registry,
|
|
138
|
+
operationSignatures: () => [],
|
|
139
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
140
|
+
create() {
|
|
141
|
+
return {
|
|
142
|
+
familyId: 'sql' as const,
|
|
143
|
+
targetId: 'postgres' as const,
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
it('validates typeParams against codec paramsSchema', () => {
|
|
150
|
+
const contract = createParamTypesTestContract({
|
|
151
|
+
types: {
|
|
152
|
+
Vector1536: {
|
|
153
|
+
codecId: 'pg/vector@1',
|
|
154
|
+
nativeType: 'vector',
|
|
155
|
+
typeParams: { length: 1536 },
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const context = createTestContext(contract, createStubAdapter(), {
|
|
161
|
+
extensionPacks: [createVectorExtensionDescriptor()],
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(context.types['Vector1536']).toBeDefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('rejects invalid typeParams with stable error code', () => {
|
|
168
|
+
const contract = createParamTypesTestContract({
|
|
169
|
+
types: {
|
|
170
|
+
InvalidVector: {
|
|
171
|
+
codecId: 'pg/vector@1',
|
|
172
|
+
nativeType: 'vector',
|
|
173
|
+
typeParams: { length: 'not-a-number' },
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
let thrownError: unknown;
|
|
179
|
+
try {
|
|
180
|
+
createTestContext(contract, createStubAdapter(), {
|
|
181
|
+
extensionPacks: [createVectorExtensionDescriptor()],
|
|
182
|
+
});
|
|
183
|
+
} catch (e) {
|
|
184
|
+
thrownError = e;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
expect(thrownError).toBeDefined();
|
|
188
|
+
expect(thrownError).toMatchObject({
|
|
189
|
+
code: 'RUNTIME.TYPE_PARAMS_INVALID',
|
|
190
|
+
category: 'RUNTIME',
|
|
191
|
+
severity: 'error',
|
|
192
|
+
details: {
|
|
193
|
+
codecId: 'pg/vector@1',
|
|
194
|
+
typeName: 'InvalidVector',
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('rejects missing required typeParams with stable error code', () => {
|
|
200
|
+
const contract = createParamTypesTestContract({
|
|
201
|
+
types: {
|
|
202
|
+
InvalidVector: {
|
|
203
|
+
codecId: 'pg/vector@1',
|
|
204
|
+
nativeType: 'vector',
|
|
205
|
+
typeParams: {},
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
let thrownError: unknown;
|
|
211
|
+
try {
|
|
212
|
+
createTestContext(contract, createStubAdapter(), {
|
|
213
|
+
extensionPacks: [createVectorExtensionDescriptor()],
|
|
214
|
+
});
|
|
215
|
+
} catch (e) {
|
|
216
|
+
thrownError = e;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
expect(thrownError).toBeDefined();
|
|
220
|
+
expect(thrownError).toMatchObject({
|
|
221
|
+
code: 'RUNTIME.TYPE_PARAMS_INVALID',
|
|
222
|
+
category: 'RUNTIME',
|
|
223
|
+
severity: 'error',
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('init hook for type helpers', () => {
|
|
229
|
+
it('calls init hook and stores result in context.types', () => {
|
|
230
|
+
const vectorParamsSchema = arktype({
|
|
231
|
+
length: 'number',
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const initFn = (params: { length: number }) => ({
|
|
235
|
+
dimensions: params.length,
|
|
236
|
+
isVector: true,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const registry = createCodecRegistry();
|
|
240
|
+
registry.register(
|
|
241
|
+
codec({
|
|
242
|
+
typeId: 'pg/vector@1',
|
|
243
|
+
targetTypes: ['vector'],
|
|
244
|
+
encode: (v: number[]) => v,
|
|
245
|
+
decode: (w: number[]) => w,
|
|
246
|
+
}),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
const parameterizedCodecs = [
|
|
250
|
+
{
|
|
251
|
+
codecId: 'pg/vector@1',
|
|
252
|
+
paramsSchema: vectorParamsSchema,
|
|
253
|
+
init: initFn,
|
|
254
|
+
},
|
|
255
|
+
];
|
|
256
|
+
|
|
257
|
+
const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
258
|
+
kind: 'extension' as const,
|
|
259
|
+
id: 'pgvector',
|
|
260
|
+
version: '0.0.1',
|
|
261
|
+
familyId: 'sql' as const,
|
|
262
|
+
targetId: 'postgres' as const,
|
|
263
|
+
codecs: () => registry,
|
|
264
|
+
operationSignatures: () => [],
|
|
265
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
266
|
+
create() {
|
|
267
|
+
return {
|
|
268
|
+
familyId: 'sql' as const,
|
|
269
|
+
targetId: 'postgres' as const,
|
|
270
|
+
};
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const contract = createParamTypesTestContract({
|
|
275
|
+
types: {
|
|
276
|
+
Vector1536: {
|
|
277
|
+
codecId: 'pg/vector@1',
|
|
278
|
+
nativeType: 'vector',
|
|
279
|
+
typeParams: { length: 1536 },
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const context = createTestContext(contract, createStubAdapter(), {
|
|
285
|
+
extensionPacks: [extensionDescriptor],
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
expect(context.types['Vector1536']).toEqual({
|
|
289
|
+
dimensions: 1536,
|
|
290
|
+
isVector: true,
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('stores full type instance when no init hook is provided', () => {
|
|
295
|
+
const vectorParamsSchema = arktype({
|
|
296
|
+
length: 'number',
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const registry = createCodecRegistry();
|
|
300
|
+
registry.register(
|
|
301
|
+
codec({
|
|
302
|
+
typeId: 'pg/vector@1',
|
|
303
|
+
targetTypes: ['vector'],
|
|
304
|
+
encode: (v: number[]) => v,
|
|
305
|
+
decode: (w: number[]) => w,
|
|
306
|
+
}),
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const parameterizedCodecs = [
|
|
310
|
+
{
|
|
311
|
+
codecId: 'pg/vector@1',
|
|
312
|
+
paramsSchema: vectorParamsSchema,
|
|
313
|
+
},
|
|
314
|
+
];
|
|
315
|
+
|
|
316
|
+
const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
317
|
+
kind: 'extension' as const,
|
|
318
|
+
id: 'pgvector',
|
|
319
|
+
version: '0.0.1',
|
|
320
|
+
familyId: 'sql' as const,
|
|
321
|
+
targetId: 'postgres' as const,
|
|
322
|
+
codecs: () => registry,
|
|
323
|
+
operationSignatures: () => [],
|
|
324
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
325
|
+
create() {
|
|
326
|
+
return {
|
|
327
|
+
familyId: 'sql' as const,
|
|
328
|
+
targetId: 'postgres' as const,
|
|
329
|
+
};
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const contract = createParamTypesTestContract({
|
|
334
|
+
types: {
|
|
335
|
+
Vector1536: {
|
|
336
|
+
codecId: 'pg/vector@1',
|
|
337
|
+
nativeType: 'vector',
|
|
338
|
+
typeParams: { length: 1536 },
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
const context = createTestContext(contract, createStubAdapter(), {
|
|
344
|
+
extensionPacks: [extensionDescriptor],
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Without init hook, stores the full type instance (matches contract typing)
|
|
348
|
+
expect(context.types['Vector1536']).toEqual({
|
|
349
|
+
codecId: 'pg/vector@1',
|
|
350
|
+
nativeType: 'vector',
|
|
351
|
+
typeParams: { length: 1536 },
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
describe('column typeParams validation', () => {
|
|
357
|
+
it('validates inline column typeParams against codec paramsSchema', () => {
|
|
358
|
+
const vectorParamsSchema = arktype({
|
|
359
|
+
length: 'number',
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const registry = createCodecRegistry();
|
|
363
|
+
registry.register(
|
|
364
|
+
codec({
|
|
365
|
+
typeId: 'pg/vector@1',
|
|
366
|
+
targetTypes: ['vector'],
|
|
367
|
+
encode: (v: number[]) => v,
|
|
368
|
+
decode: (w: number[]) => w,
|
|
369
|
+
}),
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
const parameterizedCodecs = [
|
|
373
|
+
{
|
|
374
|
+
codecId: 'pg/vector@1',
|
|
375
|
+
paramsSchema: vectorParamsSchema,
|
|
376
|
+
},
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
380
|
+
kind: 'extension' as const,
|
|
381
|
+
id: 'pgvector',
|
|
382
|
+
version: '0.0.1',
|
|
383
|
+
familyId: 'sql' as const,
|
|
384
|
+
targetId: 'postgres' as const,
|
|
385
|
+
codecs: () => registry,
|
|
386
|
+
operationSignatures: () => [],
|
|
387
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
388
|
+
create() {
|
|
389
|
+
return {
|
|
390
|
+
familyId: 'sql' as const,
|
|
391
|
+
targetId: 'postgres' as const,
|
|
392
|
+
};
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const contract = createParamTypesTestContract({
|
|
397
|
+
tableColumns: {
|
|
398
|
+
id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
|
|
399
|
+
embedding: {
|
|
400
|
+
nativeType: 'vector',
|
|
401
|
+
codecId: 'pg/vector@1',
|
|
402
|
+
nullable: false,
|
|
403
|
+
typeParams: { length: 1536 },
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
// Should not throw - valid typeParams
|
|
409
|
+
const context = createTestContext(contract, createStubAdapter(), {
|
|
410
|
+
extensionPacks: [extensionDescriptor],
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
expect(context.contract).toBe(contract);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('rejects invalid inline column typeParams with stable error code', () => {
|
|
417
|
+
const vectorParamsSchema = arktype({
|
|
418
|
+
length: 'number',
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const registry = createCodecRegistry();
|
|
422
|
+
registry.register(
|
|
423
|
+
codec({
|
|
424
|
+
typeId: 'pg/vector@1',
|
|
425
|
+
targetTypes: ['vector'],
|
|
426
|
+
encode: (v: number[]) => v,
|
|
427
|
+
decode: (w: number[]) => w,
|
|
428
|
+
}),
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
const parameterizedCodecs = [
|
|
432
|
+
{
|
|
433
|
+
codecId: 'pg/vector@1',
|
|
434
|
+
paramsSchema: vectorParamsSchema,
|
|
435
|
+
},
|
|
436
|
+
];
|
|
437
|
+
|
|
438
|
+
const extensionDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
439
|
+
kind: 'extension' as const,
|
|
440
|
+
id: 'pgvector',
|
|
441
|
+
version: '0.0.1',
|
|
442
|
+
familyId: 'sql' as const,
|
|
443
|
+
targetId: 'postgres' as const,
|
|
444
|
+
codecs: () => registry,
|
|
445
|
+
operationSignatures: () => [],
|
|
446
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
447
|
+
create() {
|
|
448
|
+
return {
|
|
449
|
+
familyId: 'sql' as const,
|
|
450
|
+
targetId: 'postgres' as const,
|
|
451
|
+
};
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const contract = createParamTypesTestContract({
|
|
456
|
+
tableColumns: {
|
|
457
|
+
id: { nativeType: 'int4', codecId: 'pg/int4@1', nullable: false },
|
|
458
|
+
embedding: {
|
|
459
|
+
nativeType: 'vector',
|
|
460
|
+
codecId: 'pg/vector@1',
|
|
461
|
+
nullable: false,
|
|
462
|
+
typeParams: { length: 'invalid' },
|
|
463
|
+
},
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
let thrownError: unknown;
|
|
468
|
+
try {
|
|
469
|
+
createTestContext(contract, createStubAdapter(), {
|
|
470
|
+
extensionPacks: [extensionDescriptor],
|
|
471
|
+
});
|
|
472
|
+
} catch (e) {
|
|
473
|
+
thrownError = e;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
expect(thrownError).toBeDefined();
|
|
477
|
+
expect(thrownError).toMatchObject({
|
|
478
|
+
code: 'RUNTIME.TYPE_PARAMS_INVALID',
|
|
479
|
+
category: 'RUNTIME',
|
|
480
|
+
severity: 'error',
|
|
481
|
+
details: {
|
|
482
|
+
tableName: 'test',
|
|
483
|
+
columnName: 'embedding',
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
describe('duplicate codec descriptor detection', () => {
|
|
490
|
+
it('throws RUNTIME.DUPLICATE_PARAMETERIZED_CODEC when multiple extensions provide same codecId', () => {
|
|
491
|
+
const vectorParamsSchema = arktype({
|
|
492
|
+
length: 'number',
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
function createVectorExtension(id: string): SqlRuntimeExtensionDescriptor<'postgres'> {
|
|
496
|
+
const parameterizedCodecs = [
|
|
497
|
+
{
|
|
498
|
+
codecId: 'pg/vector@1',
|
|
499
|
+
paramsSchema: vectorParamsSchema,
|
|
500
|
+
},
|
|
501
|
+
];
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
kind: 'extension' as const,
|
|
505
|
+
id,
|
|
506
|
+
version: '0.0.1',
|
|
507
|
+
familyId: 'sql' as const,
|
|
508
|
+
targetId: 'postgres' as const,
|
|
509
|
+
codecs: () => createCodecRegistry(),
|
|
510
|
+
operationSignatures: () => [],
|
|
511
|
+
parameterizedCodecs: () => parameterizedCodecs,
|
|
512
|
+
create() {
|
|
513
|
+
return {
|
|
514
|
+
familyId: 'sql' as const,
|
|
515
|
+
targetId: 'postgres' as const,
|
|
516
|
+
};
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const contract = createParamTypesTestContract();
|
|
522
|
+
|
|
523
|
+
expect(() =>
|
|
524
|
+
createTestContext(contract, createStubAdapter(), {
|
|
525
|
+
extensionPacks: [createVectorExtension('ext-1'), createVectorExtension('ext-2')],
|
|
526
|
+
}),
|
|
527
|
+
).toThrow(
|
|
528
|
+
expect.objectContaining({
|
|
529
|
+
code: 'RUNTIME.DUPLICATE_PARAMETERIZED_CODEC',
|
|
530
|
+
category: 'RUNTIME',
|
|
531
|
+
severity: 'error',
|
|
532
|
+
details: {
|
|
533
|
+
codecId: 'pg/vector@1',
|
|
534
|
+
},
|
|
535
|
+
}),
|
|
536
|
+
);
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
});
|