Skip to content

pgvector/pgvector-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-node

pgvector support for Node.js

Supports Sequelize, node-postgres, and pg-promise

Build Status

Installation

Run:

npm install pgvector

And follow the instructions for your database library:

Sequelize

Register the type

const { Sequelize } = require('sequelize');
const pgvector = require('pgvector/sequelize');

pgvector.registerType(Sequelize);

Add a vector field

Item.init({
  factors: {
    type: DataTypes.VECTOR(3)
  }
}, ...);

Insert a vector

await Item.create({factors: [1, 2, 3]});

Get the nearest neighbors to a vector

const items = await Item.findAll({
  order: [sequelize.literal(`factors <-> '[1, 2, 3]'`)],
  limit: 5
});

node-postgres

Register the type

const pgvector = require('pgvector/pg');

await pgvector.registerType(client);

Insert a vector

const factors = [1, 2, 3];
await client.query('INSERT INTO items (factors) VALUES ($1)', [pgvector.toSql(factors)]);

Get the nearest neighbors to a vector

const result = await client.query('SELECT * FROM items ORDER BY factors <-> $1 LIMIT 5', [pgvector.toSql(factors)]);

pg-promise

Register the type

const pgvector = require('pgvector/pg');

const initOptions = {
  async connect(client, dc, useCount) {
    await pgvector.registerType(client);
  }
};
const pgp = require('pg-promise')(initOptions);

Insert a vector

const factors = [1, 2, 3];
await db.none('INSERT INTO items (factors) VALUES ($1)', [pgvector.toSql(factors)]);

Get the nearest neighbors to a vector

const result = await db.any('SELECT * FROM items ORDER BY factors <-> $1 LIMIT 5', [pgvector.toSql(factors)]);

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
createdb pgvector_node_test
npm test

About

pgvector support for Node.js, Deno, and Bun (and TypeScript)

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors