pgvector support for Node.js
Supports Sequelize, node-postgres, pg-promise, and Prisma
Run:
npm install pgvectorAnd follow the instructions for your database library:
Register the type
const{ Sequelize }=require('sequelize');constpgvector=require('pgvector/sequelize');pgvector.registerType(Sequelize);Add a vector field
Item.init({embedding: {type: DataTypes.VECTOR(3)}}, ...);Insert a vector
awaitItem.create({embedding: [1,2,3]});Get the nearest neighbors to a vector
constitems=awaitItem.findAll({order: [sequelize.literal(`embedding <-> '[1, 2, 3]'`)],limit: 5});Register the type
constpgvector=require('pgvector/pg');awaitpgvector.registerType(client);Insert a vector
constembedding=[1,2,3];awaitclient.query('INSERT INTO items (embedding) VALUES ($1)',[pgvector.toSql(embedding)]);Get the nearest neighbors to a vector
constresult=awaitclient.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5',[pgvector.toSql(embedding)]);Register the type
constpgvector=require('pgvector/pg');constinitOptions={asyncconnect(e){awaitpgvector.registerType(e.client);}};constpgp=require('pg-promise')(initOptions);Insert a vector
constembedding=[1,2,3];awaitdb.none('INSERT INTO items (embedding) VALUES ($1)',[pgvector.toSql(embedding)]);Get the nearest neighbors to a vector
constresult=awaitdb.any('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5',[pgvector.toSql(embedding)]);Import the library
constpgvector=require('pgvector/utils')Add the extension to the schema
generatorclient{provider="prisma-client-js"previewFeatures=["postgresqlExtensions"]}datasourcedb{provider="postgresql"url=env("DATABASE_URL")extensions=[vector]}Add a vector column to the schema
modelItem{idInt@id@default(autoincrement())embeddingUnsupported("vector(3)")?}Insert a vector
constembedding=pgvector.toSql([1,1,1])awaitprisma.$executeRaw`INSERT INTO items (embedding) VALUES (${embedding}::vector)`Get the nearest neighbors to a vector
constembedding=pgvector.toSql([1,1,1])constitems=awaitprisma.$queryRaw`SELECT id, embedding::text FROM items ORDER BY embedding <-> ${embedding}::vector LIMIT 5`View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-node.git cd pgvector-node npm install npm run build:cjs createdb pgvector_node_test npx prisma migrate dev npm test