Files
bun-sqlite/dist/functions/grab-sqlite-client.js
T
tbenandClaude Opus 5.5 938a1da71d Add queue tables, quote SQL identifiers (v1.3.0)
- isQueue table option adds queue columns and a (status, available_at)
  index; BunSQLite.queue exposes add, claim, complete, fail and
  release_stale with atomic claims
- Set busy_timeout on connections so workers in separate processes wait
  instead of failing with SQLITE_BUSY
- Quote table and column names with backticks in generated SQL, so
  reserved words (to, order, group) work and unknown columns error
  instead of matching as string literals
- Tests for queue ops, multi-process claims and reserved-word names

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 16:29:14 +01:00

25 lines
872 B
JavaScript

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);
// Wait for other connections' write locks instead of failing with
// SQLITE_BUSY (queue workers in separate processes)
SQLiteClient.run("PRAGMA busy_timeout = 5000;");
if (config.wal_mode) {
SQLiteClient.run("PRAGMA journal_mode = WAL;");
}
clientCache.set(db_file_path, SQLiteClient);
return SQLiteClient;
}