Skip to content

Commit

Permalink
Added Kysely example
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Dec 13, 2023
1 parent 22c16d3 commit 898551c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[pgvector](https:/pgvector/pgvector) support for Node.js and Bun (and TypeScript)

Supports [node-postgres](https:/brianc/node-postgres), [Knex.js](https:/knex/knex), [Objection.js](https:/vincit/objection.js), [Sequelize](https:/sequelize/sequelize), [pg-promise](https:/vitaly-t/pg-promise), [Prisma](https:/prisma/prisma), [Postgres.js](https:/porsager/postgres), [TypeORM](https:/typeorm/typeorm), [MikroORM](https:/mikro-orm/mikro-orm), and [Drizzle ORM](https:/drizzle-team/drizzle-orm)
Supports [node-postgres](https:/brianc/node-postgres), [Knex.js](https:/knex/knex), [Objection.js](https:/vincit/objection.js), [Kysely](https:/kysely-org/kysely), [Sequelize](https:/sequelize/sequelize), [pg-promise](https:/vitaly-t/pg-promise), [Prisma](https:/prisma/prisma), [Postgres.js](https:/porsager/postgres), [TypeORM](https:/typeorm/typeorm), [MikroORM](https:/mikro-orm/mikro-orm), and [Drizzle ORM](https:/drizzle-team/drizzle-orm)

[![Build Status](https:/pgvector/pgvector-node/workflows/build/badge.svg?branch=master)](https:/pgvector/pgvector-node/actions)

Expand All @@ -19,6 +19,7 @@ And follow the instructions for your database library:
- [node-postgres](#node-postgres)
- [Knex.js](#knexjs)
- [Objection.js](#objectionjs)
- [Kysely](#kysely)
- [Sequelize](#sequelize)
- [pg-promise](#pg-promise)
- [Prisma](#prisma)
Expand Down Expand Up @@ -198,6 +199,65 @@ Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distanc

See a [full example](tests/objection/index.test.mjs)

## Kysely

Import the library

```javascript
import pgvector from 'pgvector/utils';
```

Enable the extension

```javascript
await sql`CREATE EXTENSION IF NOT EXISTS vector`.execute(db);
```

Create a table

```javascript
await db.schema.createTable('items')
.addColumn('id', 'serial', (cb) => cb.primaryKey())
.addColumn('embedding', 'vector(3)')
.execute();
```

Insert vectors

```javascript
const newItems = [
{embedding: pgvector.toSql([1, 2, 3])},
{embedding: pgvector.toSql([4, 5, 6])}
];
await db.insertInto('items').values(newItems).execute();
```

Get the nearest neighbors to a vector

```javascript
const items = await db.selectFrom('items')
.selectAll()
.orderBy(sql`embedding <-> '[1,1,1]'`)
.limit(5)
.execute();
```

Also supports `maxInnerProduct` and `cosineDistance`

Add an approximate index

```javascript
await db.schema.createIndex('index_name')
.on('items')
.using('hnsw')
.expression(sql`embedding vector_l2_ops`)
.execute();
```

Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance

See a [full example](tests/kysely/index.test.mjs)

## Sequelize

Enable the extension
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"drizzle-orm": "^0.29.1",
"jest": "^29.5.0",
"knex": "^3.1.0",
"kysely": "^0.26.3",
"objection": "^3.1.3",
"pg": "^8.6.0",
"pg-promise": "^11.4.3",
Expand Down
53 changes: 53 additions & 0 deletions tests/kysely/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pg from 'pg';
import { Kysely, PostgresDialect, sql } from 'kysely';
import pgvector from 'pgvector/utils';

test('example', async () => {
const dialect = new PostgresDialect({
pool: new pg.Pool({
database: 'pgvector_node_test'
})
});

const db = new Kysely({
dialect
});

await sql`CREATE EXTENSION IF NOT EXISTS vector`.execute(db);

await db.schema.dropTable('kysely_items')
.ifExists()
.execute();

await db.schema.createTable('kysely_items')
.addColumn('id', 'serial', (cb) => cb.primaryKey())
.addColumn('embedding', 'vector(3)')
.execute();

const newItems = [
{embedding: pgvector.toSql([1, 1, 1])},
{embedding: pgvector.toSql([2, 2, 2])},
{embedding: pgvector.toSql([1, 1, 2])}
];
await db.insertInto('kysely_items')
.values(newItems)
.execute();

const items = await db.selectFrom('kysely_items')
.selectAll()
.orderBy(sql`embedding <-> '[1,1,1]'`)
.limit(5)
.execute();
expect(items.map(v => v.id)).toStrictEqual([1, 3, 2]);
expect(pgvector.fromSql(items[0].embedding)).toStrictEqual([1, 1, 1]);
expect(pgvector.fromSql(items[1].embedding)).toStrictEqual([1, 1, 2]);
expect(pgvector.fromSql(items[2].embedding)).toStrictEqual([2, 2, 2]);

await db.schema.createIndex('kysely_items_embedding_idx')
.on('kysely_items')
.using('hnsw')
.expression(sql`embedding vector_l2_ops`)
.execute();

db.destroy();
});

0 comments on commit 898551c

Please sign in to comment.