refactor(core): abstract SQLite driver behind #db/driver subpath#72
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the direct
import { Database } from "bun:sqlite"with a Node subpath import#db/driverthat resolves to:driver.bun.tsunder Bun — re-exportsbun:sqlite'sDatabasedriver.node.tsunder Node —node:sqlite'sDatabaseSyncwith a.query()shim that caches prepared statements per SQL string (matchingbun:sqlite's behavior)Why
Unblocks the future
@loreai/piNode-based extension while keeping the OpenCode plugin (still Bun) identical at runtime. Inspired by OpenCode's own#dbpattern for the same reason.Key properties
.query()call sites keep working unchanged — the Node shim adds the.query()alias with caching so nothing in the core modules needed to change.Bun.CryptoHasherinlat-reader.ts→sha256()helper from#db/driver(vianode:crypto)Bun.file()inconfig.ts→node:fsexistsSync+readFileSyncNumber(result.changes)coercion in two places —node:sqlitetypeschangesasnumber | bigint(SQLite itself never exceeds 2^53).Verification
bun --filter '*' typecheckpasses for both packagesbun test→ 358 pass (354 existing + 4 new driver smoke tests)Follow-up
node+bunbundle targets)