Gathering detailed insights and metrics for pgvector
Gathering detailed insights and metrics for pgvector
Gathering detailed insights and metrics for pgvector
Gathering detailed insights and metrics for pgvector
pgvector support for Node.js, Deno, and Bun (and TypeScript)
npm install pgvector
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.4
Supply Chain
99.5
Quality
77.2
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
2,067,357
Last Day
12,294
Last Week
60,535
Last Month
185,761
Last Year
1,930,818
342 Stars
325 Commits
12 Forks
6 Watching
4 Branches
3 Contributors
Latest Version
0.2.0
Package Id
pgvector@0.2.0
Unpacked Size
39.38 kB
Size
8.89 kB
File Count
37
NPM Version
10.7.0
Node Version
22.2.0
Publised On
28 Jun 2024
Cumulative downloads
Total Downloads
Last day
3.9%
12,294
Compared to previous day
Last week
12.9%
60,535
Compared to previous week
Last month
-13.1%
185,761
Compared to previous month
Last year
1,316.6%
1,930,818
Compared to previous year
pgvector support for Node.js and Bun (and TypeScript)
Supports node-postgres, Knex.js, Objection.js, Kysely, Sequelize, pg-promise, Prisma, Postgres.js, Slonik, TypeORM, MikroORM, and Drizzle ORM
Run:
1npm install pgvector
And follow the instructions for your database library:
Or check out some examples:
COPY
Enable the extension
1await client.query('CREATE EXTENSION IF NOT EXISTS vector');
Register the type for a client
1import pgvector from 'pgvector/pg'; 2 3await pgvector.registerType(client);
or a pool
1pool.on('connect', async function (client) { 2 await pgvector.registerType(client); 3});
Create a table
1await client.query('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
Insert a vector
1await client.query('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql([1, 2, 3])]);
Get the nearest neighbors to a vector
1const result = await client.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [pgvector.toSql([1, 2, 3])]);
Add an approximate index
1await client.query('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)'); 2// or 3await client.query('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Import the library
1import pgvector from 'pgvector/knex';
Enable the extension
1await knex.schema.enableExtension('vector');
Create a table
1await knex.schema.createTable('items', (table) => { 2 table.increments('id'); 3 table.vector('embedding', 3); 4});
Insert vectors
1const newItems = [ 2 {embedding: pgvector.toSql([1, 2, 3])}, 3 {embedding: pgvector.toSql([4, 5, 6])} 4]; 5await knex('items').insert(newItems);
Get the nearest neighbors to a vector
1const items = await knex('items') 2 .orderBy(knex.l2Distance('embedding', [1, 2, 3])) 3 .limit(5);
Also supports maxInnerProduct
and cosineDistance
Add an approximate index
1await knex.schema.alterTable('items', function (table) { 2 table.index(knex.raw('embedding vector_l2_ops'), 'index_name', 'hnsw'); 3});
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Import the library
1import pgvector from 'pgvector/objection';
Enable the extension
1await knex.schema.enableExtension('vector');
Create a table
1await knex.schema.createTable('items', (table) => { 2 table.increments('id'); 3 table.vector('embedding', 3); 4});
Insert vectors
1const newItems = [ 2 {embedding: pgvector.toSql([1, 2, 3])}, 3 {embedding: pgvector.toSql([4, 5, 6])} 4]; 5await Item.query().insert(newItems);
Get the nearest neighbors to a vector
1import { l2Distance } from 'pgvector/objection'; 2 3const items = await Item.query() 4 .orderBy(l2Distance('embedding', [1, 2, 3])) 5 .limit(5);
Also supports maxInnerProduct
and cosineDistance
Add an approximate index
1await knex.schema.alterTable('items', function (table) { 2 table.index(knex.raw('embedding vector_l2_ops'), 'index_name', 'hnsw'); 3});
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Enable the extension
1await sql`CREATE EXTENSION IF NOT EXISTS vector`.execute(db);
Create a table
1await db.schema.createTable('items') 2 .addColumn('id', 'serial', (cb) => cb.primaryKey()) 3 .addColumn('embedding', sql`vector(3)`) 4 .execute();
Insert vectors
1import pgvector from 'pgvector/kysely'; 2 3const newItems = [ 4 {embedding: pgvector.toSql([1, 2, 3])}, 5 {embedding: pgvector.toSql([4, 5, 6])} 6]; 7await db.insertInto('items').values(newItems).execute();
Get the nearest neighbors to a vector
1import { l2Distance } from 'pgvector/kysely'; 2 3const items = await db.selectFrom('items') 4 .selectAll() 5 .orderBy(l2Distance('embedding', [1, 2, 3])) 6 .limit(5) 7 .execute();
Also supports maxInnerProduct
and cosineDistance
Add an approximate index
1await db.schema.createIndex('index_name') 2 .on('items') 3 .using('hnsw') 4 .expression(sql`embedding vector_l2_ops`) 5 .execute();
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Enable the extension
1await sequelize.query('CREATE EXTENSION IF NOT EXISTS vector');
Register the type
1import { Sequelize } from 'sequelize'; 2import pgvector from 'pgvector/sequelize'; 3 4pgvector.registerType(Sequelize);
Add a vector field
1const Item = sequelize.define('Item', { 2 embedding: { 3 type: DataTypes.VECTOR(3) 4 } 5}, ...);
Insert a vector
1await Item.create({embedding: [1, 2, 3]});
Get the nearest neighbors to a vector
1const items = await Item.findAll({ 2 order: l2Distance('embedding', [1, 1, 1], sequelize), 3 limit: 5 4});
Also supports maxInnerProduct
and cosineDistance
Add an approximate index
1const Item = sequelize.define('Item', ..., { 2 indexes: [ 3 { 4 fields: ['embedding'], 5 using: 'hnsw', 6 operator: 'vector_l2_ops' 7 } 8 ] 9});
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Enable the extension
1await db.none('CREATE EXTENSION IF NOT EXISTS vector');
Register the type
1import pgpromise from 'pg-promise'; 2import pgvector from 'pgvector/pg-promise'; 3 4const initOptions = { 5 async connect(e) { 6 await pgvector.registerType(e.client); 7 } 8}; 9const pgp = pgpromise(initOptions);
Create a table
1await db.none('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
Insert a vector
1await db.none('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql([1, 2, 3])]);
Get the nearest neighbors to a vector
1const result = await db.any('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [pgvector.toSql([1, 2, 3])]);
Add an approximate index
1await db.none('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)'); 2// or 3await db.none('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Note: prisma migrate dev
does not support pgvector indexes
Import the library
1import pgvector from 'pgvector';
Add the extension to the schema
1generator client { 2 provider = "prisma-client-js" 3 previewFeatures = ["postgresqlExtensions"] 4} 5 6datasource db { 7 provider = "postgresql" 8 url = env("DATABASE_URL") 9 extensions = [vector] 10}
Add a vector column to the schema
1model Item { 2 id Int @id @default(autoincrement()) 3 embedding Unsupported("vector(3)")? 4}
Insert a vector
1const embedding = pgvector.toSql([1, 2, 3]) 2await prisma.$executeRaw`INSERT INTO items (embedding) VALUES (${embedding}::vector)`
Get the nearest neighbors to a vector
1const embedding = pgvector.toSql([1, 2, 3]) 2const items = await prisma.$queryRaw`SELECT id, embedding::text FROM items ORDER BY embedding <-> ${embedding}::vector LIMIT 5`
See a full example (and the schema)
Import the library
1import pgvector from 'pgvector';
Enable the extension
1await sql`CREATE EXTENSION IF NOT EXISTS vector`;
Create a table
1await sql`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;
Insert vectors
1const newItems = [ 2 {embedding: pgvector.toSql([1, 2, 3])}, 3 {embedding: pgvector.toSql([4, 5, 6])} 4]; 5await sql`INSERT INTO items ${ sql(newItems, 'embedding') }`;
Get the nearest neighbors to a vector
1const embedding = pgvector.toSql([1, 2, 3]); 2const items = await sql`SELECT * FROM items ORDER BY embedding <-> ${ embedding } LIMIT 5`;
Add an approximate index
1await sql`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`; 2// or 3await sql`CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)`;
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Import the library
1import pgvector from 'pgvector';
Enable the extension
1await pool.query(sql.unsafe`CREATE EXTENSION IF NOT EXISTS vector`);
Create a table
1await pool.query(sql.unsafe`CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))`);
Insert a vector
1const embedding = pgvector.toSql([1, 2, 3]); 2await pool.query(sql.unsafe`INSERT INTO items (embedding) VALUES (${embedding})`);
Get the nearest neighbors to a vector
1const embedding = pgvector.toSql([1, 2, 3]); 2const items = await pool.query(sql.unsafe`SELECT * FROM items ORDER BY embedding <-> ${embedding} LIMIT 5`);
Add an approximate index
1await pool.query(sql.unsafe`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`); 2// or 3await pool.query(sql.unsafe`CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)`);
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Import the library
1import pgvector from 'pgvector';
Enable the extension
1await AppDataSource.query('CREATE EXTENSION IF NOT EXISTS vector');
Create a table
1await AppDataSource.query('CREATE TABLE item (id bigserial PRIMARY KEY, embedding vector(3))');
Define an entity
1@Entity() 2class Item { 3 @PrimaryGeneratedColumn() 4 id: number 5 6 @Column() 7 embedding: string 8}
Insert a vector
1const itemRepository = AppDataSource.getRepository(Item); 2await itemRepository.save({embedding: pgvector.toSql([1, 2, 3])});
Get the nearest neighbors to a vector
1const items = await itemRepository 2 .createQueryBuilder('item') 3 .orderBy('embedding <-> :embedding') 4 .setParameters({embedding: pgvector.toSql([1, 2, 3])}) 5 .limit(5) 6 .getMany();
See a full example
Enable the extension
1await em.execute('CREATE EXTENSION IF NOT EXISTS vector');
Define an entity
1import { VectorType } from 'pgvector/mikro-orm'; 2 3@Entity() 4class Item { 5 @PrimaryKey() 6 id: number; 7 8 @Property({type: VectorType}) 9 embedding: number[]; 10}
Insert a vector
1em.create(Item, {embedding: [1, 2, 3]});
Get the nearest neighbors to a vector
1import { l2Distance } from 'pgvector/mikro-orm'; 2 3const items = await em.createQueryBuilder(Item) 4 .orderBy({[l2Distance('embedding', [1, 2, 3])]: 'ASC'}) 5 .limit(5) 6 .getResult();
Also supports maxInnerProduct
and cosineDistance
See a full example
Drizzle ORM 0.31.0+ has built-in support for pgvector :tada:
Enable the extension
1await client`CREATE EXTENSION IF NOT EXISTS vector`;
Add a vector field
1import { vector } from 'drizzle-orm/pg-core'; 2 3const items = pgTable('items', { 4 id: serial('id').primaryKey(), 5 embedding: vector('embedding', {dimensions: 3}) 6});
Also supports halfvec
, bit
, and sparsevec
Insert vectors
1const newItems = [ 2 {embedding: [1, 2, 3]}, 3 {embedding: [4, 5, 6]} 4]; 5await db.insert(items).values(newItems);
Get the nearest neighbors to a vector
1import { l2Distance } from 'drizzle-orm'; 2 3const allItems = await db.select() 4 .from(items) 5 .orderBy(l2Distance(items.embedding, [1, 2, 3])) 6 .limit(5);
Also supports innerProduct
, cosineDistance
, l1Distance
, hammingDistance
, and jaccardDistance
See a full example
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
To get started with development:
1git clone https://github.com/pgvector/pgvector-node.git 2cd pgvector-node 3npm install 4createdb pgvector_node_test 5npx prisma migrate dev 6npm test
No vulnerabilities found.
No security vulnerabilities found.