import { Database as BunSqliteDatabase } from 'bun:sqlite'; import type { Database } from './types.js'; export function createSqliteDb(url: string): Database { // Strip the "file:" prefix if present const path = url.startsWith('file:') ? url.slice(5) : url; const db = new BunSqliteDatabase(path); // Enable WAL mode for better concurrent read performance try { db.exec('PRAGMA journal_mode = WAL'); } catch { // This can fail when multiple processes initialize the same DB concurrently. } return { run(sql, params = []) { db.query(sql).run(...params); }, get>(sql: string, params: unknown[] = []) { const row = db.query(sql).get(...params); return (row === null ? undefined : row) as T | undefined; }, all>(sql: string, params: unknown[] = []) { return db.query(sql).all(...params) as T[]; }, close() { db.close(); } }; }