diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/opencode/package.json | 15 | ||||
| -rw-r--r-- | packages/opencode/src/storage/db.bun.ts | 8 | ||||
| -rw-r--r-- | packages/opencode/src/storage/db.node.ts | 8 | ||||
| -rw-r--r-- | packages/opencode/src/storage/db.ts | 36 |
4 files changed, 39 insertions, 28 deletions
diff --git a/packages/opencode/package.json b/packages/opencode/package.json index 049573e3e..d8392901e 100644 --- a/packages/opencode/package.json +++ b/packages/opencode/package.json @@ -26,6 +26,13 @@ "exports": { "./*": "./src/*.ts" }, + "imports": { + "#db": { + "bun": "./src/storage/db.bun.ts", + "node": "./src/storage/db.node.ts", + "default": "./src/storage/db.bun.ts" + } + }, "devDependencies": { "@babel/core": "7.28.4", "@effect/language-service": "0.79.0", @@ -50,8 +57,8 @@ "@types/which": "3.0.4", "@types/yargs": "17.0.33", "@typescript/native-preview": "catalog:", - "drizzle-kit": "1.0.0-beta.16-ea816b6", - "drizzle-orm": "1.0.0-beta.16-ea816b6", + "drizzle-kit": "catalog:", + "drizzle-orm": "catalog:", "typescript": "catalog:", "vscode-languageserver-types": "3.17.5", "why-is-node-running": "3.2.2", @@ -113,7 +120,7 @@ "cross-spawn": "^7.0.6", "decimal.js": "10.5.0", "diff": "catalog:", - "drizzle-orm": "1.0.0-beta.16-ea816b6", + "drizzle-orm": "catalog:", "effect": "catalog:", "fuzzysort": "3.1.0", "glob": "13.0.5", @@ -144,6 +151,6 @@ "zod-to-json-schema": "3.24.5" }, "overrides": { - "drizzle-orm": "1.0.0-beta.16-ea816b6" + "drizzle-orm": "catalog:" } } diff --git a/packages/opencode/src/storage/db.bun.ts b/packages/opencode/src/storage/db.bun.ts new file mode 100644 index 000000000..fa6190925 --- /dev/null +++ b/packages/opencode/src/storage/db.bun.ts @@ -0,0 +1,8 @@ +import { Database } from "bun:sqlite" +import { drizzle } from "drizzle-orm/bun-sqlite" + +export function init(path: string) { + const sqlite = new Database(path, { create: true }) + const db = drizzle({ client: sqlite }) + return db +} diff --git a/packages/opencode/src/storage/db.node.ts b/packages/opencode/src/storage/db.node.ts new file mode 100644 index 000000000..0dba8dcef --- /dev/null +++ b/packages/opencode/src/storage/db.node.ts @@ -0,0 +1,8 @@ +import { DatabaseSync } from "node:sqlite" +import { drizzle } from "drizzle-orm/node-sqlite" + +export function init(path: string) { + const sqlite = new DatabaseSync(path) + const db = drizzle({ client: sqlite }) + return db +} diff --git a/packages/opencode/src/storage/db.ts b/packages/opencode/src/storage/db.ts index beb8e3eb5..dcf0942e1 100644 --- a/packages/opencode/src/storage/db.ts +++ b/packages/opencode/src/storage/db.ts @@ -1,5 +1,4 @@ -import { Database as BunDatabase } from "bun:sqlite" -import { drizzle, type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite" +import { type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite" import { migrate } from "drizzle-orm/bun-sqlite/migrator" import { type SQLiteTransaction } from "drizzle-orm/sqlite-core" export * from "drizzle-orm" @@ -11,10 +10,10 @@ import { NamedError } from "@opencode-ai/util/error" import z from "zod" import path from "path" import { readFileSync, readdirSync, existsSync } from "fs" -import * as schema from "./schema" import { Installation } from "../installation" import { Flag } from "../flag/flag" import { iife } from "@/util/iife" +import { init } from "#db" declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined @@ -36,17 +35,12 @@ export namespace Database { return path.join(Global.Path.data, `opencode-${safe}.db`) }) - type Schema = typeof schema - export type Transaction = SQLiteTransaction<"sync", void, Schema> + export type Transaction = SQLiteTransaction<"sync", void> type Client = SQLiteBunDatabase type Journal = { sql: string; timestamp: number; name: string }[] - const state = { - sqlite: undefined as BunDatabase | undefined, - } - function time(tag: string) { const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(tag) if (!match) return 0 @@ -83,17 +77,14 @@ export namespace Database { export const Client = lazy(() => { log.info("opening database", { path: Path }) - const sqlite = new BunDatabase(Path, { create: true }) - state.sqlite = sqlite - - sqlite.run("PRAGMA journal_mode = WAL") - sqlite.run("PRAGMA synchronous = NORMAL") - sqlite.run("PRAGMA busy_timeout = 5000") - sqlite.run("PRAGMA cache_size = -64000") - sqlite.run("PRAGMA foreign_keys = ON") - sqlite.run("PRAGMA wal_checkpoint(PASSIVE)") + const db = init(Path) - const db = drizzle({ client: sqlite }) + db.run("PRAGMA journal_mode = WAL") + db.run("PRAGMA synchronous = NORMAL") + db.run("PRAGMA busy_timeout = 5000") + db.run("PRAGMA cache_size = -64000") + db.run("PRAGMA foreign_keys = ON") + db.run("PRAGMA wal_checkpoint(PASSIVE)") // Apply schema migrations const entries = @@ -117,14 +108,11 @@ export namespace Database { }) export function close() { - const sqlite = state.sqlite - if (!sqlite) return - sqlite.close() - state.sqlite = undefined + Client().$client.close() Client.reset() } - export type TxOrDb = SQLiteTransaction<"sync", void, any, any> | Client + export type TxOrDb = Transaction | Client const ctx = Context.create<{ tx: TxOrDb |
