Download src/lib/db/adapters/bunSqliteAdapter.js from heyeyydss/api9nin: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/heyeyydss/api9nin/resolve/main/src/lib/db/adapters/bunSqliteAdapter.js
- Command line
-
hf download hf://spaces/heyeyydss/api9nin/src/lib/db/adapters/bunSqliteAdapter.js
-
curl -L -o bunSqliteAdapter.js https://huggingface.co/spaces/heyeyydss/api9nin/resolve/main/src/lib/db/adapters/bunSqliteAdapter.js
2.03 kB
| // Bun runtime adapter — uses built-in bun:sqlite (native, fastest under Bun). | |
| // Loaded only when process.versions.bun is present. | |
| import { PRAGMA_SQL } from "../schema.js"; | |
| const CHECKPOINT_INTERVAL_MS = 60 * 1000; | |
| export async function createBunSqliteAdapter(filePath) { | |
| // Dynamic import — only resolves under Bun runtime | |
| const { Database } = await import("bun:sqlite"); | |
| const db = new Database(filePath, { create: true }); | |
| db.exec(PRAGMA_SQL); | |
| const stmtCache = new Map(); | |
| function prepare(sql) { | |
| let stmt = stmtCache.get(sql); | |
| if (!stmt) { | |
| stmt = db.prepare(sql); | |
| stmtCache.set(sql, stmt); | |
| } | |
| return stmt; | |
| } | |
| const checkpointTimer = setInterval(() => { | |
| try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {} | |
| }, CHECKPOINT_INTERVAL_MS); | |
| if (typeof checkpointTimer.unref === "function") checkpointTimer.unref(); | |
| function gracefulClose() { | |
| try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {} | |
| try { stmtCache.clear(); } catch {} | |
| try { db.close(); } catch {} | |
| } | |
| const onShutdown = () => gracefulClose(); | |
| process.once("beforeExit", onShutdown); | |
| process.once("SIGINT", () => { onShutdown(); process.exit(0); }); | |
| process.once("SIGTERM", () => { onShutdown(); process.exit(0); }); | |
| return { | |
| driver: "bun:sqlite", | |
| run(sql, params = []) { | |
| const r = prepare(sql).run(...params); | |
| return { changes: Number(r.changes ?? 0), lastInsertRowid: Number(r.lastInsertRowid ?? 0) }; | |
| }, | |
| get(sql, params = []) { | |
| return prepare(sql).get(...params); | |
| }, | |
| all(sql, params = []) { | |
| return prepare(sql).all(...params); | |
| }, | |
| exec(sql) { return db.exec(sql); }, | |
| transaction(fn) { | |
| // bun:sqlite has db.transaction() API (similar to better-sqlite3) | |
| const tx = db.transaction(fn); | |
| return tx(); | |
| }, | |
| checkpoint() { try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {} }, | |
| close() { | |
| clearInterval(checkpointTimer); | |
| gracefulClose(); | |
| }, | |
| raw: db, | |
| }; | |
| } | |