@prisma-next/extension-pgvector 0.5.0-dev.4 → 0.5.0-dev.41

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
@@ -69,7 +69,7 @@ Add vector columns to your contract and enable the namespace via pack refs:
69
69
  import { int4Column, textColumn } from '@prisma-next/adapter-postgres/column-types';
70
70
  import sqlFamily from '@prisma-next/family-sql/pack';
71
71
  import { defineContract, field, model } from '@prisma-next/sql-contract-ts/contract-builder';
72
- import { vectorColumn } from '@prisma-next/extension-pgvector/column-types';
72
+ import { vector, vectorColumn } from '@prisma-next/extension-pgvector/column-types';
73
73
  import pgvector from '@prisma-next/extension-pgvector/pack';
74
74
  import postgres from '@prisma-next/target-postgres/pack';
75
75
 
@@ -82,13 +82,16 @@ export const contract = defineContract({
82
82
  fields: {
83
83
  id: field.column(int4Column).id(),
84
84
  title: field.column(textColumn),
85
- embedding: field.column(vectorColumn).optional(),
85
+ // Dimensioned vector — `field.embedding` resolves to `Vector<1536>`.
86
+ embedding: field.column(vector(1536)).optional(),
86
87
  },
87
88
  }).sql({ table: 'post' }),
88
89
  },
89
90
  });
