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"
32import { migrate } from "drizzle-orm/bun-sqlite/migrator"
43import { type SQLiteTransaction } from "drizzle-orm/sqlite-core"
54export * from "drizzle-orm"
@@ -11,10 +10,10 @@ import { NamedError } from "@opencode-ai/util/error"
1110import z from "zod"
1211import path from "path"
1312import { readFileSync , readdirSync , existsSync } from "fs"
14- import * as schema from "./schema"
1513import { Installation } from "../installation"
1614import { Flag } from "../flag/flag"
1715import { iife } from "@/util/iife"
16+ import { init } from "#db"
1817
1918declare 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