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