* Wraps Node's built-in `node:sqlite` (`DatabaseSync`) to match the * better-sqlite3 interface the rest of the code expects. * * node:sqlite is real SQLite compiled into Node, so it supports WAL, FTS5, * mmap, and `@named` params natively — the only shims needed are the * better-sqlite3 conveni
| 48 | * `.transaction()` helper, and `open` (node:sqlite exposes `isOpen`). |
| 49 | */ |
| 50 | class NodeSqliteAdapter implements SqliteDatabase { |
| 51 | private _db: any; |
| 52 | private _txDepth = 0; |
| 53 | |
| 54 | constructor(dbPath: string, opts?: { readOnly?: boolean }) { |
| 55 | // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 56 | const { DatabaseSync } = require('node:sqlite'); |
| 57 | this._db = opts?.readOnly ? new DatabaseSync(dbPath, { readOnly: true }) : new DatabaseSync(dbPath); |
| 58 | } |
| 59 | |
| 60 | get open(): boolean { |
| 61 | return this._db.isOpen; |
| 62 | } |
| 63 | |
| 64 | prepare(sql: string): SqliteStatement { |
| 65 | // node:sqlite matches better-sqlite3's calling convention (variadic |
| 66 | // positional args, or a single object for @named params), so params forward |
| 67 | // through unchanged. |
| 68 | const stmt = this._db.prepare(sql); |
| 69 | return { |
| 70 | run(...params: any[]) { |
| 71 | const r = stmt.run(...params); |
| 72 | return { |
| 73 | changes: Number(r?.changes ?? 0), |
| 74 | lastInsertRowid: r?.lastInsertRowid ?? 0, |
| 75 | }; |
| 76 | }, |
| 77 | get(...params: any[]) { |
| 78 | return stmt.get(...params); |
| 79 | }, |
| 80 | all(...params: any[]) { |
| 81 | return stmt.all(...params); |
| 82 | }, |
| 83 | iterate(...params: any[]) { |
| 84 | return stmt.iterate(...params); |
| 85 | }, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | exec(sql: string): void { |
| 90 | this._db.exec(sql); |
| 91 | } |
| 92 | |
| 93 | pragma(str: string, options?: { simple?: boolean }): any { |
| 94 | const trimmed = str.trim(); |
| 95 | // Write pragma ("key = value"): node:sqlite is real SQLite, so every pragma |
| 96 | // (WAL, mmap, synchronous, …) applies as-is. |
| 97 | if (trimmed.includes('=')) { |
| 98 | this._db.exec(`PRAGMA ${trimmed}`); |
| 99 | return; |
| 100 | } |
| 101 | // Read pragma. Default: the row object (e.g. { journal_mode: 'wal' }). |
| 102 | // `{ simple: true }` returns just the single column value, like better-sqlite3. |
| 103 | const row = this._db.prepare(`PRAGMA ${trimmed}`).get(); |
| 104 | if (options?.simple) { |
| 105 | return row && typeof row === 'object' ? Object.values(row)[0] : row; |
| 106 | } |
| 107 | return row; |
nothing calls this directly
no outgoing calls
no test coverage detected