|
@@ -33,6 +33,10 @@ export namespace Database {
|
|
|
|
|
|
|
|
type Journal = { sql: string; timestamp: number }[]
|
|
type Journal = { sql: string; timestamp: number }[]
|
|
|
|
|
|
|
|
|
|
+ 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
|
|
@@ -69,6 +73,7 @@ export namespace Database {
|
|
|
log.info("opening database", { path: path.join(Global.Path.data, "opencode.db") })
|
|
log.info("opening database", { path: path.join(Global.Path.data, "opencode.db") })
|
|
|
|
|
|
|
|
const sqlite = new BunDatabase(path.join(Global.Path.data, "opencode.db"), { create: true })
|
|
const sqlite = new BunDatabase(path.join(Global.Path.data, "opencode.db"), { create: true })
|
|
|
|
|
+ state.sqlite = sqlite
|
|
|
|
|
|
|
|
sqlite.run("PRAGMA journal_mode = WAL")
|
|
sqlite.run("PRAGMA journal_mode = WAL")
|
|
|
sqlite.run("PRAGMA synchronous = NORMAL")
|
|
sqlite.run("PRAGMA synchronous = NORMAL")
|
|
@@ -95,6 +100,14 @@ export namespace Database {
|
|
|
return db
|
|
return db
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ export function close() {
|
|
|
|
|
+ const sqlite = state.sqlite
|
|
|
|
|
+ if (!sqlite) return
|
|
|
|
|
+ sqlite.close()
|
|
|
|
|
+ state.sqlite = undefined
|
|
|
|
|
+ Client.reset()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
export type TxOrDb = Transaction | Client
|
|
export type TxOrDb = Transaction | Client
|
|
|
|
|
|
|
|
const ctx = Context.create<{
|
|
const ctx = Context.create<{
|