Skip to content

Commit

Permalink
Updated docs and tests for Drizzle ORM 0.31.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 2, 2024
1 parent 30334c3 commit 61d5fb7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ And follow the instructions for your database library:
- [Slonik](#slonik)
- [TypeORM](#typeorm)
- [MikroORM](#mikroorm)
- [Drizzle ORM](#drizzle-orm) (experimental)
- [Drizzle ORM](#drizzle-orm)

Or check out some examples:

Expand Down Expand Up @@ -617,7 +617,7 @@ See a [full example](tests/mikro-orm/index.test.mjs)

## Drizzle ORM

Note: This is currently experimental and does not work with Drizzle Kit
Drizzle ORM 0.31.0+ has built-in support for pgvector :tada:

Enable the extension

Expand All @@ -628,14 +628,16 @@ await client`CREATE EXTENSION IF NOT EXISTS vector`;
Add a vector field

```javascript
import { vector } from 'pgvector/drizzle-orm';
import { vector } from 'drizzle-orm/pg-core';

const items = pgTable('items', {
id: serial('id').primaryKey(),
embedding: vector('embedding', {dimensions: 3})
});
```

Also supports `halfvec`, `bit`, and `sparsevec`

Insert vectors

```javascript
Expand All @@ -649,15 +651,15 @@ await db.insert(items).values(newItems);
Get the nearest neighbors to a vector

```javascript
import { l2Distance } from 'pgvector/drizzle-orm';
import { l2Distance } from 'drizzle-orm';

const allItems = await db.select()
.from(items)
.orderBy(l2Distance(items.embedding, [1, 2, 3]))
.limit(5);
```

Also supports `maxInnerProduct` and `cosineDistance`
Also supports `innerProduct`, `cosineDistance`, `l1Distance`, `hammingDistance`, and `jaccardDistance`

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

Expand Down
8 changes: 4 additions & 4 deletions tests/drizzle-orm/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sql } from 'drizzle-orm';
import { sql, l2Distance, innerProduct, cosineDistance, l1Distance, hammingDistance, jaccardDistance } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/postgres-js';
import { pgTable, serial } from 'drizzle-orm/pg-core';
import { pgTable, serial, vector, halfvec, bit, sparsevec } from 'drizzle-orm/pg-core';
import { SparseVector } from 'pgvector';
import postgres from 'postgres';
import { cosineDistance, l2Distance, maxInnerProduct, l1Distance, hammingDistance, jaccardDistance, vector, halfvec, bit, sparsevec, SparseVector } from 'pgvector/drizzle-orm';

test('example', async () => {
const client = postgres({database: 'pgvector_node_test', onnotice: function() {}});
Expand Down Expand Up @@ -55,7 +55,7 @@ test('example', async () => {
// max inner product
allItems = await db.select()
.from(items)
.orderBy(maxInnerProduct(items.embedding, [1, 1, 1]))
.orderBy(innerProduct(items.embedding, [1, 1, 1]))
.limit(5);
expect(allItems.map(v => v.id)).toStrictEqual([2, 3, 1, 4]);

Expand Down

0 comments on commit 61d5fb7

Please sign in to comment.