90
91
  ```
91
92
 
93
+ The `vector(N)` factory is registered through the unified `CodecDescriptor<{ length: number }>` shape — `paramsSchema` validates the dimension at the contract boundary, `renderOutputType: ({ length }) => 'Vector<' + length + '>'` produces the column's TS type for `contract.d.ts`, and the curried `factory` materializes the runtime codec at context construction. See [ADR 208 — Higher-order codecs for parameterized types](../../../docs/architecture%20docs/adrs/ADR%20208%20-%20Higher-order%20codecs%20for%20parameterized%20types.md) for the descriptor model. For undimensioned vector columns (rare; typically only valid in legacy schemas where `vector` was declared without a dimension), use `vectorColumn` instead.
94
+
92
95
  ### Runtime Setup
93
96
 
94
97
  Register the extension when creating your execution stack:
package/dist/control.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
1
+ import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-DTopW_37.mjs";
2
2
 
3
3
  //#region src/exports/control.ts
4
4
  const PGVECTOR_CODEC_ID = "pg/vector@1";
@@ -47,7 +47,7 @@ const pgvectorExtensionDescriptor = {
47
47
  controlPlaneHooks: { [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks }
48
48
  }
49
49
  },
50
- queryOperations: () => pgvectorQueryOperations,
50
+ queryOperations: () => pgvectorQueryOperations(),
51
51
  databaseDependencies: pgvectorDatabaseDependencies,
52
52
  create: () => ({
53
53
  familyId: "sql",
@@ -1 +1 @@
1
- {"version":3,"file":"control.mjs","names":["vectorControlPlaneHooks: CodecControlHooks","pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown>","pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'>"],"sources":["../src/exports/control.ts"],"sourcesContent":["import type {\n CodecControlHooks,\n ComponentDatabaseDependencies,\n SqlControlExtensionDescriptor,\n} from '@prisma-next/family-sql/control';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\n\nconst PGVECTOR_CODEC_ID = 'pg/vector@1' as const;\n\nfunction buildVectorIdentityValue(typeParams: Record<string, unknown> | undefined): string | null {\n const length = typeParams?.['length'];\n if (typeof length !== 'number' || !Number.isInteger(length) || length <= 0) {\n return null;\n }\n\n const zeroVector = `[${new Array(length).fill('0').join(',')}]`;\n return `'${zeroVector}'::vector`;\n}\n\nconst vectorControlPlaneHooks: CodecControlHooks = {\n expandNativeType: ({ nativeType, typeParams }) => {\n const length = typeParams?.['length'];\n if (typeof length === 'number' && Number.isInteger(length) && length > 0) {\n return `${nativeType}(${length})`;\n }\n return nativeType;\n },\n resolveIdentityValue: ({ typeParams }) => buildVectorIdentityValue(typeParams),\n};\n\nconst pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown> = {\n init: [\n {\n id: 'postgres.extension.vector',\n label: 'Enable vector extension',\n install: [\n {\n id: 'extension.vector',\n label: 'Enable extension \"vector\"',\n summary: 'Ensures the vector extension is available for pgvector operations',\n operationClass: 'additive',\n target: { id: 'postgres' },\n precheck: [\n {\n description: 'verify extension \"vector\" is not already enabled',\n sql: \"SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n execute: [\n {\n description: 'create extension \"vector\"',\n sql: 'CREATE EXTENSION IF NOT EXISTS vector',\n },\n ],\n postcheck: [\n {\n description: 'confirm extension \"vector\" is enabled',\n sql: \"SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n },\n ],\n },\n ],\n};\n\nconst pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'> = {\n ...pgvectorPackMeta,\n types: {\n ...pgvectorPackMeta.types,\n codecTypes: {\n ...pgvectorPackMeta.types.codecTypes,\n controlPlaneHooks: {\n [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks,\n },\n },\n },\n queryOperations: () => pgvectorQueryOperations,\n databaseDependencies: pgvectorDatabaseDependencies,\n create: () => ({\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n }),\n};\n\nexport { pgvectorExtensionDescriptor };\nexport default pgvectorExtensionDescriptor;\n"],"mappings":";;;AAOA,MAAM,oBAAoB;AAE1B,SAAS,yBAAyB,YAAgE;CAChG,MAAM,SAAS,aAAa;AAC5B,KAAI,OAAO,WAAW,YAAY,CAAC,OAAO,UAAU,OAAO,IAAI,UAAU,EACvE,QAAO;AAIT,QAAO,IADY,IAAI,IAAI,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,GACvC;;AAGxB,MAAMA,0BAA6C;CACjD,mBAAmB,EAAE,YAAY,iBAAiB;EAChD,MAAM,SAAS,aAAa;AAC5B,MAAI,OAAO,WAAW,YAAY,OAAO,UAAU,OAAO,IAAI,SAAS,EACrE,QAAO,GAAG,WAAW,GAAG,OAAO;AAEjC,SAAO;;CAET,uBAAuB,EAAE,iBAAiB,yBAAyB,WAAW;CAC/E;AAED,MAAMC,+BAAuE,EAC3E,MAAM,CACJ;CACE,IAAI;CACJ,OAAO;CACP,SAAS,CACP;EACE,IAAI;EACJ,OAAO;EACP,SAAS;EACT,gBAAgB;EAChB,QAAQ,EAAE,IAAI,YAAY;EAC1B,UAAU,CACR;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,SAAS,CACP;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,WAAW,CACT;GACE,aAAa;GACb,KAAK;GACN,CACF;EACF,CACF;CACF,CACF,EACF;AAED,MAAMC,8BAAyE;CAC7E,GAAG;CACH,OAAO;EACL,GAAG,iBAAiB;EACpB,YAAY;GACV,GAAG,iBAAiB,MAAM;GAC1B,mBAAmB,GAChB,oBAAoB,yBACtB;GACF;EACF;CACD,uBAAuB;CACvB,sBAAsB;CACtB,eAAe;EACb,UAAU;EACV,UAAU;EACX;CACF;AAGD,sBAAe"}
1
+ {"version":3,"file":"control.mjs","names":["vectorControlPlaneHooks: CodecControlHooks","pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown>","pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'>"],"sources":["../src/exports/control.ts"],"sourcesContent":["import type {\n CodecControlHooks,\n ComponentDatabaseDependencies,\n SqlControlExtensionDescriptor,\n} from '@prisma-next/family-sql/control';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\n\nconst PGVECTOR_CODEC_ID = 'pg/vector@1' as const;\n\nfunction buildVectorIdentityValue(typeParams: Record<string, unknown> | undefined): string | null {\n const length = typeParams?.['length'];\n if (typeof length !== 'number' || !Number.isInteger(length) || length <= 0) {\n return null;\n }\n\n const zeroVector = `[${new Array(length).fill('0').join(',')}]`;\n return `'${zeroVector}'::vector`;\n}\n\nconst vectorControlPlaneHooks: CodecControlHooks = {\n expandNativeType: ({ nativeType, typeParams }) => {\n const length = typeParams?.['length'];\n if (typeof length === 'number' && Number.isInteger(length) && length > 0) {\n return `${nativeType}(${length})`;\n }\n return nativeType;\n },\n resolveIdentityValue: ({ typeParams }) => buildVectorIdentityValue(typeParams),\n};\n\nconst pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown> = {\n init: [\n {\n id: 'postgres.extension.vector',\n label: 'Enable vector extension',\n install: [\n {\n id: 'extension.vector',\n label: 'Enable extension \"vector\"',\n summary: 'Ensures the vector extension is available for pgvector operations',\n operationClass: 'additive',\n target: { id: 'postgres' },\n precheck: [\n {\n description: 'verify extension \"vector\" is not already enabled',\n sql: \"SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n execute: [\n {\n description: 'create extension \"vector\"',\n sql: 'CREATE EXTENSION IF NOT EXISTS vector',\n },\n ],\n postcheck: [\n {\n description: 'confirm extension \"vector\" is enabled',\n sql: \"SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n },\n ],\n },\n ],\n};\n\nconst pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'> = {\n ...pgvectorPackMeta,\n types: {\n ...pgvectorPackMeta.types,\n codecTypes: {\n ...pgvectorPackMeta.types.codecTypes,\n controlPlaneHooks: {\n [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks,\n },\n },\n },\n queryOperations: () => pgvectorQueryOperations(),\n databaseDependencies: pgvectorDatabaseDependencies,\n create: () => ({\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n }),\n};\n\nexport { pgvectorExtensionDescriptor };\nexport default pgvectorExtensionDescriptor;\n"],"mappings":";;;AAOA,MAAM,oBAAoB;AAE1B,SAAS,yBAAyB,YAAgE;CAChG,MAAM,SAAS,aAAa;AAC5B,KAAI,OAAO,WAAW,YAAY,CAAC,OAAO,UAAU,OAAO,IAAI,UAAU,EACvE,QAAO;AAIT,QAAO,IADY,IAAI,IAAI,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,GACvC;;AAGxB,MAAMA,0BAA6C;CACjD,mBAAmB,EAAE,YAAY,iBAAiB;EAChD,MAAM,SAAS,aAAa;AAC5B,MAAI,OAAO,WAAW,YAAY,OAAO,UAAU,OAAO,IAAI,SAAS,EACrE,QAAO,GAAG,WAAW,GAAG,OAAO;AAEjC,SAAO;;CAET,uBAAuB,EAAE,iBAAiB,yBAAyB,WAAW;CAC/E;AAED,MAAMC,+BAAuE,EAC3E,MAAM,CACJ;CACE,IAAI;CACJ,OAAO;CACP,SAAS,CACP;EACE,IAAI;EACJ,OAAO;EACP,SAAS;EACT,gBAAgB;EAChB,QAAQ,EAAE,IAAI,YAAY;EAC1B,UAAU,CACR;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,SAAS,CACP;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,WAAW,CACT;GACE,aAAa;GACb,KAAK;GACN,CACF;EACF,CACF;CACF,CACF,EACF;AAED,MAAMC,8BAAyE;CAC7E,GAAG;CACH,OAAO;EACL,GAAG,iBAAiB;EACpB,YAAY;GACV,GAAG,iBAAiB,MAAM;GAC1B,mBAAmB,GAChB,oBAAoB,yBACtB;GACF;EACF;CACD,uBAAuB,yBAAyB;CAChD,sBAAsB;CACtB,eAAe;EACb,UAAU;EACV,UAAU;EACX;CACF;AAGD,sBAAe"}
@@ -1,4 +1,5 @@
1
1
  import { n as VECTOR_MAX_DIM } from "./constants-Co5golCK.mjs";
2
+ import { buildOperation, toExpr } from "@prisma-next/sql-relational-core/expression";
2
3
  import { codec, defineCodecs } from "@prisma-next/sql-relational-core/ast";
3
4
 
4
5
  //#region src/core/authoring.ts
@@ -64,43 +65,41 @@ const dataTypes = codecs.dataTypes;
64
65
  //#endregion
65
66
  //#region src/core/descriptor-meta.ts
66
67
  const pgvectorTypeId = "pg/vector@1";
67
- const pgvectorQueryOperations = [{
68
- method: "cosineDistance",
69
- args: [{
70
- codecId: pgvectorTypeId,
71
- nullable: false
72
- }, {
73
- codecId: pgvectorTypeId,
74
- nullable: false
75
- }],
76
- returns: {
77
- codecId: "pg/float8@1",
78
- nullable: false
79
- },
80
- lowering: {
81
- targetFamily: "sql",
82
- strategy: "function",
83
- template: "{{self}} <=> {{arg0}}"
84
- }
85
- }, {
86
- method: "cosineSimilarity",
87
- args: [{
88
- codecId: pgvectorTypeId,
89
- nullable: false
68
+ function pgvectorQueryOperations() {
69
+ return [{
70
+ method: "cosineDistance",
71
+ self: { codecId: pgvectorTypeId },
72
+ impl: (self, other) => buildOperation({
73
+ method: "cosineDistance",
74
+ args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],
75
+ returns: {
76
+ codecId: "pg/float8@1",
77
+ nullable: false
78
+ },
79
+ lowering: {
80
+ targetFamily: "sql",
81
+ strategy: "function",
82
+ template: "{{self}} <=> {{arg0}}"
83
+ }
84
+ })
90
85
  }, {
91
- codecId: pgvectorTypeId,
92
- nullable: false
93
- }],
94
- returns: {
95
- codecId: "pg/float8@1",
96
- nullable: false
97
- },
98
- lowering: {
99
- targetFamily: "sql",
100
- strategy: "function",
101
- template: "1 - ({{self}} <=> {{arg0}})"
102
- }
103
- }];
86
+ method: "cosineSimilarity",
87
+ self: { codecId: pgvectorTypeId },
88
+ impl: (self, other) => buildOperation({
89
+ method: "cosineSimilarity",
90
+ args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],
91
+ returns: {
92
+ codecId: "pg/float8@1",
93
+ nullable: false
94
+ },
95
+ lowering: {
96
+ targetFamily: "sql",
97
+ strategy: "function",
98
+ template: "1 - ({{self}} <=> {{arg0}})"
99
+ }
100
+ })
101
+ }];
102
+ }
104
103
  const pgvectorPackMetaBase = {
105
104
  kind: "extension",
106
105
  id: "pgvector",
@@ -145,4 +144,4 @@ const pgvectorPackMeta = pgvectorPackMetaBase;
145
144
 
146
145
  //#endregion
147
146
  export { pgvectorQueryOperations as n, codecDefinitions as r, pgvectorPackMeta as t };
148
- //# sourceMappingURL=descriptor-meta-BQbvJJxu.mjs.map
147
+ //# sourceMappingURL=descriptor-meta-DTopW_37.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"descriptor-meta-DTopW_37.mjs","names":["pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n}"],"sources":["../src/core/authoring.ts","../src/core/codecs.ts","../src/core/descriptor-meta.ts"],"sourcesContent":["import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\nimport { VECTOR_MAX_DIM } from './constants';\n\nexport const pgvectorAuthoringTypes = {\n pgvector: {\n Vector: {\n kind: 'typeConstructor',\n args: [\n { kind: 'number', name: 'length', integer: true, minimum: 1, maximum: VECTOR_MAX_DIM },\n ],\n output: {\n codecId: 'pg/vector@1',\n nativeType: 'vector',\n typeParams: {\n length: { kind: 'arg', index: 0 },\n },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\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({\n typeId: 'pg/vector@1',\n targetTypes: ['vector'],\n traits: ['equality'],\n renderOutputType: (typeParams) => {\n const length = typeParams['length'];\n if (length === undefined) return undefined;\n if (typeof length !== 'number' || !Number.isFinite(length) || !Number.isInteger(length)) {\n throw new Error(\n `renderOutputType: expected positive integer \"length\" in typeParams for Vector, got ${String(length)}`,\n );\n }\n return `Vector<${length}>`;\n },\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","import type { SqlOperationDescriptor } from '@prisma-next/sql-operations';\nimport {\n buildOperation,\n type CodecExpression,\n type Expression,\n toExpr,\n} from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { codecDefinitions } from './codecs';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\ntype CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;\n\nexport function pgvectorQueryOperations<\n CT extends CodecTypesBase,\n>(): readonly SqlOperationDescriptor[] {\n return [\n {\n method: 'cosineDistance',\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> =>\n buildOperation({\n method: 'cosineDistance',\n args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '{{self}} <=> {{arg0}}',\n },\n }),\n },\n {\n method: 'cosineSimilarity',\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> =>\n buildOperation({\n method: 'cosineSimilarity',\n args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n },\n }),\n },\n ];\n}\n\nconst pgvectorPackMetaBase = {\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 authoring: {\n type: pgvectorAuthoringTypes,\n },\n types: {\n codecTypes: {\n codecInstances: Object.values(codecDefinitions).map((def) => def.codec),\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 },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n} as const;\n\nexport const pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = pgvectorPackMetaBase;\n"],"mappings":";;;;;AAGA,MAAa,yBAAyB,EACpC,UAAU,EACR,QAAQ;CACN,MAAM;CACN,MAAM,CACJ;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EA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AgB,UAAU;GAAO,UAAU;GAAY,YAAY;GAAU,CACxF;EACF;CACF;AAED,MAAaA,mBAET"}
@@ -1,7 +1,11 @@
1
+ import { CodecExpression, Expression } from "@prisma-next/sql-relational-core/expression";
1
2
  import { SqlQueryOperationTypes } from "@prisma-next/sql-contract/types";
