@prisma-next/extension-pgvector 0.5.0-dev.7 → 0.5.0-dev.70
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/README.md +5 -2
- package/dist/codec-types-CQubO6uQ.d.mts +63 -0
- package/dist/codec-types-CQubO6uQ.d.mts.map +1 -0
- package/dist/codec-types.d.mts +1 -1
- package/dist/codec-types.mjs +1 -1
- package/dist/column-types.d.mts +2 -5
- package/dist/column-types.d.mts.map +1 -1
- package/dist/column-types.mjs +4 -7
- package/dist/column-types.mjs.map +1 -1
- package/dist/{constants-Co5golCK.mjs → constants-DX-00vYk.mjs} +2 -2
- package/dist/{constants-Co5golCK.mjs.map → constants-DX-00vYk.mjs.map} +1 -1
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +4 -6
- package/dist/control.mjs.map +1 -1
- package/dist/descriptor-meta-CBnWOxms.mjs +182 -0
- package/dist/descriptor-meta-CBnWOxms.mjs.map +1 -0
- package/dist/operation-types.d.mts +57 -44
- package/dist/operation-types.d.mts.map +1 -1
- package/dist/operation-types.mjs +1 -1
- package/dist/pack.d.mts +3 -4
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +2 -3
- package/dist/runtime.d.mts +10 -1
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +6 -25
- package/dist/runtime.mjs.map +1 -1
- package/package.json +18 -16
- package/src/core/codecs.ts +127 -59
- package/src/core/descriptor-meta.ts +55 -28
- package/src/core/registry.ts +11 -0
- package/src/exports/column-types.ts +3 -6
- package/src/exports/control.ts +1 -1
- package/src/exports/runtime.ts +11 -41
- package/src/types/operation-types.ts +24 -30
- package/dist/codec-types-BifaP625.d.mts +0 -27
- package/dist/codec-types-BifaP625.d.mts.map +0 -1
- package/dist/descriptor-meta-BQbvJJxu.mjs +0 -148
- package/dist/descriptor-meta-BQbvJJxu.mjs.map +0 -1
package/dist/pack.mjs
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { t as pgvectorPackMeta } from "./descriptor-meta-
|
|
2
|
-
|
|
3
|
-
export { pgvectorPackMeta as default };
|
|
1
|
+
import { t as pgvectorPackMeta } from "./descriptor-meta-CBnWOxms.mjs";
|
|
2
|
+
export { pgvectorPackMeta as default };
|
package/dist/runtime.d.mts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { SqlRuntimeExtensionDescriptor } from "@prisma-next/sql-runtime";
|
|
2
|
+
import { CodecDescriptorRegistry } from "@prisma-next/sql-relational-core/query-lane-context";
|
|
2
3
|
|
|
4
|
+
//#region src/core/registry.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.
|
|
7
|
+
*
|
|
8
|
+
* Public consumer surface for the pgvector codec set. Currently a single entry (`pg/vector@1`); the registry shape stays consistent with the other codec-shipping packages so consumers don't need to special-case extensions. See ADR 208.
|
|
9
|
+
*/
|
|
10
|
+
declare const pgvectorCodecRegistry: CodecDescriptorRegistry;
|
|
11
|
+
//#endregion
|
|
3
12
|
//#region src/exports/runtime.d.ts
|
|
4
13
|
declare const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'>;
|
|
5
14
|
//#endregion
|
|
6
|
-
export { pgvectorRuntimeDescriptor as default };
|
|
15
|
+
export { pgvectorRuntimeDescriptor as default, pgvectorCodecRegistry };
|
|
7
16
|
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/core/registry.ts","../src/exports/runtime.ts"],"mappings":";;;;;;;AASA;;cAAa,qBAAA,EAAuB,uBAAA;;;cCL9B,yBAAA,EAA2B,6BAAA"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,32 +1,14 @@
|
|
|
1
|
-
import { n as
|
|
2
|
-
import { n as pgvectorQueryOperations, r as codecDefinitions, t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
|
|
3
|
-
import { createCodecRegistry } from "@prisma-next/sql-relational-core/ast";
|
|
4
|
-
import { type } from "arktype";
|
|
5
|
-
|
|
1
|
+
import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-CBnWOxms.mjs";
|
|
6
2
|
//#region src/exports/runtime.ts
|
|
7
|
-
const parameterizedCodecDescriptors = [{
|
|
8
|
-
codecId: VECTOR_CODEC_ID,
|
|
9
|
-
paramsSchema: type({ length: "number" }).narrow((params, ctx) => {
|
|
10
|
-
const { length } = params;
|
|
11
|
-
if (!Number.isInteger(length)) return ctx.mustBe("an integer");
|
|
12
|
-
if (length < 1 || length > VECTOR_MAX_DIM) return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
|
|
13
|
-
return true;
|
|
14
|
-
})
|
|
15
|
-
}];
|
|
16
|
-
function createPgvectorCodecRegistry() {
|
|
17
|
-
const registry = createCodecRegistry();
|
|
18
|
-
for (const def of Object.values(codecDefinitions)) registry.register(def.codec);
|
|
19
|
-
return registry;
|
|
20
|
-
}
|
|
21
3
|
const pgvectorRuntimeDescriptor = {
|
|
22
4
|
kind: "extension",
|
|
23
5
|
id: pgvectorPackMeta.id,
|
|
24
6
|
version: pgvectorPackMeta.version,
|
|
25
7
|
familyId: "sql",
|
|
26
8
|
targetId: "postgres",
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
9
|
+
types: { codecTypes: { codecDescriptors: Array.from(pgvectorCodecRegistry.values()) } },
|
|
10
|
+
codecs: () => Array.from(pgvectorCodecRegistry.values()),
|
|
11
|
+
queryOperations: () => pgvectorQueryOperations(),
|
|
30
12
|
create() {
|
|
31
13
|
return {
|
|
32
14
|
familyId: "sql",
|
|
@@ -34,8 +16,7 @@ const pgvectorRuntimeDescriptor = {
|
|
|
34
16
|
};
|
|
35
17
|
}
|
|
36
18
|
};
|
|
37
|
-
var runtime_default = pgvectorRuntimeDescriptor;
|
|
38
|
-
|
|
39
19
|
//#endregion
|
|
40
|
-
export {
|
|
20
|
+
export { pgvectorRuntimeDescriptor as default, pgvectorCodecRegistry };
|
|
21
|
+
|
|
41
22
|
//# sourceMappingURL=runtime.mjs.map
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":[
|
|
1
|
+
{"version":3,"file":"runtime.mjs","names":[],"sources":["../src/exports/runtime.ts"],"sourcesContent":["import type { SqlRuntimeExtensionDescriptor } from '@prisma-next/sql-runtime';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\nimport { pgvectorCodecRegistry } from '../core/registry';\n\nconst pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {\n kind: 'extension' as const,\n id: pgvectorPackMeta.id,\n version: pgvectorPackMeta.version,\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n // Expose the unified descriptor list so `extractCodecLookup` reads `targetTypes` / `meta` / `renderOutputType` directly off the descriptors and materializes the representative `Codec` for the SQL renderer's cast-policy lookup.\n types: {\n codecTypes: {\n codecDescriptors: Array.from(pgvectorCodecRegistry.values()),\n },\n },\n codecs: () => Array.from(pgvectorCodecRegistry.values()),\n queryOperations: () => pgvectorQueryOperations(),\n create() {\n return {\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n };\n },\n};\n\nexport { pgvectorCodecRegistry };\nexport default pgvectorRuntimeDescriptor;\n"],"mappings":";;AAIA,MAAM,4BAAuE;CAC3E,MAAM;CACN,IAAI,iBAAiB;CACrB,SAAS,iBAAiB;CAC1B,UAAU;CACV,UAAU;CAEV,OAAO,EACL,YAAY,EACV,kBAAkB,MAAM,KAAK,sBAAsB,QAAQ,CAAC,EAC7D,EACF;CACD,cAAc,MAAM,KAAK,sBAAsB,QAAQ,CAAC;CACxD,uBAAuB,yBAAyB;CAChD,SAAS;EACP,OAAO;GACL,UAAU;GACV,UAAU;GACX;;CAEJ"}
|
package/package.json
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/extension-pgvector",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
3
|
+
"version": "0.5.0-dev.70",
|
|
4
|
+
"license": "Apache-2.0",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"sideEffects": false,
|
|
6
7
|
"dependencies": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"@prisma-next/
|
|
10
|
-
"@prisma-next/sql
|
|
11
|
-
"@prisma-next/
|
|
12
|
-
"@prisma-next/
|
|
13
|
-
"@prisma-next/sql-
|
|
14
|
-
"@prisma-next/
|
|
15
|
-
"@prisma-next/sql-schema-ir": "0.5.0-dev.
|
|
8
|
+
"@standard-schema/spec": "^1.1.0",
|
|
9
|
+
"arktype": "^2.1.29",
|
|
10
|
+
"@prisma-next/contract": "0.5.0-dev.70",
|
|
11
|
+
"@prisma-next/family-sql": "0.5.0-dev.70",
|
|
12
|
+
"@prisma-next/sql-relational-core": "0.5.0-dev.70",
|
|
13
|
+
"@prisma-next/contract-authoring": "0.5.0-dev.70",
|
|
14
|
+
"@prisma-next/sql-operations": "0.5.0-dev.70",
|
|
15
|
+
"@prisma-next/framework-components": "0.5.0-dev.70",
|
|
16
|
+
"@prisma-next/sql-schema-ir": "0.5.0-dev.70",
|
|
17
|
+
"@prisma-next/sql-runtime": "0.5.0-dev.70"
|
|
16
18
|
},
|
|
17
19
|
"devDependencies": {
|
|
18
|
-
"tsdown": "0.
|
|
20
|
+
"tsdown": "0.22.0",
|
|
19
21
|
"typescript": "5.9.3",
|
|
20
|
-
"vitest": "4.
|
|
21
|
-
"@prisma-next/operations": "0.5.0-dev.
|
|
22
|
-
"@prisma-next/sql-contract": "0.5.0-dev.
|
|
22
|
+
"vitest": "4.1.5",
|
|
23
|
+
"@prisma-next/operations": "0.5.0-dev.70",
|
|
24
|
+
"@prisma-next/sql-contract": "0.5.0-dev.70",
|
|
23
25
|
"@prisma-next/test-utils": "0.0.1",
|
|
24
|
-
"@prisma-next/sql-contract-ts": "0.5.0-dev.7",
|
|
25
26
|
"@prisma-next/tsconfig": "0.0.0",
|
|
26
|
-
"@prisma-next/tsdown": "0.0.0"
|
|
27
|
+
"@prisma-next/tsdown": "0.0.0",
|
|
28
|
+
"@prisma-next/sql-contract-ts": "0.5.0-dev.70"
|
|
27
29
|
},
|
|
28
30
|
"files": [
|
|
29
31
|
"dist",
|
package/src/core/codecs.ts
CHANGED
|
@@ -1,77 +1,145 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* pgvector extension codec.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Mirrors the patterns in `postgres/codecs-class.ts` and `sqlite/codecs-class.ts` for the single `pg/vector@1` codec. Three artifacts:
|
|
5
|
+
*
|
|
6
|
+
* 1. `PgVectorCodec` extends {@link CodecImpl} with the runtime encode/decode/encodeJson/decodeJson conversions inline. Conversions are simple enough (PostgreSQL `[1,2,3]` text format) that no shared helper module is warranted; the class body is the source of truth.
|
|
7
|
+
* 2. `PgVectorDescriptor` extends {@link CodecDescriptorImpl} with the codec id, traits, target types, params schema (`{ length: number }`, validated against {@link VECTOR_MAX_DIM}), `meta` (postgres `nativeType: 'vector'`), and the emit-path `renderOutputType` producing `Vector<${length}>`.
|
|
8
|
+
* 3. `pgVectorColumn(length)` per-codec column helper invoking `descriptor.factory({ length })` directly + passing the bare `nativeType: 'vector'`. The family-layer {@link expandNativeType} hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.
|
|
9
|
+
*
|
|
10
|
+
* `length` threads into the runtime codec via the constructor so encode/decode/encodeJson/decodeJson enforce the declared dimension at every ingress path. Without this, `vector(3)` and `vector(1536)` would produce codecs with identical behaviour and a dimension-mismatched value would round-trip undetected.
|
|
6
11
|
*/
|
|
7
12
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
import type { JsonValue } from '@prisma-next/contract/types';
|
|
14
|
+
import {
|
|
15
|
+
type AnyCodecDescriptor,
|
|
16
|
+
type CodecCallContext,
|
|
17
|
+
CodecDescriptorImpl,
|
|
18
|
+
CodecImpl,
|
|
19
|
+
type CodecInstanceContext,
|
|
20
|
+
type ColumnHelperFor,
|
|
21
|
+
type ColumnHelperForStrict,
|
|
22
|
+
column,
|
|
23
|
+
} from '@prisma-next/framework-components/codec';
|
|
24
|
+
import type { ExtractCodecTypes } from '@prisma-next/sql-relational-core/ast';
|
|
25
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
26
|
+
import { type as arktype } from 'arktype';
|
|
27
|
+
import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from './constants';
|
|
28
|
+
|
|
29
|
+
type VectorParams = { readonly length: number };
|
|
30
|
+
|
|
31
|
+
const vectorParamsSchema = arktype({
|
|
32
|
+
length: 'number',
|
|
33
|
+
}).narrow((params, ctx) => {
|
|
34
|
+
const { length } = params;
|
|
35
|
+
if (!Number.isInteger(length)) {
|
|
36
|
+
return ctx.mustBe('an integer');
|
|
37
|
+
}
|
|
38
|
+
if (length < 1 || length > VECTOR_MAX_DIM) {
|
|
39
|
+
return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}) satisfies StandardSchemaV1<VectorParams>;
|
|
43
|
+
|
|
44
|
+
const PG_VECTOR_META = { db: { sql: { postgres: { nativeType: 'vector' } } } } as const;
|
|
45
|
+
|
|
46
|
+
export class PgVectorCodec extends CodecImpl<
|
|
47
|
+
typeof VECTOR_CODEC_ID,
|
|
48
|
+
readonly ['equality'],
|
|
49
|
+
string,
|
|
50
|
+
number[]
|
|
51
|
+
> {
|
|
52
|
+
readonly length: number | undefined;
|
|
53
|
+
|
|
54
|
+
constructor(descriptor: AnyCodecDescriptor, length: number | undefined) {
|
|
55
|
+
super(descriptor);
|
|
56
|
+
this.length = length;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
assertVector(value: unknown): asserts value is number[] {
|
|
26
60
|
if (!Array.isArray(value)) {
|
|
27
61
|
throw new Error('Vector value must be an array of numbers');
|
|
28
62
|
}
|
|
29
63
|
if (!value.every((v) => typeof v === 'number')) {
|
|
30
64
|
throw new Error('Vector value must contain only numbers');
|
|
31
65
|
}
|
|
32
|
-
|
|
33
|
-
|
|
66
|
+
if (this.length !== undefined && value.length !== this.length) {
|
|
67
|
+
throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async encode(value: number[], _ctx: CodecCallContext): Promise<string> {
|
|
72
|
+
this.assertVector(value);
|
|
34
73
|
return `[${value.join(',')}]`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {
|
|
38
77
|
if (typeof wire !== 'string') {
|
|
39
78
|
throw new Error('Vector wire value must be a string');
|
|
40
79
|
}
|
|
41
|
-
// Parse PostgreSQL vector format: [1,2,3]
|
|
42
80
|
if (!wire.startsWith('[') || !wire.endsWith(']')) {
|
|
43
81
|
throw new Error(`Invalid vector format: expected "[...]", got "${wire}"`);
|
|
44
82
|
}
|
|
45
83
|
const content = wire.slice(1, -1).trim();
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
84
|
+
const parsed =
|
|
85
|
+
content === ''
|
|
86
|
+
? []
|
|
87
|
+
: content.split(',').map((v) => {
|
|
88
|
+
const num = Number.parseFloat(v.trim());
|
|
89
|
+
if (Number.isNaN(num)) {
|
|
90
|
+
throw new Error(`Invalid vector value: "${v}" is not a number`);
|
|
91
|
+
}
|
|
92
|
+
return num;
|
|
93
|
+
});
|
|
94
|
+
this.assertVector(parsed);
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
encodeJson(value: number[]): JsonValue {
|
|
99
|
+
this.assertVector(value);
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
decodeJson(json: JsonValue): number[] {
|
|
104
|
+
this.assertVector(json);
|
|
105
|
+
return json;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {
|
|
110
|
+
override readonly codecId = VECTOR_CODEC_ID;
|
|
111
|
+
override readonly traits = ['equality'] as const;
|
|
112
|
+
override readonly targetTypes = ['vector'] as const;
|
|
113
|
+
override readonly meta = PG_VECTOR_META;
|
|
114
|
+
override readonly paramsSchema: StandardSchemaV1<VectorParams> = vectorParamsSchema;
|
|
115
|
+
override renderOutputType(params: VectorParams): string {
|
|
116
|
+
return `Vector<${params.length}>`;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The runtime calls `factory(undefined)(ctx)` to materialize a representative codec for parameterized descriptors that ship a no-params column variant (here, `vectorColumn` vs `vector(N)`). The runtime cast widens `params` to `unknown`, so guarding with an optional read keeps the typed call site (`factory({ length })`) strict while still producing a length-agnostic codec for representative use. Encode/decode for an undimensioned column run through this representative; the wire format `[v1,v2,...]` is dimension-independent.
|
|
120
|
+
*/
|
|
121
|
+
override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {
|
|
122
|
+
return () => new PgVectorCodec(this, (params as VectorParams | undefined)?.length);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export const pgVectorDescriptor = new PgVectorDescriptor();
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Per-codec column helper for `pg/vector@1`. Generic over `N extends number` so the column site preserves the dimension literal in `typeParams` (e.g. `pgVectorColumn(1536)` packs `typeParams: { length: 1536 }`).
|
|
130
|
+
*
|
|
131
|
+
* Passes the bare `nativeType: 'vector'`; the family-layer `expandNativeType` hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.
|
|
132
|
+
*/
|
|
133
|
+
export const pgVectorColumn = <N extends number>(length: N) =>
|
|
134
|
+
column(pgVectorDescriptor.factory({ length }), pgVectorDescriptor.codecId, { length }, 'vector');
|
|
135
|
+
|
|
136
|
+
pgVectorColumn satisfies ColumnHelperFor<PgVectorDescriptor>;
|
|
137
|
+
pgVectorColumn satisfies ColumnHelperForStrict<PgVectorDescriptor>;
|
|
138
|
+
|
|
139
|
+
const codecDescriptorMap = {
|
|
140
|
+
vector: pgVectorDescriptor,
|
|
141
|
+
} as const;
|
|
142
|
+
|
|
143
|
+
export type CodecTypes = ExtractCodecTypes<typeof codecDescriptorMap>;
|
|
144
|
+
|
|
145
|
+
export const codecDescriptors: readonly AnyCodecDescriptor[] = Object.values(codecDescriptorMap);
|
|
@@ -1,38 +1,65 @@
|
|
|
1
1
|
import type { SqlOperationDescriptor } from '@prisma-next/sql-operations';
|
|
2
|
+
import {
|
|
3
|
+
buildOperation,
|
|
4
|
+
type CodecExpression,
|
|
5
|
+
type Expression,
|
|
6
|
+
refsOf,
|
|
7
|
+
toExpr,
|
|
8
|
+
} from '@prisma-next/sql-relational-core/expression';
|
|
2
9
|
import type { CodecTypes } from '../types/codec-types';
|
|
3
10
|
import { pgvectorAuthoringTypes } from './authoring';
|
|
4
|
-
import {
|
|
11
|
+
import { pgvectorCodecRegistry } from './registry';
|
|
5
12
|
|
|
6
13
|
const pgvectorTypeId = 'pg/vector@1' as const;
|
|
7
14
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
type CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;
|
|
16
|
+
|
|
17
|
+
export function pgvectorQueryOperations<
|
|
18
|
+
CT extends CodecTypesBase,
|
|
19
|
+
>(): readonly SqlOperationDescriptor[] {
|
|
20
|
+
return [
|
|
21
|
+
{
|
|
22
|
+
method: 'cosineDistance',
|
|
23
|
+
self: { codecId: pgvectorTypeId },
|
|
24
|
+
impl: (
|
|
25
|
+
self: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
26
|
+
other: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
27
|
+
): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {
|
|
28
|
+
const selfRefs = refsOf(self);
|
|
29
|
+
return buildOperation({
|
|
30
|
+
method: 'cosineDistance',
|
|
31
|
+
args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
|
|
32
|
+
returns: { codecId: 'pg/float8@1', nullable: false },
|
|
33
|
+
lowering: {
|
|
34
|
+
targetFamily: 'sql',
|
|
35
|
+
strategy: 'function',
|
|
36
|
+
template: '{{self}} <=> {{arg0}}',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
},
|
|
20
40
|
},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
{
|
|
42
|
+
method: 'cosineSimilarity',
|
|
43
|
+
self: { codecId: pgvectorTypeId },
|
|
44
|
+
impl: (
|
|
45
|
+
self: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
46
|
+
other: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
47
|
+
): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {
|
|
48
|
+
const selfRefs = refsOf(self);
|
|
49
|
+
return buildOperation({
|
|
50
|
+
method: 'cosineSimilarity',
|
|
51
|
+
args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
|
|
52
|
+
returns: { codecId: 'pg/float8@1', nullable: false },
|
|
53
|
+
lowering: {
|
|
54
|
+
targetFamily: 'sql',
|
|
55
|
+
strategy: 'function',
|
|
56
|
+
template: '1 - ({{self}} <=> {{arg0}})',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
},
|
|
33
60
|
},
|
|
34
|
-
|
|
35
|
-
|
|
61
|
+
];
|
|
62
|
+
}
|
|
36
63
|
|
|
37
64
|
const pgvectorPackMetaBase = {
|
|
38
65
|
kind: 'extension',
|
|
@@ -50,7 +77,7 @@ const pgvectorPackMetaBase = {
|
|
|
50
77
|
},
|
|
51
78
|
types: {
|
|
52
79
|
codecTypes: {
|
|
53
|
-
|
|
80
|
+
codecDescriptors: Array.from(pgvectorCodecRegistry.values()),
|
|
54
81
|
import: {
|
|
55
82
|
package: '@prisma-next/extension-pgvector/codec-types',
|
|
56
83
|
named: 'CodecTypes',
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { buildCodecDescriptorRegistry } from '@prisma-next/sql-relational-core/codec-descriptor-registry';
|
|
2
|
+
import type { CodecDescriptorRegistry } from '@prisma-next/sql-relational-core/query-lane-context';
|
|
3
|
+
import { codecDescriptors } from './codecs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.
|
|
7
|
+
*
|
|
8
|
+
* Public consumer surface for the pgvector codec set. Currently a single entry (`pg/vector@1`); the registry shape stays consistent with the other codec-shipping packages so consumers don't need to special-case extensions. See ADR 208.
|
|
9
|
+
*/
|
|
10
|
+
export const pgvectorCodecRegistry: CodecDescriptorRegistry =
|
|
11
|
+
buildCodecDescriptorRegistry(codecDescriptors);
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Column type descriptors for pgvector extension.
|
|
3
3
|
*
|
|
4
|
-
* These descriptors provide both codecId and nativeType for use in contract authoring.
|
|
5
|
-
* They are derived from the same source of truth as codec definitions and manifests.
|
|
4
|
+
* These descriptors provide both codecId and nativeType for use in contract authoring. They are derived from the same source of truth as codec definitions and manifests.
|
|
6
5
|
*/
|
|
7
6
|
|
|
8
|
-
import type { ColumnTypeDescriptor } from '@prisma-next/
|
|
7
|
+
import type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';
|
|
9
8
|
import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
* Static vector column descriptor without dimension.
|
|
13
|
-
* Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
11
|
+
* Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
14
12
|
*/
|
|
15
13
|
export const vectorColumn = {
|
|
16
14
|
codecId: VECTOR_CODEC_ID,
|
|
@@ -25,7 +23,6 @@ export const vectorColumn = {
|
|
|
25
23
|
* .column('embedding', { type: vector(1536), nullable: false })
|
|
26
24
|
* // Produces: nativeType: 'vector', typeParams: { length: 1536 }
|
|
27
25
|
* ```
|
|
28
|
-
*
|
|
29
26
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
30
27
|
* @returns A column type descriptor with `typeParams.length` set
|
|
31
28
|
* @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]
|
package/src/exports/control.ts
CHANGED
|
@@ -75,7 +75,7 @@ const pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'> = {
|
|
|
75
75
|
},
|
|
76
76
|
},
|
|
77
77
|
},
|
|
78
|
-
queryOperations: () => pgvectorQueryOperations,
|
|
78
|
+
queryOperations: () => pgvectorQueryOperations(),
|
|
79
79
|
databaseDependencies: pgvectorDatabaseDependencies,
|
|
80
80
|
create: () => ({
|
|
81
81
|
familyId: 'sql' as const,
|
package/src/exports/runtime.ts
CHANGED
|
@@ -1,42 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
RuntimeParameterizedCodecDescriptor,
|
|
4
|
-
SqlRuntimeExtensionDescriptor,
|
|
5
|
-
} from '@prisma-next/sql-runtime';
|
|
6
|
-
import { type as arktype } from 'arktype';
|
|
7
|
-
import { codecDefinitions } from '../core/codecs';
|
|
8
|
-
import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
|
|
1
|
+
import type { SqlRuntimeExtensionDescriptor } from '@prisma-next/sql-runtime';
|
|
9
2
|
import { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';
|
|
10
|
-
|
|
11
|
-
const vectorParamsSchema = arktype({
|
|
12
|
-
length: 'number',
|
|
13
|
-
}).narrow((params, ctx) => {
|
|
14
|
-
const { length } = params;
|
|
15
|
-
if (!Number.isInteger(length)) {
|
|
16
|
-
return ctx.mustBe('an integer');
|
|
17
|
-
}
|
|
18
|
-
if (length < 1 || length > VECTOR_MAX_DIM) {
|
|
19
|
-
return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
|
|
20
|
-
}
|
|
21
|
-
return true;
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const parameterizedCodecDescriptors = [
|
|
25
|
-
{
|
|
26
|
-
codecId: VECTOR_CODEC_ID,
|
|
27
|
-
paramsSchema: vectorParamsSchema,
|
|
28
|
-
},
|
|
29
|
-
] as const satisfies ReadonlyArray<
|
|
30
|
-
RuntimeParameterizedCodecDescriptor<{ readonly length: number }>
|
|
31
|
-
>;
|
|
32
|
-
|
|
33
|
-
function createPgvectorCodecRegistry() {
|
|
34
|
-
const registry = createCodecRegistry();
|
|
35
|
-
for (const def of Object.values(codecDefinitions)) {
|
|
36
|
-
registry.register(def.codec);
|
|
37
|
-
}
|
|
38
|
-
return registry;
|
|
39
|
-
}
|
|
3
|
+
import { pgvectorCodecRegistry } from '../core/registry';
|
|
40
4
|
|
|
41
5
|
const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
42
6
|
kind: 'extension' as const,
|
|
@@ -44,9 +8,14 @@ const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
|
44
8
|
version: pgvectorPackMeta.version,
|
|
45
9
|
familyId: 'sql' as const,
|
|
46
10
|
targetId: 'postgres' as const,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
11
|
+
// Expose the unified descriptor list so `extractCodecLookup` reads `targetTypes` / `meta` / `renderOutputType` directly off the descriptors and materializes the representative `Codec` for the SQL renderer's cast-policy lookup.
|
|
12
|
+
types: {
|
|
13
|
+
codecTypes: {
|
|
14
|
+
codecDescriptors: Array.from(pgvectorCodecRegistry.values()),
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
codecs: () => Array.from(pgvectorCodecRegistry.values()),
|
|
18
|
+
queryOperations: () => pgvectorQueryOperations(),
|
|
50
19
|
create() {
|
|
51
20
|
return {
|
|
52
21
|
familyId: 'sql' as const,
|
|
@@ -55,4 +24,5 @@ const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
|
|
|
55
24
|
},
|
|
56
25
|
};
|
|
57
26
|
|
|
27
|
+
export { pgvectorCodecRegistry };
|
|
58
28
|
export default pgvectorRuntimeDescriptor;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { SqlQueryOperationTypes } from '@prisma-next/sql-contract/types';
|
|
2
|
+
import type { CodecExpression, Expression } from '@prisma-next/sql-relational-core/expression';
|
|
3
|
+
|
|
4
|
+
type CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
7
|
* Operation type definitions for pgvector extension.
|
|
@@ -10,40 +13,31 @@ import type { SqlQueryOperationTypes } from '@prisma-next/sql-contract/types';
|
|
|
10
13
|
export type OperationTypes = {
|
|
11
14
|
readonly 'pg/vector@1': {
|
|
12
15
|
readonly cosineDistance: {
|
|
13
|
-
readonly
|
|
14
|
-
readonly returns: { readonly codecId: 'pg/float8@1'; readonly nullable: false };
|
|
15
|
-
readonly lowering: {
|
|
16
|
-
readonly targetFamily: 'sql';
|
|
17
|
-
readonly strategy: 'function';
|
|
18
|
-
readonly template: string;
|
|
19
|
-
};
|
|
16
|
+
readonly self: { readonly codecId: 'pg/vector@1' };
|
|
20
17
|
};
|
|
21
18
|
readonly cosineSimilarity: {
|
|
22
|
-
readonly
|
|
23
|
-
readonly returns: { readonly codecId: 'pg/float8@1'; readonly nullable: false };
|
|
24
|
-
readonly lowering: {
|
|
25
|
-
readonly targetFamily: 'sql';
|
|
26
|
-
readonly strategy: 'function';
|
|
27
|
-
readonly template: string;
|
|
28
|
-
};
|
|
19
|
+
readonly self: { readonly codecId: 'pg/vector@1' };
|
|
29
20
|
};
|
|
30
21
|
};
|
|
31
22
|
};
|
|
32
23
|
|
|
33
24
|
/** Flat operation signatures for the query builder. */
|
|
34
|
-
export type QueryOperationTypes = SqlQueryOperationTypes<
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
{ readonly codecId: 'pg/vector@1'
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
{ readonly codecId: 'pg/vector@1'
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}>;
|
|
25
|
+
export type QueryOperationTypes<CT extends CodecTypesBase> = SqlQueryOperationTypes<
|
|
26
|
+
CT,
|
|
27
|
+
{
|
|
28
|
+
readonly cosineDistance: {
|
|
29
|
+
readonly self: { readonly codecId: 'pg/vector@1' };
|
|
30
|
+
readonly impl: (
|
|
31
|
+
self: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
32
|
+
other: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
33
|
+
) => Expression<{ codecId: 'pg/float8@1'; nullable: false }>;
|
|
34
|
+
};
|
|
35
|
+
readonly cosineSimilarity: {
|
|
36
|
+
readonly self: { readonly codecId: 'pg/vector@1' };
|
|
37
|
+
readonly impl: (
|
|
38
|
+
self: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
39
|
+
other: CodecExpression<'pg/vector@1', boolean, CT>,
|
|
40
|
+
) => Expression<{ codecId: 'pg/float8@1'; nullable: false }>;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
>;
|