{"id":14384362,"url":"https://github.com/pgvector/pgvector-node","last_synced_at":"2025-06-20T10:40:15.695Z","repository":{"id":66166613,"uuid":"378272721","full_name":"pgvector/pgvector-node","owner":"pgvector","description":"pgvector support for Node.js, Deno, and Bun (and TypeScript)","archived":false,"fork":false,"pushed_at":"2025-05-20T22:52:36.000Z","size":274,"stargazers_count":378,"open_issues_count":2,"forks_count":16,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-20T23:36:24.309Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgvector.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-06-18T21:59:24.000Z","updated_at":"2025-05-20T22:52:36.000Z","dependencies_parsed_at":"2023-12-19T00:40:11.643Z","dependency_job_id":"effa94e6-66bc-42c8-a62d-e3e7c043b98c","html_url":"https://github.com/pgvector/pgvector-node","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/pgvector/pgvector-node","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgvector","download_url":"https://codeload.github.com/pgvector/pgvector-node/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-node/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260927970,"owners_count":23084130,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-28T18:01:20.023Z","updated_at":"2025-06-20T10:40:10.676Z","avatar_url":"https://github.com/pgvector.png","language":"JavaScript","readme":"# pgvector-node\n\n[pgvector](https://github.com/pgvector/pgvector) support for Node.js, Deno, and Bun (and TypeScript)\n\nSupports [node-postgres](https://github.com/brianc/node-postgres), [Knex.js](https://github.com/knex/knex), [Objection.js](https://github.com/vincit/objection.js), [Kysely](https://github.com/kysely-org/kysely), [Sequelize](https://github.com/sequelize/sequelize), [pg-promise](https://github.com/vitaly-t/pg-promise), [Prisma](https://github.com/prisma/prisma), [Postgres.js](https://github.com/porsager/postgres), [Slonik](https://github.com/gajus/slonik), [TypeORM](https://github.com/typeorm/typeorm), [MikroORM](https://github.com/mikro-orm/mikro-orm), and [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm)\n\n[![Build Status](https://github.com/pgvector/pgvector-node/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-node/actions)\n\n## Installation\n\nRun:\n\n```sh\nnpm install pgvector\n```\n\nAnd follow the instructions for your database library:\n\n- [node-postgres](#node-postgres)\n- [Knex.js](#knexjs)\n- [Objection.js](#objectionjs)\n- [Kysely](#kysely)\n- [Sequelize](#sequelize)\n- [pg-promise](#pg-promise)\n- [Prisma](#prisma)\n- [Postgres.js](#postgresjs)\n- [Slonik](#slonik)\n- [TypeORM](#typeorm)\n- [MikroORM](#mikroorm)\n- [Drizzle ORM](#drizzle-orm)\n\nOr check out some examples:\n\n- [Embeddings](examples/openai/example.js) with OpenAI\n- [Binary embeddings](examples/cohere/example.js) with Cohere\n- [Sentence embeddings](examples/transformers/example.js) with Transformers.js\n- [Hybrid search](examples/hybrid-search/example.js) with Transformers.js\n- [Morgan fingerprints](examples/rdkit/example.js) with RDKit.js\n- [Recommendations](examples/disco/example.js) with Disco\n- [Horizontal scaling](examples/citus/example.js) with Citus\n- [WebAssembly](examples/pglite/example.js) with PGLite\n- [Bulk loading](examples/loading/example.js) with `COPY`\n\n## node-postgres\n\nEnable the extension\n\n```javascript\nawait client.query('CREATE EXTENSION IF NOT EXISTS vector');\n```\n\nRegister the types for a client\n\n```javascript\nimport pgvector from 'pgvector/pg';\n\nawait pgvector.registerTypes(client);\n```\n\nor a pool\n\n```javascript\npool.on('connect', async function (client) {\n  await pgvector.registerTypes(client);\n});\n```\n\nCreate a table\n\n```javascript\nawait client.query('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');\n```\n\nInsert a vector\n\n```javascript\nawait client.query('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql([1, 2, 3])]);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst result = await client.query('SELECT * FROM items ORDER BY embedding \u003c-\u003e $1 LIMIT 5', [pgvector.toSql([1, 2, 3])]);\n```\n\nAdd an approximate index\n\n```javascript\nawait client.query('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');\n// or\nawait client.query('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/pg.test.mjs)\n\n## Knex.js\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector/knex';\n```\n\nEnable the extension\n\n```javascript\nawait knex.schema.createExtensionIfNotExists('vector');\n```\n\nCreate a table\n\n```javascript\nawait knex.schema.createTable('items', (table) =\u003e {\n  table.increments('id');\n  table.vector('embedding', 3);\n});\n```\n\nInsert vectors\n\n```javascript\nconst newItems = [\n  {embedding: pgvector.toSql([1, 2, 3])},\n  {embedding: pgvector.toSql([4, 5, 6])}\n];\nawait knex('items').insert(newItems);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst items = await knex('items')\n  .orderBy(knex.l2Distance('embedding', [1, 2, 3]))\n  .limit(5);\n```\n\nAlso supports `maxInnerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nAdd an approximate index\n\n```javascript\nawait knex.schema.alterTable('items', function (table) {\n  table.index(knex.raw('embedding vector_l2_ops'), 'index_name', 'hnsw');\n});\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/knex.test.mjs)\n\n## Objection.js\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector/objection';\n```\n\nEnable the extension\n\n```javascript\nawait knex.schema.createExtensionIfNotExists('vector');\n```\n\nCreate a table\n\n```javascript\nawait knex.schema.createTable('items', (table) =\u003e {\n  table.increments('id');\n  table.vector('embedding', 3);\n});\n```\n\nInsert vectors\n\n```javascript\nconst newItems = [\n  {embedding: pgvector.toSql([1, 2, 3])},\n  {embedding: pgvector.toSql([4, 5, 6])}\n];\nawait Item.query().insert(newItems);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nimport { l2Distance } from 'pgvector/objection';\n\nconst items = await Item.query()\n  .orderBy(l2Distance('embedding', [1, 2, 3]))\n  .limit(5);\n```\n\nAlso supports `maxInnerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nAdd an approximate index\n\n```javascript\nawait knex.schema.alterTable('items', function (table) {\n  table.index(knex.raw('embedding vector_l2_ops'), 'index_name', 'hnsw');\n});\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/objection.test.mjs)\n\n## Kysely\n\nEnable the extension\n\n```javascript\nawait sql`CREATE EXTENSION IF NOT EXISTS vector`.execute(db);\n```\n\nCreate a table\n\n```javascript\nawait db.schema.createTable('items')\n  .addColumn('id', 'serial', (cb) =\u003e cb.primaryKey())\n  .addColumn('embedding', sql`vector(3)`)\n  .execute();\n```\n\nInsert vectors\n\n```javascript\nimport pgvector from 'pgvector/kysely';\n\nconst newItems = [\n  {embedding: pgvector.toSql([1, 2, 3])},\n  {embedding: pgvector.toSql([4, 5, 6])}\n];\nawait db.insertInto('items').values(newItems).execute();\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nimport { l2Distance } from 'pgvector/kysely';\n\nconst items = await db.selectFrom('items')\n  .selectAll()\n  .orderBy(l2Distance('embedding', [1, 2, 3]))\n  .limit(5)\n  .execute();\n```\n\nAlso supports `maxInnerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nGet items within a certain distance\n\n```javascript\nconst items = await db.selectFrom('items')\n  .selectAll()\n  .where(l2Distance('embedding', [1, 2, 3]), '\u003c', 5)\n  .execute();\n```\n\nAdd an approximate index\n\n```javascript\nawait db.schema.createIndex('index_name')\n  .on('items')\n  .using('hnsw')\n  .expression(sql`embedding vector_l2_ops`)\n  .execute();\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/kysely.test.mjs)\n\n## Sequelize\n\nEnable the extension\n\n```javascript\nawait sequelize.query('CREATE EXTENSION IF NOT EXISTS vector');\n```\n\nRegister the types\n\n```javascript\nimport { Sequelize } from 'sequelize';\nimport pgvector from 'pgvector/sequelize';\n\npgvector.registerTypes(Sequelize);\n```\n\nAdd a vector field\n\n```javascript\nconst Item = sequelize.define('Item', {\n  embedding: {\n    type: DataTypes.VECTOR(3)\n  }\n}, ...);\n```\n\nInsert a vector\n\n```javascript\nawait Item.create({embedding: [1, 2, 3]});\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst items = await Item.findAll({\n  order: l2Distance('embedding', [1, 1, 1], sequelize),\n  limit: 5\n});\n```\n\nAlso supports `maxInnerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nAdd an approximate index\n\n```javascript\nconst Item = sequelize.define('Item', ..., {\n  indexes: [\n    {\n      fields: ['embedding'],\n      using: 'hnsw',\n      operator: 'vector_l2_ops'\n    }\n  ]\n});\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/sequelize.test.mjs)\n\n## pg-promise\n\nEnable the extension\n\n```javascript\nawait db.none('CREATE EXTENSION IF NOT EXISTS vector');\n```\n\nRegister the types\n\n```javascript\nimport pgpromise from 'pg-promise';\nimport pgvector from 'pgvector/pg-promise';\n\nconst initOptions = {\n  async connect(e) {\n    await pgvector.registerTypes(e.client);\n  }\n};\nconst pgp = pgpromise(initOptions);\n```\n\nCreate a table\n\n```javascript\nawait db.none('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');\n```\n\nInsert a vector\n\n```javascript\nawait db.none('INSERT INTO items (embedding) VALUES ($1)', [pgvector.toSql([1, 2, 3])]);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst result = await db.any('SELECT * FROM items ORDER BY embedding \u003c-\u003e $1 LIMIT 5', [pgvector.toSql([1, 2, 3])]);\n```\n\nAdd an approximate index\n\n```javascript\nawait db.none('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');\n// or\nawait db.none('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/pg-promise.test.mjs)\n\n## Prisma\n\nNote: `prisma migrate dev` does not support pgvector indexes\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector';\n```\n\nAdd the extension to the schema\n\n```prisma\ngenerator client {\n  provider        = \"prisma-client-js\"\n  previewFeatures = [\"postgresqlExtensions\"]\n}\n\ndatasource db {\n  provider   = \"postgresql\"\n  url        = env(\"DATABASE_URL\")\n  extensions = [vector]\n}\n```\n\nAdd a vector column to the schema\n\n```prisma\nmodel Item {\n  id        Int                       @id @default(autoincrement())\n  embedding Unsupported(\"vector(3)\")?\n}\n```\n\nInsert a vector\n\n```javascript\nconst embedding = pgvector.toSql([1, 2, 3])\nawait prisma.$executeRaw`INSERT INTO items (embedding) VALUES (${embedding}::vector)`\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst embedding = pgvector.toSql([1, 2, 3])\nconst items = await prisma.$queryRaw`SELECT id, embedding::text FROM items ORDER BY embedding \u003c-\u003e ${embedding}::vector LIMIT 5`\n```\n\nSee a [full example](tests/prisma.test.mjs) (and the [schema](prisma/schema.prisma))\n\n## Postgres.js\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector';\n```\n\nEnable the extension\n\n```javascript\nawait sql`CREATE EXTENSION IF NOT EXISTS vector`;\n```\n\nCreate a table\n\n```javascript\nawait sql`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;\n```\n\nInsert vectors\n\n```javascript\nconst newItems = [\n  {embedding: pgvector.toSql([1, 2, 3])},\n  {embedding: pgvector.toSql([4, 5, 6])}\n];\nawait sql`INSERT INTO items ${ sql(newItems, 'embedding') }`;\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst embedding = pgvector.toSql([1, 2, 3]);\nconst items = await sql`SELECT * FROM items ORDER BY embedding \u003c-\u003e ${ embedding } LIMIT 5`;\n```\n\nAdd an approximate index\n\n```javascript\nawait sql`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`;\n// or\nawait sql`CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)`;\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/postgres.test.mjs)\n\n## Slonik\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector';\n```\n\nEnable the extension\n\n```javascript\nawait pool.query(sql.unsafe`CREATE EXTENSION IF NOT EXISTS vector`);\n```\n\nCreate a table\n\n```javascript\nawait pool.query(sql.unsafe`CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))`);\n```\n\nInsert a vector\n\n```javascript\nconst embedding = pgvector.toSql([1, 2, 3]);\nawait pool.query(sql.unsafe`INSERT INTO items (embedding) VALUES (${embedding})`);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst embedding = pgvector.toSql([1, 2, 3]);\nconst items = await pool.query(sql.unsafe`SELECT * FROM items ORDER BY embedding \u003c-\u003e ${embedding} LIMIT 5`);\n```\n\nAdd an approximate index\n\n```javascript\nawait pool.query(sql.unsafe`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`);\n// or\nawait pool.query(sql.unsafe`CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)`);\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\nSee a [full example](tests/slonik.test.mjs)\n\n## TypeORM\n\nImport the library\n\n```javascript\nimport pgvector from 'pgvector';\n```\n\nEnable the extension\n\n```javascript\nawait AppDataSource.query('CREATE EXTENSION IF NOT EXISTS vector');\n```\n\nCreate a table\n\n```javascript\nawait AppDataSource.query('CREATE TABLE item (id bigserial PRIMARY KEY, embedding vector(3))');\n```\n\nDefine an entity\n\n```typescript\n@Entity()\nclass Item {\n  @PrimaryGeneratedColumn()\n  id: number\n\n  @Column()\n  embedding: string\n}\n```\n\nInsert a vector\n\n```javascript\nconst itemRepository = AppDataSource.getRepository(Item);\nawait itemRepository.save({embedding: pgvector.toSql([1, 2, 3])});\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nconst items = await itemRepository\n  .createQueryBuilder('item')\n  .orderBy('embedding \u003c-\u003e :embedding')\n  .setParameters({embedding: pgvector.toSql([1, 2, 3])})\n  .limit(5)\n  .getMany();\n```\n\nSee a [full example](tests/typeorm.test.mjs)\n\n## MikroORM\n\nEnable the extension\n\n```javascript\nawait em.execute('CREATE EXTENSION IF NOT EXISTS vector');\n```\n\nDefine an entity\n\n```typescript\nimport { VectorType } from 'pgvector/mikro-orm';\n\n@Entity()\nclass Item {\n  @PrimaryKey()\n  id: number;\n\n  @Property({type: VectorType})\n  embedding: number[];\n}\n```\n\nInsert a vector\n\n```javascript\nem.create(Item, {embedding: [1, 2, 3]});\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nimport { l2Distance } from 'pgvector/mikro-orm';\n\nconst items = await em.createQueryBuilder(Item)\n  .orderBy({[l2Distance('embedding', [1, 2, 3])]: 'ASC'})\n  .limit(5)\n  .getResult();\n```\n\nAlso supports `maxInnerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nSee a [full example](tests/mikro-orm.test.mjs)\n\n## Drizzle ORM\n\nDrizzle ORM 0.31.0+ has [built-in support](https://orm.drizzle.team/docs/extensions/pg#pg_vector) for pgvector :tada:\n\nEnable the extension\n\n```javascript\nawait client`CREATE EXTENSION IF NOT EXISTS vector`;\n```\n\nAdd a vector field\n\n```javascript\nimport { vector } from 'drizzle-orm/pg-core';\n\nconst items = pgTable('items', {\n  id: serial('id').primaryKey(),\n  embedding: vector('embedding', {dimensions: 3})\n});\n```\n\nAlso supports `halfvec`, `bit`, and `sparsevec`\n\nInsert vectors\n\n```javascript\nconst newItems = [\n  {embedding: [1, 2, 3]},\n  {embedding: [4, 5, 6]}\n];\nawait db.insert(items).values(newItems);\n```\n\nGet the nearest neighbors to a vector\n\n```javascript\nimport { l2Distance } from 'drizzle-orm';\n\nconst allItems = await db.select()\n  .from(items)\n  .orderBy(l2Distance(items.embedding, [1, 2, 3]))\n  .limit(5);\n```\n\nAlso supports `innerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`\n\nSee a [full example](tests/drizzle-orm.test.mjs)\n\n## History\n\nView the [changelog](https://github.com/pgvector/pgvector-node/blob/master/CHANGELOG.md)\n\n## Contributing\n\nEveryone is encouraged to help improve this project. Here are a few ways you can help:\n\n- [Report bugs](https://github.com/pgvector/pgvector-node/issues)\n- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-node/pulls)\n- Write, clarify, or fix documentation\n- Suggest or add new features\n\nTo get started with development:\n\n```sh\ngit clone https://github.com/pgvector/pgvector-node.git\ncd pgvector-node\nnpm install\ncreatedb pgvector_node_test\nnpx prisma migrate dev\nnpm test\n```\n\nTo run an example:\n\n```sh\ncd examples/loading\nnpm install\ncreatedb pgvector_example\nnode example.js\n```\n","funding_links":[],"categories":["JavaScript","Sdks \u0026 Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgvector%2Fpgvector-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgvector%2Fpgvector-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgvector%2Fpgvector-node/lists"}