chore: extract SQLite abstraction into separate PR (#refactor/sqlite-abstraction)
This commit is contained in:
parent
fcf1bb010c
commit
2bfe81ee5c
4 changed files with 24 additions and 35 deletions
|
|
@ -26,13 +26,6 @@
|
||||||
"exports": {
|
"exports": {
|
||||||
"./*": "./src/*.ts"
|
"./*": "./src/*.ts"
|
||||||
},
|
},
|
||||||
"imports": {
|
|
||||||
"#db": {
|
|
||||||
"bun": "./src/storage/db.bun.ts",
|
|
||||||
"node": "./src/storage/db.node.ts",
|
|
||||||
"default": "./src/storage/db.bun.ts"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "7.28.4",
|
"@babel/core": "7.28.4",
|
||||||
"@effect/language-service": "0.79.0",
|
"@effect/language-service": "0.79.0",
|
||||||
|
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite"
|
import { Database as BunDatabase } from "bun:sqlite"
|
||||||
|
import { drizzle, type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite"
|
||||||
import { migrate } from "drizzle-orm/bun-sqlite/migrator"
|
import { migrate } from "drizzle-orm/bun-sqlite/migrator"
|
||||||
import { type SQLiteTransaction } from "drizzle-orm/sqlite-core"
|
import { type SQLiteTransaction } from "drizzle-orm/sqlite-core"
|
||||||
export * from "drizzle-orm"
|
export * from "drizzle-orm"
|
||||||
|
|
@ -10,10 +11,10 @@ import { NamedError } from "@opencode-ai/util/error"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { readFileSync, readdirSync, existsSync } from "fs"
|
import { readFileSync, readdirSync, existsSync } from "fs"
|
||||||
|
import * as schema from "./schema"
|
||||||
import { Installation } from "../installation"
|
import { Installation } from "../installation"
|
||||||
import { Flag } from "../flag/flag"
|
import { Flag } from "../flag/flag"
|
||||||
import { iife } from "@/util/iife"
|
import { iife } from "@/util/iife"
|
||||||
import { init } from "#db"
|
|
||||||
|
|
||||||
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined
|
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined
|
||||||
|
|
||||||
|
|
@ -35,12 +36,17 @@ export namespace Database {
|
||||||
return path.join(Global.Path.data, `opencode-${safe}.db`)
|
return path.join(Global.Path.data, `opencode-${safe}.db`)
|
||||||
})
|
})
|
||||||
|
|
||||||
export type Transaction = SQLiteTransaction<"sync", void>
|
type Schema = typeof schema
|
||||||
|
export type Transaction = SQLiteTransaction<"sync", void, Schema>
|
||||||
|
|
||||||
type Client = SQLiteBunDatabase
|
type Client = SQLiteBunDatabase
|
||||||
|
|
||||||
type Journal = { sql: string; timestamp: number; name: string }[]
|
type Journal = { sql: string; timestamp: number; name: string }[]
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
sqlite: undefined as BunDatabase | undefined,
|
||||||
|
}
|
||||||
|
|
||||||
function time(tag: string) {
|
function time(tag: string) {
|
||||||
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(tag)
|
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(tag)
|
||||||
if (!match) return 0
|
if (!match) return 0
|
||||||
|
|
@ -77,14 +83,17 @@ export namespace Database {
|
||||||
export const Client = lazy(() => {
|
export const Client = lazy(() => {
|
||||||
log.info("opening database", { path: Path })
|
log.info("opening database", { path: Path })
|
||||||
|
|
||||||
const db = init(Path)
|
const sqlite = new BunDatabase(Path, { create: true })
|
||||||
|
state.sqlite = sqlite
|
||||||
|
|
||||||
db.run("PRAGMA journal_mode = WAL")
|
sqlite.run("PRAGMA journal_mode = WAL")
|
||||||
db.run("PRAGMA synchronous = NORMAL")
|
sqlite.run("PRAGMA synchronous = NORMAL")
|
||||||
db.run("PRAGMA busy_timeout = 5000")
|
sqlite.run("PRAGMA busy_timeout = 5000")
|
||||||
db.run("PRAGMA cache_size = -64000")
|
sqlite.run("PRAGMA cache_size = -64000")
|
||||||
db.run("PRAGMA foreign_keys = ON")
|
sqlite.run("PRAGMA foreign_keys = ON")
|
||||||
db.run("PRAGMA wal_checkpoint(PASSIVE)")
|
sqlite.run("PRAGMA wal_checkpoint(PASSIVE)")
|
||||||
|
|
||||||
|
const db = drizzle({ client: sqlite })
|
||||||
|
|
||||||
// Apply schema migrations
|
// Apply schema migrations
|
||||||
const entries =
|
const entries =
|
||||||
|
|
@ -108,11 +117,14 @@ export namespace Database {
|
||||||
})
|
})
|
||||||
|
|
||||||
export function close() {
|
export function close() {
|
||||||
Client().$client.close()
|
const sqlite = state.sqlite
|
||||||
|
if (!sqlite) return
|
||||||
|
sqlite.close()
|
||||||
|
state.sqlite = undefined
|
||||||
Client.reset()
|
Client.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TxOrDb = Transaction | Client
|
export type TxOrDb = SQLiteTransaction<"sync", void, any, any> | Client
|
||||||
|
|
||||||
const ctx = Context.create<{
|
const ctx = Context.create<{
|
||||||
tx: TxOrDb
|
tx: TxOrDb
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue