Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asg017 committed Jan 24, 2025
1 parent dbc5098 commit a2dd24f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
53 changes: 42 additions & 11 deletions site/using/js.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The `load()` function is compatible with

if your vectors are represented as an array of numbers, wrap it in a
[`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array),
and use
and use the
[`.buffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/buffer)
accessor to bind as a parameter to `sqlite-vec` SQL functions.

Expand All @@ -62,8 +62,32 @@ console.log(stmt.run(embedding.buffer)); // 4

## Node.js

Here's a quick recipe of using `sqlite-vec` with
[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) in Node.js.
If you're on Node.js `23.5.0` or above, you can use [the builtin `node:sqlite` module](https://nodejs.org/api/sqlite.html) with `sqlite-vec` like so:

```js
import { DatabaseSync } from "node:sqlite";
import * as sqliteVec from "sqlite-vec";

const db = new DatabaseSync(":memory:", { allowExtension: true });
sqliteVec.load(db);

const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]);
const { result } = db
.prepare("select vec_length(?) as result")
.get(new Uint8Array(embedding.buffer));

console.log(result); // 4
```


See
[`simple-node2/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node2/demo.mjs)
for a complete `node:sqlite` + `sqlite-vec` demo.


Alternatively, you can use the
[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)
NPM package with `sqlite-vec` in Node as well.

```js
import * as sqliteVec from "sqlite-vec";
Expand All @@ -83,21 +107,19 @@ console.log(result); // 4

See
[`simple-node/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node/demo.mjs)
for a more complete Node.js demo.
for a more complete demo.

## Deno

Here's a quick recipe of using `sqlite-vec` with
[`jsr:@db/sqlite`](https://jsr.io/@db/sqlite) in Deno. It will only work on Deno
version `1.44` or greater, because of a bug in previous Deno versions.

Keep in mind, the `better-sqlite3` example above also works in Deno, you just
need to prefix the `better-sqlite3` import with `npm:`, like
`import * from "npm:better-sqlite3"`.


```ts
import { Database } from "jsr:@db/sqlite@0.11";
import * as sqliteVec from "npm:sqlite-vec@0.0.1-alpha.9";
import { Database } from "jsr:@db/sqlite";
import * as sqliteVec from "npm:sqlite-vec";

const db = new Database(":memory:");
db.enableLoadExtension = true;
Expand All @@ -115,11 +137,17 @@ See
[`simple-deno/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-deno/demo.ts)
for a more complete Deno demo.

The `better-sqlite3` example above also works in Deno, when the `better-sqlite3` import is prefixed with `npm:`:

```js
import * from "better-sqlite3"; // [!code --]
import * from "npm:better-sqlite3"; // [!code ++]
```

## Bun

Here's a quick recipe of using `sqlite-vec` with
[`bun:sqlite`](https://bun.sh/docs/api/sqlite) in Bun. The `better-sqlite3`
example above also works with Bun.
[`bun:sqlite`](https://bun.sh/docs/api/sqlite) in Bun.

```ts
import { Database } from "bun:sqlite";
Expand All @@ -143,3 +171,6 @@ console.log(result); // 4
See
[`simple-bun/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-bun/demo.ts)
for a more complete Bun demo.

The `better-sqlite3`
example above also works with Bun.
2 changes: 1 addition & 1 deletion site/using/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ for a more complete Python demo.

If your vectors in Python are provided as a list of floats, you can
convert them into the compact BLOB format that `sqlite-vec` uses with
`serialize_float32()`. This will internally call [`struct.pack()`](https://docs.python.org/3/library/struct.html#struct.pack).
`serialize_float32()`. This internally calls [`struct.pack()`](https://docs.python.org/3/library/struct.html#struct.pack).

```python
from sqlite_vec import serialize_float32
Expand Down

0 comments on commit a2dd24f

Please sign in to comment.