-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexample.js
More file actions
20 lines (14 loc) · 870 Bytes
/
example.js
File metadata and controls
20 lines (14 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Client } from 'jsr:@db/postgres';
import pgvector from 'npm:pgvector';
const client = new Client({database: 'pgvector_example', hostname: 'localhost', user: Deno.env.get('USER'), tls: {enabled: false}});
await client.connect();
await client.queryArray`CREATE EXTENSION IF NOT EXISTS vector`;
await client.queryArray`DROP TABLE IF EXISTS items`;
await client.queryArray`CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))`;
const embedding = pgvector.toSql([1, 2, 3]);
const embedding2 = pgvector.toSql([4, 5, 6]);
await client.queryArray`INSERT INTO items (embedding) VALUES (${embedding}), (${embedding2})`;
await client.queryArray`CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)`;
const { rows } = await client.queryArray`SELECT * FROM items ORDER BY embedding <-> ${embedding} LIMIT 5`;
console.log(rows);
await client.end();