Skip to content

Commit 61973ff

Browse files
committed
Added Bun SQL example
1 parent 2e8ef81 commit 61973ff

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ jobs:
2727
- run: bun install
2828
- run: bunx prisma migrate reset --force
2929
- run: bun run test
30+
- run: |
31+
cd examples/bun
32+
bun install
33+
bun run example.js
3034
3135
# Deno
3236
- uses: denoland/setup-deno@v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
package-lock.json
3+
bun.lock
34
bun.lockb

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[pgvector](https://github.com/pgvector/pgvector) support for Node.js, Deno, and Bun (and TypeScript)
44

5-
Supports [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)
5+
Supports [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), [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm), and [Bun SQL](https://bun.sh/docs/api/sql)
66

77
[![Build Status](https://github.com/pgvector/pgvector-node/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-node/actions)
88

@@ -28,6 +28,7 @@ And follow the instructions for your database library:
2828
- [TypeORM](#typeorm)
2929
- [MikroORM](#mikroorm)
3030
- [Drizzle ORM](#drizzle-orm)
31+
- [Bun SQL](#bun-sql)
3132

3233
Or check out some examples:
3334

@@ -678,6 +679,55 @@ Also supports `innerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`,
678679

679680
See a [full example](tests/drizzle-orm.test.mjs)
680681

682+
## Bun SQL
683+
684+
Import the library
685+
686+
```javascript
687+
import pgvector from 'pgvector';
688+
```
689+
690+
Enable the extension
691+
692+
```javascript
693+
await sql`CREATE EXTENSION IF NOT EXISTS vector`;
694+
```
695+
696+
Create a table
697+
698+
```javascript
699+
await sql`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;
700+
```
701+
702+
Insert vectors
703+
704+
```javascript
705+
const newItems = [
706+
{embedding: pgvector.toSql([1, 2, 3])},
707+
{embedding: pgvector.toSql([4, 5, 6])}
708+
];
709+
await sql`INSERT INTO items (embedding) ${sql(newItems)}`;
710+
```
711+
712+
Get the nearest neighbors to a vector
713+
714+
```javascript
715+
const embedding = pgvector.toSql([1, 2, 3]);
716+
const items = await sql`SELECT * FROM items ORDER BY embedding <-> ${embedding} LIMIT 5`.values();
717+
```
718+
719+
Add an approximate index
720+
721+
```javascript
722+
await sql`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`;
723+
// or
724+
await sql`CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)`;
725+
```
726+
727+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
728+
729+
See a [full example](examples/bun/example.js)
730+
681731
## History
682732

683733
View the [changelog](https://github.com/pgvector/pgvector-node/blob/master/CHANGELOG.md)

examples/bun/example.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SQL } from 'bun';
2+
import pgvector from 'pgvector';
3+
4+
const sql = new SQL({database: 'pgvector_example'});
5+
6+
await sql`CREATE EXTENSION IF NOT EXISTS vector`;
7+
await sql`DROP TABLE IF EXISTS items`;
8+
await sql`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;
9+
10+
const item = {
11+
embedding: pgvector.toSql([1, 2, 3])
12+
};
13+
await sql`INSERT INTO items (embedding) ${sql(item)}`;
14+
15+
const items = [
16+
{embedding: pgvector.toSql([4, 5, 6])},
17+
{embedding: pgvector.toSql([7, 8, 9])}
18+
];
19+
await sql`INSERT INTO items (embedding) ${sql(items)}`;
20+
21+
await sql`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`;
22+
23+
const rows = await sql`SELECT * FROM items ORDER BY embedding <-> ${item.embedding} LIMIT 5`.values();
24+
console.log(rows);

examples/bun/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"type": "module",
4+
"dependencies": {
5+
"pgvector": "file:../.."
6+
}
7+
}

0 commit comments

Comments
 (0)