|
| 1 | +/** |
| 2 | + * SQLite adapter that provides a unified API surface for database access. |
| 3 | + * This module is the single import point for all SQLite access in the |
| 4 | + * codebase, replacing direct `bun:sqlite` imports. |
| 5 | + * |
| 6 | + * Runtime detection: |
| 7 | + * - **Bun**: Re-exports `bun:sqlite`'s Database directly (zero overhead). |
| 8 | + * - **Node.js**: Wraps `node:sqlite`'s DatabaseSync with a bun:sqlite- |
| 9 | + * compatible API (`.query()` → `.prepare()`, manual transaction wrapper). |
| 10 | + * |
| 11 | + * Call sites continue to use `.query(sql).get()` / `.all()` / `.run()` |
| 12 | + * and `db.transaction()` exactly as before — no migration churn needed. |
| 13 | + */ |
| 14 | + |
| 15 | +/** Valid SQLite binding value */ |
| 16 | +export type SQLQueryBindings = |
| 17 | + | string |
| 18 | + | number |
| 19 | + | bigint |
| 20 | + | null |
| 21 | + | Uint8Array |
| 22 | + | undefined; |
| 23 | + |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +// Runtime detection |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +const isBun = typeof globalThis.Bun !== "undefined"; |
| 29 | + |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +// Node.js implementation (wraps node:sqlite DatabaseSync) |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | + |
| 34 | +/** |
| 35 | + * Minimal statement wrapper matching the bun:sqlite query API. |
| 36 | + * Wraps node:sqlite's StatementSync to expose `.get()`, `.all()`, `.run()`. |
| 37 | + */ |
| 38 | +class NodeStatementWrapper { |
| 39 | + // biome-ignore lint/suspicious/noExplicitAny: node:sqlite types loaded lazily |
| 40 | + private readonly stmt: any; |
| 41 | + |
| 42 | + // biome-ignore lint/suspicious/noExplicitAny: node:sqlite types loaded lazily |
| 43 | + constructor(stmt: any) { |
| 44 | + this.stmt = stmt; |
| 45 | + } |
| 46 | + |
| 47 | + get( |
| 48 | + ...params: SQLQueryBindings[] |
| 49 | + ): Record<string, SQLQueryBindings> | undefined { |
| 50 | + return this.stmt.get(...params) as |
| 51 | + | Record<string, SQLQueryBindings> |
| 52 | + | undefined; |
| 53 | + } |
| 54 | + |
| 55 | + all(...params: SQLQueryBindings[]): Record<string, SQLQueryBindings>[] { |
| 56 | + return this.stmt.all(...params) as Record<string, SQLQueryBindings>[]; |
| 57 | + } |
| 58 | + |
| 59 | + run(...params: SQLQueryBindings[]): void { |
| 60 | + this.stmt.run(...params); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Node.js SQLite database wrapper with bun:sqlite-compatible API. |
| 66 | + * Used only when running under Node.js (not Bun). |
| 67 | + */ |
| 68 | +class NodeDatabase { |
| 69 | + // biome-ignore lint/suspicious/noExplicitAny: node:sqlite types loaded lazily |
| 70 | + private readonly db: any; |
| 71 | + |
| 72 | + constructor(path: string) { |
| 73 | + // Lazy-load node:sqlite to avoid crashing on runtimes without it. |
| 74 | + // biome-ignore lint/suspicious/noExplicitAny: node:sqlite types loaded lazily |
| 75 | + const { DatabaseSync } = require("node:sqlite") as any; |
| 76 | + this.db = new DatabaseSync(path); |
| 77 | + } |
| 78 | + |
| 79 | + exec(sql: string): void { |
| 80 | + this.db.exec(sql); |
| 81 | + } |
| 82 | + |
| 83 | + query(sql: string): NodeStatementWrapper { |
| 84 | + return new NodeStatementWrapper(this.db.prepare(sql)); |
| 85 | + } |
| 86 | + |
| 87 | + close(): void { |
| 88 | + this.db.close(); |
| 89 | + } |
| 90 | + |
| 91 | + transaction<T>(fn: () => T): () => T { |
| 92 | + return () => { |
| 93 | + this.db.exec("BEGIN"); |
| 94 | + try { |
| 95 | + const result = fn(); |
| 96 | + this.db.exec("COMMIT"); |
| 97 | + return result; |
| 98 | + } catch (error) { |
| 99 | + this.db.exec("ROLLBACK"); |
| 100 | + throw error; |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// Unified export — picks the right implementation at runtime |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +/** |
| 111 | + * SQLite Database class. |
| 112 | + * |
| 113 | + * Under Bun this is the native `bun:sqlite` Database (zero-overhead re-export). |
| 114 | + * Under Node.js this is a wrapper around `node:sqlite`'s DatabaseSync that |
| 115 | + * provides the same `.exec()`, `.query()`, `.close()`, `.transaction()` API. |
| 116 | + */ |
| 117 | +// biome-ignore lint/suspicious/noExplicitAny: conditional runtime export |
| 118 | +export const Database: any = isBun |
| 119 | + ? require("bun:sqlite").Database |
| 120 | + : NodeDatabase; |
| 121 | + |
| 122 | +// Re-export the type so `import type { Database }` works correctly. |
| 123 | +// The type matches the bun:sqlite Database shape used across the codebase. |
| 124 | +export type Database = InstanceType<typeof Database>; |
0 commit comments