Skip to content

refactor(core): abstract SQLite driver behind #db/driver subpath#72

Merged
BYK merged 1 commit intomainfrom
refactor/sqlite-node-driver
Apr 17, 2026
Merged

refactor(core): abstract SQLite driver behind #db/driver subpath#72
BYK merged 1 commit intomainfrom
refactor/sqlite-node-driver

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented Apr 17, 2026

Summary

Replaces the direct import { Database } from "bun:sqlite" with a Node subpath import #db/driver that resolves to:

  • driver.bun.ts under Bun — re-exports bun:sqlite's Database
  • driver.node.ts under Node — node:sqlite's DatabaseSync with a .query() shim that caches prepared statements per SQL string (matching bun:sqlite's behavior)

Why

Unblocks the future @loreai/pi Node-based extension while keeping the OpenCode plugin (still Bun) identical at runtime. Inspired by OpenCode's own #db pattern for the same reason.

Key properties

  • All 99 .query() call sites keep working unchanged — the Node shim adds the .query() alias with caching so nothing in the core modules needed to change.
  • Only 2 Bun-specific APIs in the whole source tree, both swapped:
    • Bun.CryptoHasher in lat-reader.tssha256() helper from #db/driver (via node:crypto)
    • Bun.file() in config.tsnode:fs existsSync + readFileSync
  • Number(result.changes) coercion in two places — node:sqlite types changes as number | bigint (SQLite itself never exceeds 2^53).

Verification

  • bun --filter '*' typecheck passes for both packages
  • bun test → 358 pass (354 existing + 4 new driver smoke tests)
  • No behavior change for existing OpenCode users

Follow-up

  • Add esbuild build scripts for each package (node + bun bundle targets)
  • Pi extension package consuming the node build

Replaces the direct `import { Database } from "bun:sqlite"` with a subpath
import `#db/driver` that resolves to:
- `driver.bun.ts` under Bun  (re-exports bun:sqlite's Database)
- `driver.node.ts` under Node (node:sqlite with a .query() cache shim)

This unblocks the future `@loreai/pi` extension (runs on Node) while keeping
the OpenCode plugin (still runs on Bun) identical at runtime.

Changes:

- packages/core/src/db/driver.{bun,node}.ts
  Two thin drivers. Node adds a .query() alias on DatabaseSync that caches
  prepared statements per SQL string — matches bun:sqlite's behavior so all 99
  existing .query() call sites keep working unchanged.

- packages/core/src/db.ts
  - `bun:sqlite` → `#db/driver` import
  - Drop `{ create: true }` option (not supported by DatabaseSync;
    creation-on-missing is the default for both drivers anyway)

- packages/core/src/config.ts
  Drop `Bun.file` / `Bun.file().exists` / `Bun.file().json` → use
  `node:fs` `existsSync` + `readFileSync` + `JSON.parse`. One of two
  Bun-* API calls in the whole source tree.

- packages/core/src/lat-reader.ts
  Drop `Bun.CryptoHasher` → use a small `sha256()` helper re-exported
  from `#db/driver` (backed by `node:crypto`). Second and last Bun-* API.

- packages/core/src/distillation.ts, src/ltm.ts
  `Number(result.changes)` coercion where the return type is compared to a
  `number`. node:sqlite types `changes` as `number | bigint` — SQLite
  itself never returns > 2^53, so the coercion is safe.

- packages/opencode/scripts/list-sessions.ts
  Drop bun:sqlite-specific `db.query<Row, []>(sql)` type arguments (not
  supported by the node shim); cast the query result instead.

- packages/core/package.json
  Adds the `imports: { "#db/driver": ... }` map.

- packages/core/test/db-driver.test.ts
  Smoke tests for the driver API (query cache, FTS5+bm25, DELETE RETURNING,
  sha256). 4 new tests.

Tests: 354 → 358 pass. No behavior change for existing OpenCode users.
@BYK BYK enabled auto-merge (squash) April 17, 2026 10:12
@BYK BYK merged commit 76fb307 into main Apr 17, 2026
1 check passed
@BYK BYK deleted the refactor/sqlite-node-driver branch April 17, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant