Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536import Database from 'better-sqlite3';import { CONFIG } from '../config';
// Singleton connections. better-sqlite3 is synchronous/in-process, so one// long-lived read connection and one writer is the recommended pattern —// avoids FD churn and eliminates leak risk on thrown handlers.let readDb: Database.Database | null = null;let writeDb: Database.Database | null = null;
export function getReadDb(): Database.Database { if (!readDb) { readDb = new Database(CONFIG.TAP.DB_PATH, { readonly: true }); } return readDb;}
export function getWriteDb(): Database.Database { if (!writeDb) { writeDb = new Database(CONFIG.TAP.DB_PATH); } return writeDb;}
export function closeDbs(): void { try { readDb?.close(); } catch {} try { writeDb?.close(); } catch {} readDb = null; writeDb = null;}
// Back-compat shim — existing callers passing `{ readonly: false }` get the// writer; everything else gets the reader. Prefer getReadDb/getWriteDb in new code.export function getDb(options: { readonly: boolean } = { readonly: true }) { return options.readonly ? getReadDb() : getWriteDb();}