Skip to content

Commit

Permalink
feat: add node-sqlite3 connector (#147)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
farnabaz and pi0 authored Jan 24, 2025
1 parent 164c21c commit 54e81ed
Show file tree
Hide file tree
Showing 6 changed files with 732 additions and 19 deletions.
44 changes: 37 additions & 7 deletions docs/2.connectors/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ icon: simple-icons:sqlite

# SQLite

> Connect DB0 to SQLite using better-sqlite3
> Connect DB0 to local SQLite database with Node.js
<!-- :read-more{to=""} -->
You have two options for using SQLite with Node.js: [`better-sqlite3`](#better-sqlite3) and [`sqlite3`](#node-sqlite3).

## Usage
> [!NOTE]
> Unless needed for compatibility reasons, `better-sqlite3` is recommended.
## `better-sqlite3`

:read-more{to="https://github.com/WiseLibs/better-sqlite3" title="better-sqlite3"}

For this connector, you need to install [`better-sqlite3`](https://www.npmjs.com/package/better-sqlite3) dependency:

Expand All @@ -27,19 +32,44 @@ const db = createDatabase(
);
```

## Options
### Options

### `cwd`
#### `cwd`

Working directory to create database. Default is current working directory of project. (It will be ignored if `path` is provided an absolute path.)

### `name`
#### `name`

Database (file) name. Default is `db`.

> [!NOTE]
> You can use `:memory:` as name for in-memory storage.
### `path`
#### `path`

Related (to `cwd`) or absolute path to the sql file. By default it is stored in `{cwd}/.data/{name}.sqlite3` / `.data/db.sqlite3`

## `node-sqlite3`

:read-more{to="https://github.com/TryGhost/node-sqlite3" title="node-sqlite3"}

For this connector, you need to install [`sqlite3`](https://www.npmjs.com/package/sqlite3) dependency:

:pm-install{name="sqlite3"}

Use `node-sqlite3` connector:

```js
import { createDatabase } from "db0";
import sqlite from "db0/connectors/node-sqlite3";

const db = createDatabase(
sqlite({
name: ":memory:",
}),
);
```

### Options

(same as [better-sqlite3](#better-sqlite3))
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@types/better-sqlite3": "^7.6.12",
"@types/bun": "^1.2.0",
"@types/pg": "^8.11.11",
"@types/sqlite3": "^3.1.11",
"@vitest/coverage-v8": "^3.0.4",
"automd": "^0.3.12",
"better-sqlite3": "^11.8.1",
Expand All @@ -81,7 +82,8 @@
"@libsql/client": "*",
"better-sqlite3": "*",
"drizzle-orm": "*",
"mysql2": "*"
"mysql2": "*",
"sqlite3": "*"
},
"peerDependenciesMeta": {
"@libsql/client": {
Expand All @@ -98,6 +100,9 @@
},
"@electric-sql/pglite": {
"optional": true
},
"sqlite3": {
"optional": true
}
},
"packageManager": "[email protected]"
Expand Down
Loading

0 comments on commit 54e81ed

Please sign in to comment.