Skip to content

Commit a93cb5d

Browse files
committed
docs: add Knex.js integration guide with installation and setup instructions
1 parent db773d5 commit a93cb5d

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

doc/knex-integration.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Using with Knex.js
2+
3+
[@photostructure/knex-sqlite](https://github.com/photostructure/knex-sqlite) provides a Knex.js dialect that uses @photostructure/sqlite as the SQLite driver instead of better-sqlite3.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @photostructure/knex-sqlite @photostructure/sqlite knex
9+
```
10+
11+
## Setup
12+
13+
```javascript
14+
const knex = require("knex");
15+
const Client = require("@photostructure/knex-sqlite");
16+
17+
const db = knex({
18+
client: Client,
19+
connection: {
20+
filename: "./mydb.sqlite",
21+
},
22+
useNullAsDefault: true,
23+
});
24+
```
25+
26+
## Connection options
27+
28+
```javascript
29+
const db = knex({
30+
client: Client,
31+
connection: {
32+
filename: "./mydb.sqlite",
33+
options: {
34+
readonly: false, // open as read-only
35+
safeIntegers: false, // return BigInt for large integers
36+
},
37+
},
38+
useNullAsDefault: true,
39+
});
40+
```
41+
42+
## Features
43+
44+
All standard Knex.js features work: schema building, queries, transactions, joins, aggregations, raw SQL, and `RETURNING` clauses on INSERT/UPDATE.
45+
46+
```javascript
47+
// Schema
48+
await db.schema.createTable("users", (table) => {
49+
table.increments("id");
50+
table.string("name");
51+
table.integer("age");
52+
});
53+
54+
// CRUD
55+
await db("users").insert({ name: "Alice", age: 30 });
56+
const users = await db("users").where("age", ">", 25).select("*");
57+
58+
// Transactions
59+
await db.transaction(async (trx) => {
60+
await trx("users").insert({ name: "Bob", age: 25 });
61+
await trx("posts").insert({ user_id: 1, title: "Hello" });
62+
});
63+
```
64+
65+
## How it works
66+
67+
The dialect extends Knex's built-in `Client_BetterSQLite3` and adapts three things:
68+
69+
1. **Driver**: loads @photostructure/sqlite and calls `enhance()` to add better-sqlite3-style methods
70+
2. **Statement `.reader` property**: uses `stmt.columns().length > 0` to detect whether a statement returns rows (correctly handles `RETURNING` clauses)
71+
3. **Binding format**: spreads array bindings as variadic arguments, and maps `safeIntegers()` to `setReadBigInts()`
72+
73+
See the [@photostructure/knex-sqlite README](https://github.com/photostructure/knex-sqlite) for full details.

0 commit comments

Comments
 (0)