Skip to content

Commit 1eb1445

Browse files
committed
Added deno-postgres example
1 parent 9ab6801 commit 1eb1445

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ jobs:
4242
deno-version: v2.x
4343
- run: deno install
4444
- run: deno task test
45+
- run: |
46+
cd examples/deno
47+
deno install
48+
deno run --allow-env --allow-net example.js

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ package-lock.json
55
pnpm-lock.yaml
66
bun.lock
77
bun.lockb
8+
deno.lock

README.md

Lines changed: 48 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), [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm), and [Bun SQL](https://bun.sh/docs/api/sql)
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), [deno-postgres](https://github.com/denodrivers/postgres), 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+
- [deno-postgres](#deno-postgres)
3132
- [Bun SQL](#bun-sql)
3233

3334
Or check out some examples:
@@ -678,6 +679,52 @@ Also supports `innerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`,
678679

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

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

683730
Import the library

examples/deno/deno.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"imports": {
3+
"@db/postgres": "jsr:@db/postgres@^0.19.5",
4+
"pgvector": "npm:pgvector@^0.2.1"
5+
}
6+
}

examples/deno/example.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Client } from 'jsr:@db/postgres';
2+
import pgvector from 'npm:pgvector';
3+
4+
const client = new Client({database: 'pgvector_example', hostname: 'localhost', user: Deno.env.get('USER'), tls: {enabled: false}});
5+
await client.connect();
6+
7+
await client.queryArray`CREATE EXTENSION IF NOT EXISTS vector`;
8+
await client.queryArray`DROP TABLE IF EXISTS items`;
9+
await client.queryArray`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;
10+
11+
const embedding = pgvector.toSql([1, 2, 3]);
12+
const embedding2 = pgvector.toSql([4, 5, 6]);
13+
await client.queryArray`INSERT INTO items (embedding) VALUES (${embedding}), (${embedding2})`;
14+
15+
await client.queryArray`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`;
16+
17+
const { rows } = await client.queryArray`SELECT * FROM items ORDER BY embedding <-> ${embedding} LIMIT 5`;
18+
console.log(rows);
19+
20+
await client.end();

0 commit comments

Comments
 (0)