pgvector support for Node.js and typescript
npm install pgvector-tspgvector support for Node.js Typescript
Supports Sequelize, node-postgres, and pg-promise

Run:
``sh`
npm install pgvector-ts
And follow the instructions for your database library:
- pgvector-ts
- Installation
- Sequelize
- node-postgres
- pg-promise
- History
- Contributing
Register the type
`js
const { Sequelize } = require('sequelize-typescript');
const pgvector = require('pgvector-ts/sequelize');
pgvector.registerType(Sequelize);
`
Add a vector field
`js`
Item.init({
embedding: {
type: DataTypes.VECTOR(3)
}
}, ...);
Insert a vector
`js`
await Item.create({embedding: [1, 2, 3]});
Get the nearest neighbors to a vector
`jsembedding <-> '[1, 2, 3]'
const items = await Item.findAll({
order: [sequelize.literal()],`
limit: 5
});
Register the type
`js
const pgvector = require('pgvector-ts/pg');
await pgvector.registerType(client);
`
Insert a vector
`js`
const embedding = [1, 2, 3];
await client.query('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql(embedding)]);
Get the nearest neighbors to a vector
`js`
const result = await client.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [pgvector.toSql(embedding)]);
Register the type
`js
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
`js`
const embedding = [1, 2, 3];
await db.none('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql(embedding)]);
Get the nearest neighbors to a vector
`js`
const result = await db.any('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [pgvector.toSql(embedding)]);
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:
`sh``
git clone https://github.com/pgvector/pgvector-node.git
cd pgvector-node
npm install
createdb pgvector_node_test
npm test