Skip to content

pnutmath/pgvector-node

Repository files navigation

pgvector-node

pgvector support for Node.js

Supports Sequelize, node-postgres, pg-promise, and Prisma

Build Status

Installation

Run:

npm install pgvector

And follow the instructions for your database library:

Sequelize

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});

node-postgres

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)]);

pg-promise

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)]);

Prisma

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`

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

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

About

pgvector support for Node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%