@prisma-next/extension-pgvector 0.3.0-dev.32 → 0.3.0-dev.33

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 CHANGED
@@ -8,7 +8,7 @@ This extension pack adds support for the `vector` data type and vector similarit
8
8
 
9
9
  ## Responsibilities
10
10
 
11
- - **Vector Codec**: Provides codec for `pg/vector@1` type ID mapping to `number[]` JavaScript type
11
+ - **Vector Codec**: Provides codec for `pg/vector@1` mapping to `number[]` at runtime, and a `Vector<N>` type for dimensioned typing in `contract.d.ts`
12
12
  - **Vector Operations**: Registers vector similarity operations (e.g., `cosineDistance`) for use in queries
13
13
  - **CLI Integration**: Provides extension descriptor for `prisma-next.config.ts` configuration
14
14
  - **Runtime Extension**: Registers codecs and operations at runtime for vector column operations
@@ -135,12 +135,16 @@ type Row = ResultType<typeof plan>;
135
135
 
136
136
  ### Codec Types
137
137
 
138
- The extension provides a `CodecTypes` export mapping the `pg/vector@1` type ID to `number[]`:
138
+ The extension provides:
139
+
140
+ - `CodecTypes` mapping the `pg/vector@1` type ID to `number[]` (runtime representation)
141
+ - `Vector<N>` type for dimensioned vector typing in emitted `contract.d.ts` and schema result types when the contract includes dimension metadata
139
142
 
140
143
  ```typescript
141
- import type { CodecTypes } from '@prisma-next/extension-pgvector/codec-types';
144
+ import type { CodecTypes, Vector } from '@prisma-next/extension-pgvector/codec-types';
142
145
 
143
146
  // CodecTypes['pg/vector@1']['output'] = number[]
147
+ // Vector<1536> is a branded number[] type used for dimensioned typing
144
148
  ```
145
149
 
146
150
  ### Operation Types
@@ -29,10 +29,16 @@ var pgvectorPackMeta = {
29
29
  named: "CodecTypes",
30
30
  alias: "PgVectorTypes"
31
31
  },
32
+ typeImports: [
33
+ {
34
+ package: "@prisma-next/extension-pgvector/codec-types",
35
+ named: "Vector",
36
+ alias: "Vector"
37
+ }
38
+ ],
32
39
  // Parameterized codec renderers for type emission.
33
40
  // The renderer template produces precise TypeScript types like Vector<1536>
34
41
  // when columns have typeParams with a `length` property.
35
- // Note: The Vector<N> type import is deferred to Phase 6.
36
42
  parameterized: {
37
43
  [pgvectorTypeId]: "Vector<{{length}}>"
38
44
  }
@@ -64,4 +70,4 @@ export {
64
70
  pgvectorPackMeta,
65
71
  pgvectorRuntimeOperation
66
72
  };
67
- //# sourceMappingURL=chunk-OI5XEHIN.js.map
73
+ //# sourceMappingURL=chunk-DSJTFXJR.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":["import type { ExtensionPackRef } from '@prisma-next/contract/framework-components';\nimport type { SqlOperationSignature } from '@prisma-next/sql-operations';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\nconst cosineLowering = {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n} as const;\n\n/**\n * Shared operation definition used by both pack metadata and runtime descriptor.\n * Frozen to prevent accidental mutation.\n */\nconst cosineDistanceOperation = Object.freeze({\n method: 'cosineDistance',\n args: [{ kind: 'param' }],\n returns: { kind: 'builtin', type: 'number' },\n lowering: cosineLowering,\n} as const);\n\nexport const pgvectorPackMeta = {\n kind: 'extension',\n id: 'pgvector',\n familyId: 'sql',\n targetId: 'postgres',\n version: '0.0.1',\n capabilities: {\n postgres: {\n 'pgvector/cosine': true,\n },\n },\n types: {\n codecTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'CodecTypes',\n alias: 'PgVectorTypes',\n },\n typeImports: [\n {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'Vector',\n alias: 'Vector',\n },\n ],\n // Parameterized codec renderers for type emission.\n // The renderer template produces precise TypeScript types like Vector<1536>\n // when columns have typeParams with a `length` property.\n parameterized: {\n [pgvectorTypeId]: 'Vector<{{length}}>',\n },\n },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n operations: [\n {\n for: pgvectorTypeId,\n ...cosineDistanceOperation,\n },\n ],\n} as const satisfies ExtensionPackRef<'sql', 'postgres'>;\n\nexport const pgvectorRuntimeOperation: SqlOperationSignature = {\n forTypeId: pgvectorTypeId,\n ...cosineDistanceOperation,\n};\n"],"mappings":";AAGA,IAAM,iBAAiB;AAEvB,IAAM,iBAAiB;AAAA,EACrB,cAAc;AAAA,EACd,UAAU;AAAA,EACV,UAAU;AACZ;AAMA,IAAM,0BAA0B,OAAO,OAAO;AAAA,EAC5C,QAAQ;AAAA,EACR,MAAM,CAAC,EAAE,MAAM,QAAQ,CAAC;AAAA,EACxB,SAAS,EAAE,MAAM,WAAW,MAAM,SAAS;AAAA,EAC3C,UAAU;AACZ,CAAU;AAEH,IAAM,mBAAmB;AAAA,EAC9B,MAAM;AAAA,EACN,IAAI;AAAA,EACJ,UAAU;AAAA,EACV,UAAU;AAAA,EACV,SAAS;AAAA,EACT,cAAc;AAAA,IACZ,UAAU;AAAA,MACR,mBAAmB;AAAA,IACrB;AAAA,EACF;AAAA,EACA,OAAO;AAAA,IACL,YAAY;AAAA,MACV,QAAQ;AAAA,QACN,SAAS;AAAA,QACT,OAAO;AAAA,QACP,OAAO;AAAA,MACT;AAAA,MACA,aAAa;AAAA,QACX;AAAA,UACE,SAAS;AAAA,UACT,OAAO;AAAA,UACP,OAAO;AAAA,QACT;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAIA,eAAe;AAAA,QACb,CAAC,cAAc,GAAG;AAAA,MACpB;AAAA,IACF;AAAA,IACA,gBAAgB;AAAA,MACd,QAAQ;AAAA,QACN,SAAS;AAAA,QACT,OAAO;AAAA,QACP,OAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,SAAS;AAAA,MACP,EAAE,QAAQ,gBAAgB,UAAU,OAAO,UAAU,YAAY,YAAY,SAAS;AAAA,IACxF;AAAA,EACF;AAAA,EACA,YAAY;AAAA,IACV;AAAA,MACE,KAAK;AAAA,MACL,GAAG;AAAA,IACL;AAAA,EACF;AACF;AAEO,IAAM,2BAAkD;AAAA,EAC7D,WAAW;AAAA,EACX,GAAG;AACL;","names":[]}
@@ -0,0 +1,9 @@
1
+ // src/core/constants.ts
2
+ var VECTOR_CODEC_ID = "pg/vector@1";
3
+ var VECTOR_MAX_DIM = 16e3;
4
+
5
+ export {
6
+ VECTOR_CODEC_ID,
7
+ VECTOR_MAX_DIM
8
+ };
9
+ //# sourceMappingURL=chunk-MUHUVAQX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/constants.ts"],"sourcesContent":["/**\n * Codec ID for pgvector's vector type.\n */\nexport const VECTOR_CODEC_ID = 'pg/vector@1' as const;\n\n/**\n * Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).\n */\nexport const VECTOR_MAX_DIM = 16000;\n"],"mappings":";AAGO,IAAM,kBAAkB;AAKxB,IAAM,iBAAiB;","names":[]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Codec ID for pgvector's vector type.
3
+ */
4
+ export declare const VECTOR_CODEC_ID: "pg/vector@1";
5
+ /**
6
+ * Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).
7
+ */
8
+ export declare const VECTOR_MAX_DIM = 16000;
9
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/core/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,eAAe,EAAG,aAAsB,CAAC;AAEtD;;GAEG;AACH,eAAO,MAAM,cAAc,QAAQ,CAAC"}
@@ -17,6 +17,11 @@ export declare const pgvectorPackMeta: {
17
17
  readonly named: "CodecTypes";
18
18
  readonly alias: "PgVectorTypes";
19
19
  };
