Skip to content

Commit 37b8662

Browse files
authored
refactor: abstract SQLite behind runtime-conditional #db import (#18316)
1 parent ddcb32a commit 37b8662

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

bun.lock

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"@tailwindcss/vite": "4.1.11",
4444
"diff": "8.0.2",
4545
"dompurify": "3.3.1",
46-
"drizzle-kit": "1.0.0-beta.16-ea816b6",
47-
"drizzle-orm": "1.0.0-beta.16-ea816b6",
46+
"drizzle-kit": "1.0.0-beta.19-d95b7a4",
47+
"drizzle-orm": "1.0.0-beta.19-d95b7a4",
4848
"effect": "4.0.0-beta.35",
4949
"ai": "5.0.124",
5050
"hono": "4.10.7",

packages/opencode/package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"exports": {
2727
"./*": "./src/*.ts"
2828
},
29+
"imports": {
30+
"#db": {
31+
"bun": "./src/storage/db.bun.ts",
32+
"node": "./src/storage/db.node.ts",
33+
"default": "./src/storage/db.bun.ts"
34+
}
35+
},
2936
"devDependencies": {
3037
"@babel/core": "7.28.4",
3138
"@effect/language-service": "0.79.0",
@@ -50,8 +57,8 @@
5057
"@types/which": "3.0.4",
5158
"@types/yargs": "17.0.33",
5259
"@typescript/native-preview": "catalog:",
53-
"drizzle-kit": "1.0.0-beta.16-ea816b6",
54-
"drizzle-orm": "1.0.0-beta.16-ea816b6",
60+
"drizzle-kit": "catalog:",
61+
"drizzle-orm": "catalog:",
5562
"typescript": "catalog:",
5663
"vscode-languageserver-types": "3.17.5",
5764
"why-is-node-running": "3.2.2",
@@ -113,7 +120,7 @@
113120
"cross-spawn": "^7.0.6",
114121
"decimal.js": "10.5.0",
115122
"diff": "catalog:",
116-
"drizzle-orm": "1.0.0-beta.16-ea816b6",
123+
"drizzle-orm": "catalog:",
117124
"effect": "catalog:",
118125
"fuzzysort": "3.1.0",
119126
"glob": "13.0.5",
@@ -144,6 +151,6 @@
144151
"zod-to-json-schema": "3.24.5"
145152
},
146153
"overrides": {
147-
"drizzle-orm": "1.0.0-beta.16-ea816b6"
154+
"drizzle-orm": "catalog:"
148155
}
149156
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Database } from "bun:sqlite"
2+
import { drizzle } from "drizzle-orm/bun-sqlite"
3+
4+
export function init(path: string) {
5+
const sqlite = new Database(path, { create: true })
6+
const db = drizzle({ client: sqlite })
7+
return db
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { DatabaseSync } from "node:sqlite"
2+
import { drizzle } from "drizzle-orm/node-sqlite"
3+
4+
export function init(path: string) {
5+
const sqlite = new DatabaseSync(path)
6+
const db = drizzle({ client: sqlite })
7+
return db
8+
}

packages/opencode/src/storage/db.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Database as BunDatabase } from "bun:sqlite"
2-
import { drizzle, type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite"
1+
import { type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite"
32
import { migrate } from "drizzle-orm/bun-sqlite/migrator"
43
import { type SQLiteTransaction } from "drizzle-orm/sqlite-core"
54
export * from "drizzle-orm"
@@ -11,10 +10,10 @@ import { NamedError } from "@opencode-ai/util/error"
1110
import z from "zod"
1211
import path from "path"
1312
import { readFileSync, readdirSync, existsSync } from "fs"
14-
import * as schema from "./schema"
1513
import { Installation } from "../installation"
1614
import { Flag } from "../flag/flag"
1715
import { iife } from "@/util/iife"
16+
import { init } from "#db"
1817

1918
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined
2019

@@ -36,17 +35,12 @@ export namespace Database {
3635
return path.join(Global.Path.data, `opencode-${safe}.db`)
3736
})
3837

39-
type Schema = typeof schema
40-
export type Transaction = SQLiteTransaction<"sync", void, Schema>
38+
export type Transaction = SQLiteTransaction<"sync", void>
4139

4240
type Client = SQLiteBunDatabase
4341

4442
type Journal = { sql: string; timestamp: number; name: string }[]
4543

46-
const state = {
47-
sqlite: undefined as BunDatabase | undefined,
48-
}
49-
5044
function time(tag: string) {
5145
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(tag)
5246
if (!match) return 0
@@ -83,17 +77,14 @@ export namespace Database {
8377
export const Client = lazy(() => {
8478
log.info("opening database", { path: Path })
8579

86-
const sqlite = new BunDatabase(Path, { create: true })
87-
state.sqlite = sqlite
88-
89-
sqlite.run("PRAGMA journal_mode = WAL")
90-
sqlite.run("PRAGMA synchronous = NORMAL")
91-
sqlite.run("PRAGMA busy_timeout = 5000")
92-
sqlite.run("PRAGMA cache_size = -64000")
93-
sqlite.run("PRAGMA foreign_keys = ON")
94-
sqlite.run("PRAGMA wal_checkpoint(PASSIVE)")
80+
const db = init(Path)
9581

96-
const db = drizzle({ client: sqlite })
82+
db.run("PRAGMA journal_mode = WAL")
83+
db.run("PRAGMA synchronous = NORMAL")
84+
db.run("PRAGMA busy_timeout = 5000")
85+
db.run("PRAGMA cache_size = -64000")
86+
db.run("PRAGMA foreign_keys = ON")
87+
db.run("PRAGMA wal_checkpoint(PASSIVE)")
9788

9889
// Apply schema migrations
9990
const entries =
@@ -117,14 +108,11 @@ export namespace Database {
117108
})
118109

119110
export function close() {
120-
const sqlite = state.sqlite
121-
if (!sqlite) return
122-
sqlite.close()
123-
state.sqlite = undefined
111+
Client().$client.close()
124112
Client.reset()
125113
}
126114

127-
export type TxOrDb = SQLiteTransaction<"sync", void, any, any> | Client
115+
export type TxOrDb = Transaction | Client
128116

129117
const ctx = Context.create<{
130118
tx: TxOrDb

0 commit comments

Comments
 (0)