import { Database } from "bun:sqlite"; import * as sqliteVec from "sqlite-vec"; import grabDBDir from "../utils/grab-db-dir"; /** Reuse one connection per database file. */ const clientCache = new Map(); export default function grabSQLiteClient({ config }) { const { db_file_path } = grabDBDir({ config }); const cached = clientCache.get(db_file_path); if (cached) { return cached; } const SQLiteClient = new Database(db_file_path, { create: true, }); sqliteVec.load(SQLiteClient); if (config.wal_mode) { SQLiteClient.run("PRAGMA journal_mode = WAL;"); } clientCache.set(db_file_path, SQLiteClient); return SQLiteClient; }