20
+ readonly typeImports: readonly [{
21
+ readonly package: "@prisma-next/extension-pgvector/codec-types";
22
+ readonly named: "Vector";
23
+ readonly alias: "Vector";
24
+ }];
20
25
  readonly parameterized: {
21
26
  readonly "pg/vector@1": "Vector<{{length}}>";
22
27
  };
@@ -1 +1 @@
1
- {"version":3,"file":"descriptor-meta.d.ts","sourceRoot":"","sources":["../../src/core/descriptor-meta.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAqBzE,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2C2B,CAAC;AAEzD,eAAO,MAAM,wBAAwB,EAAE,qBAGtC,CAAC"}
1
+ {"version":3,"file":"descriptor-meta.d.ts","sourceRoot":"","sources":["../../src/core/descriptor-meta.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAqBzE,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiD2B,CAAC;AAEzD,eAAO,MAAM,wBAAwB,EAAE,qBAGtC,CAAC"}
@@ -3,5 +3,5 @@
3
3
  *
4
4
  * Re-export from types module for public API.
5
5
  */
6
- export type { CodecTypes } from '../types/codec-types';
6
+ export type { CodecTypes, Vector } from '../types/codec-types';
7
7
  //# sourceMappingURL=codec-types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec-types.d.ts","sourceRoot":"","sources":["../../src/exports/codec-types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"codec-types.d.ts","sourceRoot":"","sources":["../../src/exports/codec-types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC"}
@@ -4,8 +4,31 @@
4
4
  * These descriptors provide both codecId and nativeType for use in contract authoring.
5
5
  * They are derived from the same source of truth as codec definitions and manifests.
6
6
  */
7
+ import type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';
8
+ /**
9
+ * Static vector column descriptor without dimension.
10
+ * Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
11
+ */
7
12
  export declare const vectorColumn: {
8
13
  readonly codecId: "pg/vector@1";
9
14
  readonly nativeType: "vector";
10
15
  };
16
+ /**
17
+ * Factory for creating dimensioned vector column descriptors.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * .column('embedding', { type: vector(1536), nullable: false })
22
+ * // Produces: nativeType: 'vector(1536)', typeParams: { length: 1536 }
23
+ * ```
24
+ *
25
+ * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
26
+ * @returns A column type descriptor with `typeParams.length` set
27
+ * @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]
28
+ */
29
+ export declare function vector<N extends number>(length: N): ColumnTypeDescriptor & {
30
+ readonly typeParams: {
31
+ readonly length: N;
32
+ };
33
+ };
11
34
  //# sourceMappingURL=column-types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"column-types.d.ts","sourceRoot":"","sources":["../../src/exports/column-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,eAAO,MAAM,YAAY;;;CAGgB,CAAC"}
1
+ {"version":3,"file":"column-types.d.ts","sourceRoot":"","sources":["../../src/exports/column-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAG5E;;;GAGG;AACH,eAAO,MAAM,YAAY;;;CAGgB,CAAC;AAE1C;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,EACrC,MAAM,EAAE,CAAC,GACR,oBAAoB,GAAG;IAAE,QAAQ,CAAC,UAAU,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAA;KAAE,CAAA;CAAE,CAWxE"}
@@ -1,9 +1,27 @@
1
+ import {
2
+ VECTOR_CODEC_ID,
3
+ VECTOR_MAX_DIM
4
+ } from "../chunk-MUHUVAQX.js";
5
+
1
6
  // src/exports/column-types.ts
2
7
  var vectorColumn = {
3
- codecId: "pg/vector@1",
8
+ codecId: VECTOR_CODEC_ID,
4
9
  nativeType: "vector"
5
10
  };
11
+ function vector(length) {
12
+ if (!Number.isInteger(length) || length < 1 || length > VECTOR_MAX_DIM) {
13
+ throw new RangeError(
14
+ `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`
15
+ );
16
+ }
17
+ return {
18
+ codecId: VECTOR_CODEC_ID,
19
+ nativeType: `vector(${length})`,
20
+ typeParams: { length }
21
+ };
22
+ }
6
23
  export {
24
+ vector,
7
25
  vectorColumn
8
26
  };
9
27
  //# sourceMappingURL=column-types.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptors for pgvector extension.\n *\n * These descriptors provide both codecId and nativeType for use in contract authoring.\n * They are derived from the same source of truth as codec definitions and manifests.\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';\n\nexport const vectorColumn = {\n codecId: 'pg/vector@1',\n nativeType: 'vector',\n} as const satisfies ColumnTypeDescriptor;\n"],"mappings":";AASO,IAAM,eAAe;AAAA,EAC1B,SAAS;AAAA,EACT,YAAY;AACd;","names":[]}
1
+ {"version":3,"sources":["../../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptors for pgvector extension.\n *\n * These descriptors provide both codecId and nativeType for use in contract authoring.\n * They are derived from the same source of truth as codec definitions and manifests.\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\n\n/**\n * Static vector column descriptor without dimension.\n * Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.\n */\nexport const vectorColumn = {\n codecId: VECTOR_CODEC_ID,\n nativeType: 'vector',\n} as const satisfies ColumnTypeDescriptor;\n\n/**\n * Factory for creating dimensioned vector column descriptors.\n *\n * @example\n * ```typescript\n * .column('embedding', { type: vector(1536), nullable: false })\n * // Produces: nativeType: 'vector(1536)', typeParams: { length: 1536 }\n * ```\n *\n * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)\n * @returns A column type descriptor with `typeParams.length` set\n * @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]\n */\nexport function vector<N extends number>(\n length: N,\n): ColumnTypeDescriptor & { readonly typeParams: { readonly length: N } } {\n if (!Number.isInteger(length) || length < 1 || length > VECTOR_MAX_DIM) {\n throw new RangeError(\n `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`,\n );\n }\n return {\n codecId: VECTOR_CODEC_ID,\n nativeType: `vector(${length})`,\n typeParams: { length },\n } as const;\n}\n"],"mappings":";;;;;;AAcO,IAAM,eAAe;AAAA,EAC1B,SAAS;AAAA,EACT,YAAY;AACd;AAeO,SAAS,OACd,QACwE;AACxE,MAAI,CAAC,OAAO,UAAU,MAAM,KAAK,SAAS,KAAK,SAAS,gBAAgB;AACtE,UAAM,IAAI;AAAA,MACR,iDAAiD,cAAc,UAAU,MAAM;AAAA,IACjF;AAAA,EACF;AACA,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,UAAU,MAAM;AAAA,IAC5B,YAAY,EAAE,OAAO;AAAA,EACvB;AACF;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  pgvectorPackMeta
3
- } from "../chunk-OI5XEHIN.js";
3
+ } from "../chunk-DSJTFXJR.js";
4
4
 
5
5
  // src/exports/control.ts
6
6
  function verifyVectorExtensionInstalled(schema) {
@@ -16,6 +16,11 @@ declare const pgvectorPack: {
16
16
  readonly named: "CodecTypes";
17
17
  readonly alias: "PgVectorTypes";
18
18
  };
19
+ readonly typeImports: readonly [{
20
+ readonly package: "@prisma-next/extension-pgvector/codec-types";
21
+ readonly named: "Vector";
22
+ readonly alias: "Vector";
23
+ }];
19
24
  readonly parameterized: {
20
25
  readonly "pg/vector@1": "Vector<{{length}}>";
21
26
  };
@@ -1 +1 @@
1
- {"version":3,"file":"pack.d.ts","sourceRoot":"","sources":["../../src/exports/pack.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAmB,CAAC;AAEtC,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"pack.d.ts","sourceRoot":"","sources":["../../src/exports/pack.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAmB,CAAC;AAEtC,eAAe,YAAY,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  pgvectorPackMeta
3
- } from "../chunk-OI5XEHIN.js";
3
+ } from "../chunk-DSJTFXJR.js";
4
4
 
5
5
  // src/exports/pack.ts
6
6
  var pgvectorPack = pgvectorPackMeta;
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/exports/runtime.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,6BAA6B,EAE9B,MAAM,0BAA0B,CAAC;AA0BlC;;;GAGG;AACH,QAAA,MAAM,yBAAyB,EAAE,6BAA6B,CAAC,UAAU,CASxE,CAAC;AAEF,eAAe,yBAAyB,CAAC"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/exports/runtime.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,6BAA6B,EAE9B,MAAM,0BAA0B,CAAC;AA2DlC;;;GAGG;AACH,QAAA,MAAM,yBAAyB,EAAE,6BAA6B,CAAC,UAAU,CASxE,CAAC;AAEF,eAAe,yBAAyB,CAAC"}
@@ -1,10 +1,15 @@
1
+ import {
2
+ VECTOR_CODEC_ID,
3
+ VECTOR_MAX_DIM
4
+ } from "../chunk-MUHUVAQX.js";
1
5
  import {
2
6
  pgvectorPackMeta,
3
7
  pgvectorRuntimeOperation
4
- } from "../chunk-OI5XEHIN.js";
8
+ } from "../chunk-DSJTFXJR.js";
5
9
 
6
10
  // src/exports/runtime.ts
7
11
  import { createCodecRegistry } from "@prisma-next/sql-relational-core/ast";
12
+ import { type as arktype } from "arktype";
8
13
 
9
14
  // src/core/codecs.ts
10
15
  import { codec, defineCodecs } from "@prisma-next/sql-relational-core/ast";
@@ -55,6 +60,24 @@ var codecDefinitions = codecs.codecDefinitions;
55
60
  var dataTypes = codecs.dataTypes;
56
61
 
57
62
  // src/exports/runtime.ts
63
+ var vectorParamsSchema = arktype({
64
+ length: "number"
65
+ }).narrow((params, ctx) => {
66
+ const { length } = params;
67
+ if (!Number.isInteger(length)) {
68
+ return ctx.mustBe("an integer");
69
+ }
70
+ if (length < 1 || length > VECTOR_MAX_DIM) {
71
+ return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
72
+ }
73
+ return true;
74
+ });
75
+ var parameterizedCodecDescriptors = [
76
+ {
77
+ codecId: VECTOR_CODEC_ID,
78
+ paramsSchema: vectorParamsSchema
79
+ }
80
+ ];
58
81
  var PgVectorRuntimeExtensionInstance = class {
59
82
  familyId = "sql";
60
83
  targetId = "postgres";
@@ -68,6 +91,9 @@ var PgVectorRuntimeExtensionInstance = class {
68
91
  operations() {
69
92
  return [pgvectorRuntimeOperation];
70
93
  }
94
+ parameterizedCodecs() {
95
+ return parameterizedCodecDescriptors;
96
+ }
71
97
  };
72
98
  var pgvectorRuntimeDescriptor = {
73
99
  kind: "extension",
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/exports/runtime.ts","../../src/core/codecs.ts"],"sourcesContent":["import type { SqlOperationSignature } from '@prisma-next/sql-operations';\nimport type { CodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport type {\n SqlRuntimeExtensionDescriptor,\n SqlRuntimeExtensionInstance,\n} from '@prisma-next/sql-runtime';\nimport { codecDefinitions } from '../core/codecs';\nimport { pgvectorPackMeta, pgvectorRuntimeOperation } from '../core/descriptor-meta';\n\n/**\n * pgvector SQL runtime extension instance.\n * Provides codecs and operations for vector data type and similarity operations.\n */\nclass PgVectorRuntimeExtensionInstance implements SqlRuntimeExtensionInstance<'postgres'> {\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n\n codecs(): CodecRegistry {\n const registry = createCodecRegistry();\n // Register all codecs from codecDefinitions\n for (const def of Object.values(codecDefinitions)) {\n registry.register(def.codec);\n }\n return registry;\n }\n\n operations(): ReadonlyArray<SqlOperationSignature> {\n return [pgvectorRuntimeOperation];\n }\n}\n\n/**\n * pgvector SQL runtime extension descriptor.\n * Provides metadata and factory for creating runtime extension instances.\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 create(): SqlRuntimeExtensionInstance<'postgres'> {\n return new PgVectorRuntimeExtensionInstance();\n },\n};\n\nexport default pgvectorRuntimeDescriptor;\n","/**\n * Vector codec implementation for pgvector extension.\n *\n * Provides encoding/decoding for the `vector` PostgreSQL type.\n * Wire format is a string like `[1,2,3]` (PostgreSQL vector text format).\n */\n\nimport { codec, defineCodecs } from '@prisma-next/sql-relational-core/ast';\n\nconst pgVectorCodec = codec<'pg/vector@1', string, number[]>({\n typeId: 'pg/vector@1',\n targetTypes: ['vector'],\n encode: (value: number[]): string => {\n // Validate that value is an array of numbers\n if (!Array.isArray(value)) {\n throw new Error('Vector value must be an array of numbers');\n }\n if (!value.every((v) => typeof v === 'number')) {\n throw new Error('Vector value must contain only numbers');\n }\n // Format as PostgreSQL vector text format: [1,2,3]\n // PostgreSQL's pg library requires the vector format string\n return `[${value.join(',')}]`;\n },\n decode: (wire: string): number[] => {\n // Handle string format from PostgreSQL: [1,2,3]\n if (typeof wire !== 'string') {\n throw new Error('Vector wire value must be a string');\n }\n // Parse PostgreSQL vector format: [1,2,3]\n if (!wire.startsWith('[') || !wire.endsWith(']')) {\n throw new Error(`Invalid vector format: expected \"[...]\", got \"${wire}\"`);\n }\n const content = wire.slice(1, -1).trim();\n if (content === '') {\n return [];\n }\n const values = content.split(',').map((v) => {\n const num = Number.parseFloat(v.trim());\n if (Number.isNaN(num)) {\n throw new Error(`Invalid vector value: \"${v}\" is not a number`);\n }\n return num;\n });\n return values;\n },\n meta: {\n db: {\n sql: {\n postgres: {\n nativeType: 'vector',\n },\n },\n },\n },\n});\n\n// Build codec definitions using the builder DSL\nconst codecs = defineCodecs().add('vector', pgVectorCodec);\n\n// Export derived structures directly from codecs builder\nexport const codecDefinitions = codecs.codecDefinitions;\nexport const dataTypes = codecs.dataTypes;\n\n// Export types derived from codecs builder\nexport type CodecTypes = typeof codecs.CodecTypes;\n"],"mappings":";;;;;;AAEA,SAAS,2BAA2B;;;ACKpC,SAAS,OAAO,oBAAoB;AAEpC,IAAM,gBAAgB,MAAuC;AAAA,EAC3D,QAAQ;AAAA,EACR,aAAa,CAAC,QAAQ;AAAA,EACtB,QAAQ,CAAC,UAA4B;AAEnC,QAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AACA,QAAI,CAAC,MAAM,MAAM,CAAC,MAAM,OAAO,MAAM,QAAQ,GAAG;AAC9C,YAAM,IAAI,MAAM,wCAAwC;AAAA,IAC1D;AAGA,WAAO,IAAI,MAAM,KAAK,GAAG,CAAC;AAAA,EAC5B;AAAA,EACA,QAAQ,CAAC,SAA2B;AAElC,QAAI,OAAO,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AAEA,QAAI,CAAC,KAAK,WAAW,GAAG,KAAK,CAAC,KAAK,SAAS,GAAG,GAAG;AAChD,YAAM,IAAI,MAAM,iDAAiD,IAAI,GAAG;AAAA,IAC1E;AACA,UAAM,UAAU,KAAK,MAAM,GAAG,EAAE,EAAE,KAAK;AACvC,QAAI,YAAY,IAAI;AAClB,aAAO,CAAC;AAAA,IACV;AACA,UAAM,SAAS,QAAQ,MAAM,GAAG,EAAE,IAAI,CAAC,MAAM;AAC3C,YAAM,MAAM,OAAO,WAAW,EAAE,KAAK,CAAC;AACtC,UAAI,OAAO,MAAM,GAAG,GAAG;AACrB,cAAM,IAAI,MAAM,0BAA0B,CAAC,mBAAmB;AAAA,MAChE;AACA,aAAO;AAAA,IACT,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EACA,MAAM;AAAA,IACJ,IAAI;AAAA,MACF,KAAK;AAAA,QACH,UAAU;AAAA,UACR,YAAY;AAAA,QACd;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;AAGD,IAAM,SAAS,aAAa,EAAE,IAAI,UAAU,aAAa;AAGlD,IAAM,mBAAmB,OAAO;AAChC,IAAM,YAAY,OAAO;;;ADhDhC,IAAM,mCAAN,MAA0F;AAAA,EAC/E,WAAW;AAAA,EACX,WAAW;AAAA,EAEpB,SAAwB;AACtB,UAAM,WAAW,oBAAoB;AAErC,eAAW,OAAO,OAAO,OAAO,gBAAgB,GAAG;AACjD,eAAS,SAAS,IAAI,KAAK;AAAA,IAC7B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,aAAmD;AACjD,WAAO,CAAC,wBAAwB;AAAA,EAClC;AACF;AAMA,IAAM,4BAAuE;AAAA,EAC3E,MAAM;AAAA,EACN,IAAI,iBAAiB;AAAA,EACrB,SAAS,iBAAiB;AAAA,EAC1B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,SAAkD;AAChD,WAAO,IAAI,iCAAiC;AAAA,EAC9C;AACF;AAEA,IAAO,kBAAQ;","names":[]}
1
+ {"version":3,"sources":["../../src/exports/runtime.ts","../../src/core/codecs.ts"],"sourcesContent":["import type { SqlOperationSignature } from '@prisma-next/sql-operations';\nimport type { CodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport type {\n RuntimeParameterizedCodecDescriptor,\n SqlRuntimeExtensionDescriptor,\n SqlRuntimeExtensionInstance,\n} from '@prisma-next/sql-runtime';\nimport { type as arktype } from 'arktype';\nimport { codecDefinitions } from '../core/codecs';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\nimport { pgvectorPackMeta, pgvectorRuntimeOperation } from '../core/descriptor-meta';\n\nconst vectorParamsSchema = arktype({\n length: 'number',\n}).narrow((params, ctx) => {\n const { length } = params;\n if (!Number.isInteger(length)) {\n return ctx.mustBe('an integer');\n }\n if (length < 1 || length > VECTOR_MAX_DIM) {\n return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);\n }\n return true;\n});\n\n/**\n * Pre-allocated parameterized codec descriptors to avoid per-call allocations.\n */\nconst parameterizedCodecDescriptors = [\n {\n codecId: VECTOR_CODEC_ID,\n paramsSchema: vectorParamsSchema,\n },\n] as const satisfies ReadonlyArray<\n RuntimeParameterizedCodecDescriptor<{ readonly length: number }>\n>;\n\n/**\n * pgvector SQL runtime extension instance.\n * Provides codecs and operations for vector data type and similarity operations.\n */\nclass PgVectorRuntimeExtensionInstance implements SqlRuntimeExtensionInstance<'postgres'> {\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n\n codecs(): CodecRegistry {\n const registry = createCodecRegistry();\n // Register all codecs from codecDefinitions\n for (const def of Object.values(codecDefinitions)) {\n registry.register(def.codec);\n }\n return registry;\n }\n\n operations(): ReadonlyArray<SqlOperationSignature> {\n return [pgvectorRuntimeOperation];\n }\n\n parameterizedCodecs(): ReadonlyArray<\n RuntimeParameterizedCodecDescriptor<{ readonly length: number }>\n > {\n return parameterizedCodecDescriptors;\n }\n}\n\n/**\n * pgvector SQL runtime extension descriptor.\n * Provides metadata and factory for creating runtime extension instances.\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 create(): SqlRuntimeExtensionInstance<'postgres'> {\n return new PgVectorRuntimeExtensionInstance();\n },\n};\n\nexport default pgvectorRuntimeDescriptor;\n","/**\n * Vector codec implementation for pgvector extension.\n *\n * Provides encoding/decoding for the `vector` PostgreSQL type.\n * Wire format is a string like `[1,2,3]` (PostgreSQL vector text format).\n */\n\nimport { codec, defineCodecs } from '@prisma-next/sql-relational-core/ast';\n\nconst pgVectorCodec = codec<'pg/vector@1', string, number[]>({\n typeId: 'pg/vector@1',\n targetTypes: ['vector'],\n encode: (value: number[]): string => {\n // Validate that value is an array of numbers\n if (!Array.isArray(value)) {\n throw new Error('Vector value must be an array of numbers');\n }\n if (!value.every((v) => typeof v === 'number')) {\n throw new Error('Vector value must contain only numbers');\n }\n // Format as PostgreSQL vector text format: [1,2,3]\n // PostgreSQL's pg library requires the vector format string\n return `[${value.join(',')}]`;\n },\n decode: (wire: string): number[] => {\n // Handle string format from PostgreSQL: [1,2,3]\n if (typeof wire !== 'string') {\n throw new Error('Vector wire value must be a string');\n }\n // Parse PostgreSQL vector format: [1,2,3]\n if (!wire.startsWith('[') || !wire.endsWith(']')) {\n throw new Error(`Invalid vector format: expected \"[...]\", got \"${wire}\"`);\n }\n const content = wire.slice(1, -1).trim();\n if (content === '') {\n return [];\n }\n const values = content.split(',').map((v) => {\n const num = Number.parseFloat(v.trim());\n if (Number.isNaN(num)) {\n throw new Error(`Invalid vector value: \"${v}\" is not a number`);\n }\n return num;\n });\n return values;\n },\n meta: {\n db: {\n sql: {\n postgres: {\n nativeType: 'vector',\n },\n },\n },\n },\n});\n\n// Build codec definitions using the builder DSL\nconst codecs = defineCodecs().add('vector', pgVectorCodec);\n\n// Export derived structures directly from codecs builder\nexport const codecDefinitions = codecs.codecDefinitions;\nexport const dataTypes = codecs.dataTypes;\n\n// Export types derived from codecs builder\nexport type CodecTypes = typeof codecs.CodecTypes;\n"],"mappings":";;;;;;;;;;AAEA,SAAS,2BAA2B;AAMpC,SAAS,QAAQ,eAAe;;;ACDhC,SAAS,OAAO,oBAAoB;AAEpC,IAAM,gBAAgB,MAAuC;AAAA,EAC3D,QAAQ;AAAA,EACR,aAAa,CAAC,QAAQ;AAAA,EACtB,QAAQ,CAAC,UAA4B;AAEnC,QAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AACA,QAAI,CAAC,MAAM,MAAM,CAAC,MAAM,OAAO,MAAM,QAAQ,GAAG;AAC9C,YAAM,IAAI,MAAM,wCAAwC;AAAA,IAC1D;AAGA,WAAO,IAAI,MAAM,KAAK,GAAG,CAAC;AAAA,EAC5B;AAAA,EACA,QAAQ,CAAC,SAA2B;AAElC,QAAI,OAAO,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AAEA,QAAI,CAAC,KAAK,WAAW,GAAG,KAAK,CAAC,KAAK,SAAS,GAAG,GAAG;AAChD,YAAM,IAAI,MAAM,iDAAiD,IAAI,GAAG;AAAA,IAC1E;AACA,UAAM,UAAU,KAAK,MAAM,GAAG,EAAE,EAAE,KAAK;AACvC,QAAI,YAAY,IAAI;AAClB,aAAO,CAAC;AAAA,IACV;AACA,UAAM,SAAS,QAAQ,MAAM,GAAG,EAAE,IAAI,CAAC,MAAM;AAC3C,YAAM,MAAM,OAAO,WAAW,EAAE,KAAK,CAAC;AACtC,UAAI,OAAO,MAAM,GAAG,GAAG;AACrB,cAAM,IAAI,MAAM,0BAA0B,CAAC,mBAAmB;AAAA,MAChE;AACA,aAAO;AAAA,IACT,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EACA,MAAM;AAAA,IACJ,IAAI;AAAA,MACF,KAAK;AAAA,QACH,UAAU;AAAA,UACR,YAAY;AAAA,QACd;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;AAGD,IAAM,SAAS,aAAa,EAAE,IAAI,UAAU,aAAa;AAGlD,IAAM,mBAAmB,OAAO;AAChC,IAAM,YAAY,OAAO;;;ADjDhC,IAAM,qBAAqB,QAAQ;AAAA,EACjC,QAAQ;AACV,CAAC,EAAE,OAAO,CAAC,QAAQ,QAAQ;AACzB,QAAM,EAAE,OAAO,IAAI;AACnB,MAAI,CAAC,OAAO,UAAU,MAAM,GAAG;AAC7B,WAAO,IAAI,OAAO,YAAY;AAAA,EAChC;AACA,MAAI,SAAS,KAAK,SAAS,gBAAgB;AACzC,WAAO,IAAI,OAAO,oBAAoB,cAAc,GAAG;AAAA,EACzD;AACA,SAAO;AACT,CAAC;AAKD,IAAM,gCAAgC;AAAA,EACpC;AAAA,IACE,SAAS;AAAA,IACT,cAAc;AAAA,EAChB;AACF;AAQA,IAAM,mCAAN,MAA0F;AAAA,EAC/E,WAAW;AAAA,EACX,WAAW;AAAA,EAEpB,SAAwB;AACtB,UAAM,WAAW,oBAAoB;AAErC,eAAW,OAAO,OAAO,OAAO,gBAAgB,GAAG;AACjD,eAAS,SAAS,IAAI,KAAK;AAAA,IAC7B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,aAAmD;AACjD,WAAO,CAAC,wBAAwB;AAAA,EAClC;AAAA,EAEA,sBAEE;AACA,WAAO;AAAA,EACT;AACF;AAMA,IAAM,4BAAuE;AAAA,EAC3E,MAAM;AAAA,EACN,IAAI,iBAAiB;AAAA,EACrB,SAAS,iBAAiB;AAAA,EAC1B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,SAAkD;AAChD,WAAO,IAAI,iCAAiC;AAAA,EAC9C;AACF;AAEA,IAAO,kBAAQ;","names":[]}
@@ -6,5 +6,29 @@
6
6
  *
7
7
  * Runtime codec implementations are provided by the extension's codec registry.
8
8
  */
9
- export type { CodecTypes } from '../core/codecs';
9
+ import type { CodecTypes as CoreCodecTypes } from '../core/codecs';
10
+ /**
11
+ * Type-level branded vector.
12
+ *
13
+ * The runtime values are plain number arrays, but parameterized column typing can
14
+ * carry the dimension at the type level (e.g. Vector<1536>).
15
+ */
16
+ export type Vector<N extends number = number> = number[] & {
17
+ readonly __vectorLength?: N;
18
+ };
19
+ /**
20
+ * Codec types for pgvector.
21
+ *
22
+ * - Scalar output remains `number[]` (runtime representation).
23
+ * - `parameterizedOutput` enables lane typing to compute `Vector<N>` from column `typeParams`.
24
+ */
25
+ export type CodecTypes = CoreCodecTypes & {
26
+ readonly 'pg/vector@1': CoreCodecTypes['pg/vector@1'] & {
27
+ readonly parameterizedOutput: <P extends {
28
+ readonly length: number;
29
+ }>(params: P) => P extends {
30
+ readonly length: infer N extends number;
31
+ } ? Vector<N> : Vector<number>;
32
+ };
33
+ };
10
34
  //# sourceMappingURL=codec-types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec-types.d.ts","sourceRoot":"","sources":["../../src/types/codec-types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"codec-types.d.ts","sourceRoot":"","sources":["../../src/types/codec-types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,IAAI,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEnE;;;;;GAKG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,MAAM,EAAE,GAAG;IAAE,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAA;CAAE,CAAC;AAE3F;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,cAAc,GAAG;IACxC,QAAQ,CAAC,aAAa,EAAE,cAAc,CAAC,aAAa,CAAC,GAAG;QACtD,QAAQ,CAAC,mBAAmB,EAAE,CAAC,CAAC,SAAS;YAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;SAAE,EAClE,MAAM,EAAE,CAAC,KACN,CAAC,SAAS;YAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,SAAS,MAAM,CAAA;SAAE,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;KACzF,CAAC;CACH,CAAC"}
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
2
  "name": "@prisma-next/extension-pgvector",
3
- "version": "0.3.0-dev.32",
3
+ "version": "0.3.0-dev.33",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
7
7
  "arktype": "^2.0.0",
8
- "@prisma-next/cli": "0.3.0-dev.32",
9
- "@prisma-next/contract": "0.3.0-dev.32",
10
- "@prisma-next/contract-authoring": "0.3.0-dev.32",
11
- "@prisma-next/core-control-plane": "0.3.0-dev.32",
12
- "@prisma-next/family-sql": "0.3.0-dev.32",
13
- "@prisma-next/sql-relational-core": "0.3.0-dev.32",
14
- "@prisma-next/sql-operations": "0.3.0-dev.32",
15
- "@prisma-next/sql-runtime": "0.3.0-dev.32",
16
- "@prisma-next/sql-schema-ir": "0.3.0-dev.32"
8
+ "@prisma-next/cli": "0.3.0-dev.33",
9
+ "@prisma-next/contract": "0.3.0-dev.33",
10
+ "@prisma-next/contract-authoring": "0.3.0-dev.33",
11
+ "@prisma-next/core-control-plane": "0.3.0-dev.33",
12
+ "@prisma-next/sql-operations": "0.3.0-dev.33",
13
+ "@prisma-next/sql-relational-core": "0.3.0-dev.33",
14
+ "@prisma-next/sql-runtime": "0.3.0-dev.33",
15
+ "@prisma-next/sql-schema-ir": "0.3.0-dev.33",
16
+ "@prisma-next/family-sql": "0.3.0-dev.33"
17
17
  },
18
18
  "devDependencies": {
19
19
  "tsup": "8.5.1",
20
20
  "typescript": "5.9.3",
21
21
  "vitest": "4.0.16",
22
- "@prisma-next/adapter-postgres": "0.3.0-dev.32",
23
- "@prisma-next/operations": "0.3.0-dev.32",
24
- "@prisma-next/sql-contract": "0.3.0-dev.32",
25
- "@prisma-next/sql-contract-ts": "0.3.0-dev.32",
26
- "@prisma-next/sql-lane": "0.3.0-dev.32",
22
+ "@prisma-next/adapter-postgres": "0.3.0-dev.33",
23
+ "@prisma-next/operations": "0.3.0-dev.33",
24
+ "@prisma-next/sql-contract": "0.3.0-dev.33",
25
+ "@prisma-next/sql-contract-ts": "0.3.0-dev.33",
26
+ "@prisma-next/sql-lane": "0.3.0-dev.33",
27
27
  "@prisma-next/test-utils": "0.0.1",
28
28
  "@prisma-next/tsconfig": "0.0.0"
29
29
  },
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Codec ID for pgvector's vector type.
3
+ */
4
+ export const VECTOR_CODEC_ID = 'pg/vector@1' as const;
5
+
6
+ /**
7
+ * Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).
8
+ */
9
+ export const VECTOR_MAX_DIM = 16000;
@@ -38,10 +38,16 @@ export const pgvectorPackMeta = {
38
38
  named: 'CodecTypes',
39
39
  alias: 'PgVectorTypes',
40
40
  },
41
+ typeImports: [
42
+ {
43
+ package: '@prisma-next/extension-pgvector/codec-types',
44
+ named: 'Vector',
45
+ alias: 'Vector',
46
+ },
47
+ ],
41
48
  // Parameterized codec renderers for type emission.
42
49
  // The renderer template produces precise TypeScript types like Vector<1536>
43
50
  // when columns have typeParams with a `length` property.
44
- // Note: The Vector<N> type import is deferred to Phase 6.
45
51
  parameterized: {
46
52
  [pgvectorTypeId]: 'Vector<{{length}}>',
47
53
  },
@@ -4,4 +4,4 @@
4
4
  * Re-export from types module for public API.
5
5
  */
6
6
 
7
- export type { CodecTypes } from '../types/codec-types';
7
+ export type { CodecTypes, Vector } from '../types/codec-types';
@@ -6,8 +6,41 @@
6
6
  */
7
7
 
8
8
  import type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';
9
+ import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
9
10
 
11
+ /**
12
+ * Static vector column descriptor without dimension.
13
+ * Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
14
+ */
10
15
  export const vectorColumn = {
11
- codecId: 'pg/vector@1',
16
+ codecId: VECTOR_CODEC_ID,
12
17
  nativeType: 'vector',
13
18
  } as const satisfies ColumnTypeDescriptor;
19
+
20
+ /**
21
+ * Factory for creating dimensioned vector column descriptors.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * .column('embedding', { type: vector(1536), nullable: false })
26
+ * // Produces: nativeType: 'vector(1536)', typeParams: { length: 1536 }
27
+ * ```
28
+ *
29
+ * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
30
+ * @returns A column type descriptor with `typeParams.length` set
31
+ * @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]
32
+ */
33
+ export function vector<N extends number>(
34
+ length: N,
35
+ ): ColumnTypeDescriptor & { readonly typeParams: { readonly length: N } } {
36
+ if (!Number.isInteger(length) || length < 1 || length > VECTOR_MAX_DIM) {
37
+ throw new RangeError(
38
+ `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`,
39
+ );
40
+ }
41
+ return {
42
+ codecId: VECTOR_CODEC_ID,
43
+ nativeType: `vector(${length})`,
44
+ typeParams: { length },
45
+ } as const;
46
+ }
@@ -2,12 +2,40 @@ import type { SqlOperationSignature } from '@prisma-next/sql-operations';
2
2
  import type { CodecRegistry } from '@prisma-next/sql-relational-core/ast';
3
3
  import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
4
4
  import type {
5
+ RuntimeParameterizedCodecDescriptor,
5
6
  SqlRuntimeExtensionDescriptor,
6
7
  SqlRuntimeExtensionInstance,
7
8
  } from '@prisma-next/sql-runtime';
9
+ import { type as arktype } from 'arktype';
8
10
  import { codecDefinitions } from '../core/codecs';
11
+ import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
9
12
  import { pgvectorPackMeta, pgvectorRuntimeOperation } from '../core/descriptor-meta';
10
13
 
14
+ const vectorParamsSchema = arktype({
15
+ length: 'number',
16
+ }).narrow((params, ctx) => {
17
+ const { length } = params;
18
+ if (!Number.isInteger(length)) {
19
+ return ctx.mustBe('an integer');
20
+ }
21
+ if (length < 1 || length > VECTOR_MAX_DIM) {
22
+ return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
23
+ }
24
+ return true;
25
+ });
26
+
27
+ /**
28
+ * Pre-allocated parameterized codec descriptors to avoid per-call allocations.
29
+ */
30
+ const parameterizedCodecDescriptors = [
31
+ {
32
+ codecId: VECTOR_CODEC_ID,
33
+ paramsSchema: vectorParamsSchema,
34
+ },
35
+ ] as const satisfies ReadonlyArray<
36
+ RuntimeParameterizedCodecDescriptor<{ readonly length: number }>
37
+ >;
38
+
11
39
  /**
12
40
  * pgvector SQL runtime extension instance.
13
41
  * Provides codecs and operations for vector data type and similarity operations.
@@ -28,6 +56,12 @@ class PgVectorRuntimeExtensionInstance implements SqlRuntimeExtensionInstance<'p
28
56
  operations(): ReadonlyArray<SqlOperationSignature> {
29
57
  return [pgvectorRuntimeOperation];
30
58
  }
59
+
60
+ parameterizedCodecs(): ReadonlyArray<
61
+ RuntimeParameterizedCodecDescriptor<{ readonly length: number }>
62
+ > {
63
+ return parameterizedCodecDescriptors;
64
+ }
31
65
  }
32
66
 
33
67
  /**
@@ -7,4 +7,26 @@
7
7
  * Runtime codec implementations are provided by the extension's codec registry.
8
8
  */
9
9
 
10
- export type { CodecTypes } from '../core/codecs';
10
+ import type { CodecTypes as CoreCodecTypes } from '../core/codecs';
11
+
12
+ /**
13
+ * Type-level branded vector.
14
+ *
15
+ * The runtime values are plain number arrays, but parameterized column typing can
16
+ * carry the dimension at the type level (e.g. Vector<1536>).
17
+ */
18
+ export type Vector<N extends number = number> = number[] & { readonly __vectorLength?: N };
19
+
20
+ /**
21
+ * Codec types for pgvector.
22
+ *
23
+ * - Scalar output remains `number[]` (runtime representation).
24
+ * - `parameterizedOutput` enables lane typing to compute `Vector<N>` from column `typeParams`.
25
+ */
26
+ export type CodecTypes = CoreCodecTypes & {
27
+ readonly 'pg/vector@1': CoreCodecTypes['pg/vector@1'] & {
28
+ readonly parameterizedOutput: <P extends { readonly length: number }>(
29
+ params: P,
30
+ ) => P extends { readonly length: infer N extends number } ? Vector<N> : Vector<number>;
31
+ };
32
+ };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":["import type { ExtensionPackRef } from '@prisma-next/contract/framework-components';\nimport type { SqlOperationSignature } from '@prisma-next/sql-operations';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\nconst cosineLowering = {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n} as const;\n\n/**\n * Shared operation definition used by both pack metadata and runtime descriptor.\n * Frozen to prevent accidental mutation.\n */\nconst cosineDistanceOperation = Object.freeze({\n method: 'cosineDistance',\n args: [{ kind: 'param' }],\n returns: { kind: 'builtin', type: 'number' },\n lowering: cosineLowering,\n} as const);\n\nexport const pgvectorPackMeta = {\n kind: 'extension',\n id: 'pgvector',\n familyId: 'sql',\n targetId: 'postgres',\n version: '0.0.1',\n capabilities: {\n postgres: {\n 'pgvector/cosine': true,\n },\n },\n types: {\n codecTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'CodecTypes',\n alias: 'PgVectorTypes',\n },\n // Parameterized codec renderers for type emission.\n // The renderer template produces precise TypeScript types like Vector<1536>\n // when columns have typeParams with a `length` property.\n // Note: The Vector<N> type import is deferred to Phase 6.\n parameterized: {\n [pgvectorTypeId]: 'Vector<{{length}}>',\n },\n },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n operations: [\n {\n for: pgvectorTypeId,\n ...cosineDistanceOperation,\n },\n ],\n} as const satisfies ExtensionPackRef<'sql', 'postgres'>;\n\nexport const pgvectorRuntimeOperation: SqlOperationSignature = {\n forTypeId: pgvectorTypeId,\n ...cosineDistanceOperation,\n};\n"],"mappings":";AAGA,IAAM,iBAAiB;AAEvB,IAAM,iBAAiB;AAAA,EACrB,cAAc;AAAA,EACd,UAAU;AAAA,EACV,UAAU;AACZ;AAMA,IAAM,0BAA0B,OAAO,OAAO;AAAA,EAC5C,QAAQ;AAAA,EACR,MAAM,CAAC,EAAE,MAAM,QAAQ,CAAC;AAAA,EACxB,SAAS,EAAE,MAAM,WAAW,MAAM,SAAS;AAAA,EAC3C,UAAU;AACZ,CAAU;AAEH,IAAM,mBAAmB;AAAA,EAC9B,MAAM;AAAA,EACN,IAAI;AAAA,EACJ,UAAU;AAAA,EACV,UAAU;AAAA,EACV,SAAS;AAAA,EACT,cAAc;AAAA,IACZ,UAAU;AAAA,MACR,mBAAmB;AAAA,IACrB;AAAA,EACF;AAAA,EACA,OAAO;AAAA,IACL,YAAY;AAAA,MACV,QAAQ;AAAA,QACN,SAAS;AAAA,QACT,OAAO;AAAA,QACP,OAAO;AAAA,MACT;AAAA;AAAA;AAAA;AAAA;AAAA,MAKA,eAAe;AAAA,QACb,CAAC,cAAc,GAAG;AAAA,MACpB;AAAA,IACF;AAAA,IACA,gBAAgB;AAAA,MACd,QAAQ;AAAA,QACN,SAAS;AAAA,QACT,OAAO;AAAA,QACP,OAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,SAAS;AAAA,MACP,EAAE,QAAQ,gBAAgB,UAAU,OAAO,UAAU,YAAY,YAAY,SAAS;AAAA,IACxF;AAAA,EACF;AAAA,EACA,YAAY;AAAA,IACV;AAAA,MACE,KAAK;AAAA,MACL,GAAG;AAAA,IACL;AAAA,EACF;AACF;AAEO,IAAM,2BAAkD;AAAA,EAC7D,WAAW;AAAA,EACX,GAAG;AACL;","names":[]}