db: split runtime facade by domain

This commit is contained in:
ejclaw
2026-04-11 08:09:29 +09:00
parent 10fa7fb747
commit af04fe04cf
5 changed files with 1117 additions and 956 deletions

View File

@@ -0,0 +1,34 @@
import { Database } from 'bun:sqlite';
import {
openInitializedDatabaseFromFile,
openInitializedInMemoryDatabase,
openInitializedPersistentDatabase,
} from './database-lifecycle.js';
let db: Database | undefined;
export function initDatabase(): void {
db = openInitializedPersistentDatabase();
}
/** @internal - for tests only. Creates a fresh in-memory database. */
export function _initTestDatabase(): void {
db = openInitializedInMemoryDatabase();
}
/** @internal - for tests only. Opens an existing database file and runs schema/migrations. */
export function _initTestDatabaseFromFile(dbPath: string): void {
db = openInitializedDatabaseFromFile(dbPath);
}
export function requireDatabase(): Database {
if (!db) {
throw new Error('Database not initialized');
}
return db;
}
export function getDatabaseIfInitialized(): Database | undefined {
return db;
}