2
3
 
3
4
  //#region src/types/operation-types.d.ts
4
-
5
+ type CodecTypesBase = Record<string, {
6
+ readonly input: unknown;
7
+ readonly output: unknown;
8
+ }>;
5
9
  /**
6
10
  * Operation type definitions for pgvector extension.
7
11
  *
@@ -11,64 +15,36 @@ import { SqlQueryOperationTypes } from "@prisma-next/sql-contract/types";
11
15
  type OperationTypes = {
12
16
  readonly 'pg/vector@1': {
13
17
  readonly cosineDistance: {
14
- readonly args: readonly [{
18
+ readonly self: {
15
19
  readonly codecId: 'pg/vector@1';
16
- readonly nullable: false;
17
- }];
18
- readonly returns: {
19
- readonly codecId: 'pg/float8@1';
20
- readonly nullable: false;
21
- };
22
- readonly lowering: {
23
- readonly targetFamily: 'sql';
24
- readonly strategy: 'function';
25
- readonly template: string;
26
20
  };
27
21
  };
28
22
  readonly cosineSimilarity: {
29
- readonly args: readonly [{
23
+ readonly self: {
30
24
  readonly codecId: 'pg/vector@1';
31
- readonly nullable: false;
32
- }];
33
- readonly returns: {
34
- readonly codecId: 'pg/float8@1';
35
- readonly nullable: false;
36
- };
37
- readonly lowering: {
38
- readonly targetFamily: 'sql';
39
- readonly strategy: 'function';
40
- readonly template: string;
41
25
  };
42
26
  };
43
27
  };
44
28
  };
45
29
  /** Flat operation signatures for the query builder. */
