Skip to main content
The pgvector extension pack adds the vector column type and cosine-based similarity operations to Prisma Next for PostgreSQL databases. You define vector columns with an explicit dimension (for example, vector(1536) for OpenAI embeddings), query them with cosineDistance and cosineSimilarity, and let the framework handle the wire format, codec registration, and the CREATE EXTENSION vector baseline migration.

What the extension provides

  • vector(N) column type — a dimensioned number[] column backed by the pgvector vector native type. The dimension is carried through contract.d.ts as a Vector<N> branded type.
  • cosineDistance(rhs) — computes cosine distance between two vectors using the pgvector <=> operator.
  • cosineSimilarity(rhs) — computes 1 - cosineDistance, i.e. 1 - (vector1 <=> vector2).
  • Baseline migration — ships an on-disk migration that runs CREATE EXTENSION IF NOT EXISTS vector automatically when the extension is composed into your application.
  • Capability flag — declares pgvector.cosine to signal cosine distance and similarity support.

Setup

1

Install the package

2

Register the extension in prisma-next.config.ts

Add pgvector to the extensions list. With the simplified @prisma-next/postgres/config facade use extensions; with the full @prisma-next/cli/config-types use extensionPacks:
3

Define vector columns in your contract

Import vector from @prisma-next/extension-pgvector/column-types and use field.column(vector(N)). Every pgvector column must declare an explicit dimension.
src/prisma/contract.ts
4

Emit the contract and apply the baseline migration

db init applies the bundled baseline migration, which runs CREATE EXTENSION IF NOT EXISTS vector on your database before any application migration.
The baseline migration must be applied before you run workloads that use vector columns. If you are setting up the database manually, run CREATE EXTENSION IF NOT EXISTS vector; before your first migration.
5

Register the extension at runtime

Pass pgvector in the extensionPacks array when creating your execution stack:
src/prisma/db.ts

Querying with vector operations

Both operations are available on any vector column as methods on the column expression. Pass the query vector as a param() reference.

Operations reference

cosineDistance

Computes the cosine distance between two vectors. Lower values indicate greater similarity.
  • Signature: cosineDistance(rhs: number[] | vector): number
  • SQL: vector1 <=> vector2 (pgvector <=> operator)

cosineSimilarity

Computes the cosine similarity between two vectors (equivalent to 1 - cosineDistance). Higher values indicate greater similarity.
  • Signature: cosineSimilarity(rhs: number[] | vector): number
  • SQL: 1 - (vector1 <=> vector2)

Types

Vector<N>

Vector<N> is a branded number[] type that carries the vector dimension at the type level. It appears in emitted contract.d.ts files when you use the dimensioned vector(N) column factory.

OperationTypes

Database setup reference

The extension ships an on-disk baseline migration in its contract space. The equivalent DDL for manual setup is:
When pgvector is listed in extensionPacks, both prisma-next db init and prisma-next db update apply this baseline automatically before any application-space migration runs.

Capability flag

The extension declares the pgvector.cosine capability. Features that depend on cosine distance and similarity operations can declare a requires: ['pgvector.cosine'] constraint in their capability gate.