46
- type QueryOperationTypes = SqlQueryOperationTypes<{
30
+ type QueryOperationTypes<CT extends CodecTypesBase> = SqlQueryOperationTypes<CT, {
47
31
  readonly cosineDistance: {
48
- readonly args: readonly [{
49
- readonly codecId: 'pg/vector@1';
50
- readonly nullable: boolean;
51
- }, {
32
+ readonly self: {
52
33
  readonly codecId: 'pg/vector@1';
53
- readonly nullable: boolean;
54
- }];
55
- readonly returns: {
56
- readonly codecId: 'pg/float8@1';
57
- readonly nullable: false;
58
34
  };
35
+ readonly impl: (self: CodecExpression<'pg/vector@1', boolean, CT>, other: CodecExpression<'pg/vector@1', boolean, CT>) => Expression<{
36
+ codecId: 'pg/float8@1';
37
+ nullable: false;
38
+ }>;
59
39
  };
60
40
  readonly cosineSimilarity: {
61
- readonly args: readonly [{
62
- readonly codecId: 'pg/vector@1';
63
- readonly nullable: boolean;
64
- }, {
41
+ readonly self: {
65
42
  readonly codecId: 'pg/vector@1';
66
- readonly nullable: boolean;
67
- }];
68
- readonly returns: {
69
- readonly codecId: 'pg/float8@1';
70
- readonly nullable: false;
71
43
  };
44
+ readonly impl: (self: CodecExpression<'pg/vector@1', boolean, CT>, other: CodecExpression<'pg/vector@1', boolean, CT>) => Expression<{
45
+ codecId: 'pg/float8@1';
46
+ nullable: false;
47
+ }>;
72
48
  };
73
49
  }>;
74
50
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"operation-types.d.mts","names":[],"sources":["../src/types/operation-types.ts"],"sourcesContent":[],"mappings":";;;;;;AASA;AAwBA;;;KAxBY,cAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwBA,mBAAA,GAAsB"}
1
+ {"version":3,"file":"operation-types.d.mts","names":[],"sources":["../src/types/operation-types.ts"],"sourcesContent":[],"mappings":";;;;KAGK,cAAA,GAAiB;;EAAjB,SAAA,MAAA,EAAc,OAAA;AASnB,CAAA,CAAA;AAYA;;;;;;AAOe,KAnBH,cAAA,GAmBG;EACJ,SAAA,aAAA,EAAA;IAK2C,SAAA,cAAA,EAAA;MAAxC,SAAA,IAAA,EAAA;QACyC,SAAA,OAAA,EAAA,aAAA;MAAxC,CAAA;IACJ,CAAA;IAfkD,SAAA,gBAAA,EAAA;MAAsB,SAAA,IAAA,EAAA;;;;;;;KAAvE,+BAA+B,kBAAkB,uBAC3D;;;;;0BAKY,wCAAwC,YACvC,wCAAwC,QAC5C;;;;;;;;;0BAKG,wCAAwC,YACvC,wCAAwC,QAC5C"}
package/dist/pack.d.mts CHANGED
@@ -2,6 +2,7 @@ import { t as CodecTypes } from "./codec-types-BifaP625.mjs";
2
2
  import * as _prisma_next_sql_relational_core_ast0 from "@prisma-next/sql-relational-core/ast";
3
3
 
4
4
  //#region src/core/descriptor-meta.d.ts
5
+
5
6
  declare const pgvectorPackMetaBase: {
6
7
  readonly kind: "extension";
7
8
  readonly id: "pgvector";
@@ -1 +1 @@
1
- {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":[],"mappings":";;;;cAoCM;EAAA,SAAA,IAAA,EAAA,WAgDI;EAEG,SAAA,EAAA,EAAA,UAEW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAJd,qCAAA,CAAA,8DAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,yBAAyB;0BACZ"}
1
+ {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":[],"mappings":";;;;;cA0DM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAgDI,qCAAA,CAAA,8DAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,yBAAyB;0BACZ"}
package/dist/pack.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
1
+ import { t as pgvectorPackMeta } from "./descriptor-meta-DTopW_37.mjs";
2
2
 
3
3
  export { pgvectorPackMeta as default };
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/exports/runtime.ts"],"sourcesContent":[],"mappings":";;;cAwCM,2BAA2B"}
1
+ {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/exports/runtime.ts"],"sourcesContent":[],"mappings":";;;cA6DM,2BAA2B"}
package/dist/runtime.mjs CHANGED
@@ -1,17 +1,24 @@
1
1
  import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-Co5golCK.mjs";
2
- import { n as pgvectorQueryOperations, r as codecDefinitions, t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
2
+ import { n as pgvectorQueryOperations, r as codecDefinitions, t as pgvectorPackMeta } from "./descriptor-meta-DTopW_37.mjs";
3
3
  import { createCodecRegistry } from "@prisma-next/sql-relational-core/ast";
4
4
  import { type } from "arktype";
5
5
 
6
6
  //#region src/exports/runtime.ts
7
+ const vectorParamsSchema = type({ length: "number" }).narrow((params, ctx) => {
8
+ const { length } = params;
9
+ if (!Number.isInteger(length)) return ctx.mustBe("an integer");
10
+ if (length < 1 || length > VECTOR_MAX_DIM) return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
11
+ return true;
12
+ });
13
+ const sharedVectorCodec = codecDefinitions.vector.codec;
14
+ const vectorFactory = (_params) => (_ctx) => sharedVectorCodec;
7
15
  const parameterizedCodecDescriptors = [{
8
16
  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
- })
17
+ traits: ["equality"],
18
+ targetTypes: ["vector"],
19
+ paramsSchema: vectorParamsSchema,
20
+ renderOutputType: (params) => `Vector<${params.length}>`,
21
+ factory: vectorFactory
15
22
  }];
16
23
  function createPgvectorCodecRegistry() {
17
24
  const registry = createCodecRegistry();
@@ -24,8 +31,9 @@ const pgvectorRuntimeDescriptor = {
24
31
  version: pgvectorPackMeta.version,
25
32
  familyId: "sql",
26
33
  targetId: "postgres",
34
+ types: { codecTypes: { codecInstances: Object.values(codecDefinitions).map((def) => def.codec) } },
27
35
  codecs: createPgvectorCodecRegistry,
28
- queryOperations: () => pgvectorQueryOperations,
36
+ queryOperations: () => pgvectorQueryOperations(),
29
37
  parameterizedCodecs: () => parameterizedCodecDescriptors,
30
38
  create() {
31
39
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.mjs","names":["arktype","pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'>"],"sources":["../src/exports/runtime.ts"],"sourcesContent":["import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport type {\n RuntimeParameterizedCodecDescriptor,\n SqlRuntimeExtensionDescriptor,\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, pgvectorQueryOperations } 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\nconst parameterizedCodecDescriptors = [\n {\n codecId: VECTOR_CODEC_ID,\n paramsSchema: vectorParamsSchema,\n },\n] as const satisfies ReadonlyArray<\n RuntimeParameterizedCodecDescriptor<{ readonly length: number }>\n>;\n\nfunction createPgvectorCodecRegistry() {\n const registry = createCodecRegistry();\n for (const def of Object.values(codecDefinitions)) {\n registry.register(def.codec);\n }\n return registry;\n}\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 codecs: createPgvectorCodecRegistry,\n queryOperations: () => pgvectorQueryOperations,\n parameterizedCodecs: () => parameterizedCodecDescriptors,\n create() {\n return {\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n };\n },\n};\n\nexport default pgvectorRuntimeDescriptor;\n"],"mappings":";;;;;;AAuBA,MAAM,gCAAgC,CACpC;CACE,SAAS;CACT,cAhBuBA,KAAQ,EACjC,QAAQ,UACT,CAAC,CAAC,QAAQ,QAAQ,QAAQ;EACzB,MAAM,EAAE,WAAW;AACnB,MAAI,CAAC,OAAO,UAAU,OAAO,CAC3B,QAAO,IAAI,OAAO,aAAa;AAEjC,MAAI,SAAS,KAAK,SAAS,eACzB,QAAO,IAAI,OAAO,oBAAoB,eAAe,GAAG;AAE1D,SAAO;GACP;CAMC,CACF;AAID,SAAS,8BAA8B;CACrC,MAAM,WAAW,qBAAqB;AACtC,MAAK,MAAM,OAAO,OAAO,OAAO,iBAAiB,CAC/C,UAAS,SAAS,IAAI,MAAM;AAE9B,QAAO;;AAGT,MAAMC,4BAAuE;CAC3E,MAAM;CACN,IAAI,iBAAiB;CACrB,SAAS,iBAAiB;CAC1B,UAAU;CACV,UAAU;CACV,QAAQ;CACR,uBAAuB;CACvB,2BAA2B;CAC3B,SAAS;AACP,SAAO;GACL,UAAU;GACV,UAAU;GACX;;CAEJ;AAED,sBAAe"}
1
+ {"version":3,"file":"runtime.mjs","names":["arktype","sharedVectorCodec: Codec","pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'>"],"sources":["../src/exports/runtime.ts"],"sourcesContent":["import type { Codec, CodecInstanceContext } from '@prisma-next/framework-components/codec';\nimport { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport type {\n RuntimeParameterizedCodecDescriptor,\n SqlRuntimeExtensionDescriptor,\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, pgvectorQueryOperations } 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// pgvector's encode is parameter-independent (the wire format `[v1,v2,...]`\n// doesn't care about declared length), so the resolved codec for every\n// `(length)` instance is the same shared codec object today. The factory\n// returns it directly; `ctx` is unused. When a future refactor wants per-\n// instance state (e.g. capping wire length to declared dimension), the\n// closure over `params` is the place to add it.\n//\n// The factory parameter types as the family-agnostic\n// `CodecInstanceContext` because pgvector doesn't read the SQL-specific\n// `usedAt` field. The SQL runtime materializer passes a\n// `SqlCodecInstanceContext`; family-agnostic factories are structurally\n// compatible with the SQL extended type.\nconst sharedVectorCodec: Codec = codecDefinitions.vector.codec;\nconst vectorFactory = (_params: { readonly length: number }) => (_ctx: CodecInstanceContext) =>\n sharedVectorCodec;\n\nconst parameterizedCodecDescriptors = [\n {\n codecId: VECTOR_CODEC_ID,\n traits: ['equality'] as const,\n targetTypes: ['vector'] as const,\n paramsSchema: vectorParamsSchema,\n renderOutputType: (params: { readonly length: number }) => `Vector<${params.length}>`,\n factory: vectorFactory,\n },\n] as const satisfies ReadonlyArray<\n RuntimeParameterizedCodecDescriptor<{ readonly length: number }>\n>;\n\nfunction createPgvectorCodecRegistry() {\n const registry = createCodecRegistry();\n for (const def of Object.values(codecDefinitions)) {\n registry.register(def.codec);\n }\n return registry;\n}\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 // Mirror `pgvectorPackMeta.types.codecTypes.codecInstances` here so that\n // runtime-plane assemblers driven by `extractCodecLookup` (which reads\n // `descriptor.types?.codecTypes?.codecInstances`) discover `pg/vector@1`.\n // Without this, the Postgres adapter's runtime-plane codec lookup misses\n // the vector codec and `$N::vector` would silently disappear once the\n // renderer switches to lookup-driven cast policy.\n types: {\n codecTypes: {\n codecInstances: Object.values(codecDefinitions).map((def) => def.codec),\n },\n },\n codecs: createPgvectorCodecRegistry,\n queryOperations: () => pgvectorQueryOperations(),\n parameterizedCodecs: () => parameterizedCodecDescriptors,\n create() {\n return {\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n };\n },\n};\n\nexport default pgvectorRuntimeDescriptor;\n"],"mappings":";;;;;;AAWA,MAAM,qBAAqBA,KAAQ,EACjC,QAAQ,UACT,CAAC,CAAC,QAAQ,QAAQ,QAAQ;CACzB,MAAM,EAAE,WAAW;AACnB,KAAI,CAAC,OAAO,UAAU,OAAO,CAC3B,QAAO,IAAI,OAAO,aAAa;AAEjC,KAAI,SAAS,KAAK,SAAS,eACzB,QAAO,IAAI,OAAO,oBAAoB,eAAe,GAAG;AAE1D,QAAO;EACP;AAcF,MAAMC,oBAA2B,iBAAiB,OAAO;AACzD,MAAM,iBAAiB,aAA0C,SAC/D;AAEF,MAAM,gCAAgC,CACpC;CACE,SAAS;CACT,QAAQ,CAAC,WAAW;CACpB,aAAa,CAAC,SAAS;CACvB,cAAc;CACd,mBAAmB,WAAwC,UAAU,OAAO,OAAO;CACnF,SAAS;CACV,CACF;AAID,SAAS,8BAA8B;CACrC,MAAM,WAAW,qBAAqB;AACtC,MAAK,MAAM,OAAO,OAAO,OAAO,iBAAiB,CAC/C,UAAS,SAAS,IAAI,MAAM;AAE9B,QAAO;;AAGT,MAAMC,4BAAuE;CAC3E,MAAM;CACN,IAAI,iBAAiB;CACrB,SAAS,iBAAiB;CAC1B,UAAU;CACV,UAAU;CAOV,OAAO,EACL,YAAY,EACV,gBAAgB,OAAO,OAAO,iBAAiB,CAAC,KAAK,QAAQ,IAAI,MAAM,EACxE,EACF;CACD,QAAQ;CACR,uBAAuB,yBAAyB;CAChD,2BAA2B;CAC3B,SAAS;AACP,SAAO;GACL,UAAU;GACV,UAAU;GACX;;CAEJ;AAED,sBAAe"}
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
2
  "name": "@prisma-next/extension-pgvector",
3
- "version": "0.5.0-dev.4",
3
+ "version": "0.5.0-dev.41",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
7
7
  "arktype": "^2.0.0",
8
- "@prisma-next/contract": "0.5.0-dev.4",
9
- "@prisma-next/contract-authoring": "0.5.0-dev.4",
10
- "@prisma-next/framework-components": "0.5.0-dev.4",
11
- "@prisma-next/family-sql": "0.5.0-dev.4",
12
- "@prisma-next/sql-relational-core": "0.5.0-dev.4",
13
- "@prisma-next/sql-runtime": "0.5.0-dev.4",
14
- "@prisma-next/sql-schema-ir": "0.5.0-dev.4",
15
- "@prisma-next/sql-operations": "0.5.0-dev.4"
8
+ "@prisma-next/family-sql": "0.5.0-dev.41",
9
+ "@prisma-next/contract-authoring": "0.5.0-dev.41",
10
+ "@prisma-next/sql-operations": "0.5.0-dev.41",
11
+ "@prisma-next/sql-relational-core": "0.5.0-dev.41",
12
+ "@prisma-next/contract": "0.5.0-dev.41",
13
+ "@prisma-next/sql-runtime": "0.5.0-dev.41",
14
+ "@prisma-next/framework-components": "0.5.0-dev.41",
15
+ "@prisma-next/sql-schema-ir": "0.5.0-dev.41"
16
16
  },
17
17
  "devDependencies": {
18
18
  "tsdown": "0.18.4",
19
19
  "typescript": "5.9.3",
20
20
  "vitest": "4.0.17",
21
- "@prisma-next/sql-contract": "0.5.0-dev.4",
22
- "@prisma-next/sql-contract-ts": "0.5.0-dev.4",
21
+ "@prisma-next/operations": "0.5.0-dev.41",
22
+ "@prisma-next/sql-contract": "0.5.0-dev.41",
23
+ "@prisma-next/sql-contract-ts": "0.5.0-dev.41",
23
24
  "@prisma-next/test-utils": "0.0.1",
24
- "@prisma-next/operations": "0.5.0-dev.4",
25
- "@prisma-next/tsconfig": "0.0.0",
26
- "@prisma-next/tsdown": "0.0.0"
25
+ "@prisma-next/tsdown": "0.0.0",
26
+ "@prisma-next/tsconfig": "0.0.0"
27
27
  },
28
28
  "files": [
29
29
  "dist",
@@ -1,38 +1,60 @@
1
1
  import type { SqlOperationDescriptor } from '@prisma-next/sql-operations';
2
+ import {
3
+ buildOperation,
4
+ type CodecExpression,
5
+ type Expression,
6
+ toExpr,
7
+ } from '@prisma-next/sql-relational-core/expression';
2
8
  import type { CodecTypes } from '../types/codec-types';
3
9
  import { pgvectorAuthoringTypes } from './authoring';
4
10
  import { codecDefinitions } from './codecs';
5
11
 
6
12
  const pgvectorTypeId = 'pg/vector@1' as const;
7
13
 
8
- export const pgvectorQueryOperations: readonly SqlOperationDescriptor[] = [
9
- {
10
- method: 'cosineDistance',
11
- args: [
12
- { codecId: pgvectorTypeId, nullable: false },
13
- { codecId: pgvectorTypeId, nullable: false },
14
- ],
15
- returns: { codecId: 'pg/float8@1', nullable: false },
16
- lowering: {
17
- targetFamily: 'sql',
18
- strategy: 'function',
19
- template: '{{self}} <=> {{arg0}}',
14
+ type CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;
15
+
16
+ export function pgvectorQueryOperations<
17
+ CT extends CodecTypesBase,
18
+ >(): readonly SqlOperationDescriptor[] {
19
+ return [
20
+ {
21
+ method: 'cosineDistance',
22
+ self: { codecId: pgvectorTypeId },
23
+ impl: (
24
+ self: CodecExpression<'pg/vector@1', boolean, CT>,
25
+ other: CodecExpression<'pg/vector@1', boolean, CT>,
26
+ ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> =>
27
+ buildOperation({
28
+ method: 'cosineDistance',
29
+ args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],
30
+ returns: { codecId: 'pg/float8@1', nullable: false },
31
+ lowering: {
32
+ targetFamily: 'sql',
33
+ strategy: 'function',
34
+ template: '{{self}} <=> {{arg0}}',
35
+ },
36
+ }),
20
37
  },
21
- },
22
- {
23
- method: 'cosineSimilarity',
24
- args: [
25
- { codecId: pgvectorTypeId, nullable: false },
26
- { codecId: pgvectorTypeId, nullable: false },
27
- ],
28
- returns: { codecId: 'pg/float8@1', nullable: false },
29
- lowering: {
30
- targetFamily: 'sql',
31
- strategy: 'function',
32
- template: '1 - ({{self}} <=> {{arg0}})',
38
+ {
39
+ method: 'cosineSimilarity',
40
+ self: { codecId: pgvectorTypeId },
41
+ impl: (
42
+ self: CodecExpression<'pg/vector@1', boolean, CT>,
43
+ other: CodecExpression<'pg/vector@1', boolean, CT>,
44
+ ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> =>
45
+ buildOperation({
46
+ method: 'cosineSimilarity',
47
+ args: [toExpr(self, pgvectorTypeId), toExpr(other, pgvectorTypeId)],
48
+ returns: { codecId: 'pg/float8@1', nullable: false },
49
+ lowering: {
50
+ targetFamily: 'sql',
51
+ strategy: 'function',
52
+ template: '1 - ({{self}} <=> {{arg0}})',
53
+ },
54
+ }),
33
55
  },
34
- },
35
- ];
56
+ ];
57
+ }
36
58
 
37
59
  const pgvectorPackMetaBase = {
38
60
  kind: 'extension',
@@ -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,
@@ -1,3 +1,4 @@
1
+ import type { Codec, CodecInstanceContext } from '@prisma-next/framework-components/codec';
1
2
  import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
2
3
  import type {
3
4
  RuntimeParameterizedCodecDescriptor,
@@ -21,10 +22,30 @@ const vectorParamsSchema = arktype({
21
22
  return true;
22
23
  });
23
24
 
25
+ // pgvector's encode is parameter-independent (the wire format `[v1,v2,...]`
26
+ // doesn't care about declared length), so the resolved codec for every
27
+ // `(length)` instance is the same shared codec object today. The factory
28
+ // returns it directly; `ctx` is unused. When a future refactor wants per-
29
+ // instance state (e.g. capping wire length to declared dimension), the
30
+ // closure over `params` is the place to add it.
31
+ //
32
+ // The factory parameter types as the family-agnostic
33
+ // `CodecInstanceContext` because pgvector doesn't read the SQL-specific
34
+ // `usedAt` field. The SQL runtime materializer passes a
35
+ // `SqlCodecInstanceContext`; family-agnostic factories are structurally
36
+ // compatible with the SQL extended type.
37
+ const sharedVectorCodec: Codec = codecDefinitions.vector.codec;
38
+ const vectorFactory = (_params: { readonly length: number }) => (_ctx: CodecInstanceContext) =>
39
+ sharedVectorCodec;
40
+
24
41
  const parameterizedCodecDescriptors = [
25
42
  {
26
43
  codecId: VECTOR_CODEC_ID,
44
+ traits: ['equality'] as const,
45
+ targetTypes: ['vector'] as const,
27
46
  paramsSchema: vectorParamsSchema,
47
+ renderOutputType: (params: { readonly length: number }) => `Vector<${params.length}>`,
48
+ factory: vectorFactory,
28
49
  },
29
50
  ] as const satisfies ReadonlyArray<
30
51
  RuntimeParameterizedCodecDescriptor<{ readonly length: number }>
@@ -44,8 +65,19 @@ const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {
44
65
  version: pgvectorPackMeta.version,
45
66
  familyId: 'sql' as const,
46
67
  targetId: 'postgres' as const,
68
+ // Mirror `pgvectorPackMeta.types.codecTypes.codecInstances` here so that
69
+ // runtime-plane assemblers driven by `extractCodecLookup` (which reads
70
+ // `descriptor.types?.codecTypes?.codecInstances`) discover `pg/vector@1`.
71
+ // Without this, the Postgres adapter's runtime-plane codec lookup misses
72
+ // the vector codec and `$N::vector` would silently disappear once the
73
+ // renderer switches to lookup-driven cast policy.
74
+ types: {
75
+ codecTypes: {
76
+ codecInstances: Object.values(codecDefinitions).map((def) => def.codec),
77
+ },
78
+ },
47
79
  codecs: createPgvectorCodecRegistry,
48
- queryOperations: () => pgvectorQueryOperations,
80
+ queryOperations: () => pgvectorQueryOperations(),
49
81
  parameterizedCodecs: () => parameterizedCodecDescriptors,
50
82
  create() {
51
83
  return {
@@ -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 args: readonly [{ readonly codecId: 'pg/vector@1'; readonly nullable: false }];
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 args: readonly [{ readonly codecId: 'pg/vector@1'; readonly nullable: false }];
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
- readonly cosineDistance: {
36
- readonly args: readonly [
37
- { readonly codecId: 'pg/vector@1'; readonly nullable: boolean },
38
- { readonly codecId: 'pg/vector@1'; readonly nullable: boolean },
39
- ];
40
- readonly returns: { readonly codecId: 'pg/float8@1'; readonly nullable: false };
41
- };
42
- readonly cosineSimilarity: {
43
- readonly args: readonly [
44
- { readonly codecId: 'pg/vector@1'; readonly nullable: boolean },
45
- { readonly codecId: 'pg/vector@1'; readonly nullable: boolean },
46
- ];
47
- readonly returns: { readonly codecId: 'pg/float8@1'; readonly nullable: false };
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
+ >;
@@ -1 +0,0 @@
1
- {"version":3,"file":"descriptor-meta-BQbvJJxu.mjs","names":["pgvectorQueryOperations: readonly SqlOperationDescriptor[]","pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n}"],"sources":["../src/core/authoring.ts","../src/core/codecs.ts","../src/core/descriptor-meta.ts"],"sourcesContent":["import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\nimport { VECTOR_MAX_DIM } from './constants';\n\nexport const pgvectorAuthoringTypes = {\n pgvector: {\n Vector: {\n kind: 'typeConstructor',\n args: [\n { kind: 'number', name: 'length', integer: true, minimum: 1, maximum: VECTOR_MAX_DIM },\n ],\n output: {\n codecId: 'pg/vector@1',\n nativeType: 'vector',\n typeParams: {\n length: { kind: 'arg', index: 0 },\n },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\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({\n typeId: 'pg/vector@1',\n targetTypes: ['vector'],\n traits: ['equality'],\n renderOutputType: (typeParams) => {\n const length = typeParams['length'];\n if (length === undefined) return undefined;\n if (typeof length !== 'number' || !Number.isFinite(length) || !Number.isInteger(length)) {\n throw new Error(\n `renderOutputType: expected positive integer \"length\" in typeParams for Vector, got ${String(length)}`,\n );\n }\n return `Vector<${length}>`;\n },\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","import type { SqlOperationDescriptor } from '@prisma-next/sql-operations';\nimport type { CodecTypes } from '../types/codec-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { codecDefinitions } from './codecs';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\nexport const pgvectorQueryOperations: readonly SqlOperationDescriptor[] = [\n {\n method: 'cosineDistance',\n args: [\n { codecId: pgvectorTypeId, nullable: false },\n { codecId: pgvectorTypeId, nullable: false },\n ],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '{{self}} <=> {{arg0}}',\n },\n },\n {\n method: 'cosineSimilarity',\n args: [\n { codecId: pgvectorTypeId, nullable: false },\n { codecId: pgvectorTypeId, nullable: false },\n ],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n },\n },\n];\n\nconst pgvectorPackMetaBase = {\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 authoring: {\n type: pgvectorAuthoringTypes,\n },\n types: {\n codecTypes: {\n codecInstances: Object.values(codecDefinitions).map((def) => def.codec),\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 },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n} as const;\n\nexport const pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = pgvectorPackMetaBase;\n"],"mappings":";;;;AAGA,MAAa,yBAAyB,EACpC,UAAU,EACR,QAAQ;CACN,MAAM;CACN,MAAM,CACJ;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EAAG,SAAS;EAAgB,CACvF;CACD,QAAQ;EACN,SAAS;EACT,YAAY;EACZ,YAAY,EACV,QAAQ;GAAE,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