* 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
| 72 | * `.transaction()` helper, and `open` (node:sqlite exposes `isOpen`). |
| 73 | */ |
| 74 | class NodeSqliteAdapter implements SqliteDatabase { |
| 75 | private _db: any; |
| 76 | private _txDepth = 0; |
| 77 | private readonly _dbPath: string; |
| 78 | |
| 79 | constructor(dbPath: string, opts?: { readOnly?: boolean }) { |
| 80 | const { DatabaseSync } = loadNodeSqlite(); |
| 81 | this._dbPath = dbPath; |
| 82 | this._db = opts?.readOnly ? new DatabaseSync(dbPath, { readOnly: true }) : new DatabaseSync(dbPath); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * What a failed call throws: the error itself, or — for a "disk I/O error" |
| 87 | * on a WSL index that Windows CodeGraph shares — the actionable rewrite |
| 88 | * (#995). Every open runs its PRAGMAs and first reads through the methods |
| 89 | * below, and so does every later query. `iterate()` is left raw: a |
| 90 | * row-by-row wrapper would tax the unbounded scans it exists for, and a |
| 91 | * session reads through `get`/`all` long before it reaches one. |
| 92 | */ |
| 93 | private failure(err: unknown): unknown { |
| 94 | return toWslSharedIndexError(err, this._dbPath) ?? err; |
| 95 | } |
| 96 | |
| 97 | get inTransaction(): boolean | undefined { |
| 98 | return this._db.isTransaction; |
| 99 | } |
| 100 | |
| 101 | get open(): boolean { |
| 102 | return this._db.isOpen; |
| 103 | } |
| 104 | |
| 105 | prepare(sql: string): SqliteStatement { |
| 106 | // node:sqlite matches better-sqlite3's calling convention (variadic |
| 107 | // positional args, or a single object for @named params), so params forward |
| 108 | // through unchanged. |
| 109 | let stmt: any; |
| 110 | try { |
| 111 | stmt = this._db.prepare(sql); |
| 112 | } catch (err) { |
| 113 | throw this.failure(err); |
| 114 | } |
| 115 | const failure = (err: unknown): unknown => this.failure(err); |
| 116 | return { |
| 117 | run(...params: any[]) { |
| 118 | let r: any; |
| 119 | try { |
| 120 | r = stmt.run(...params); |
| 121 | } catch (err) { |
| 122 | throw failure(err); |
| 123 | } |
| 124 | return { |
| 125 | changes: Number(r?.changes ?? 0), |
| 126 | lastInsertRowid: r?.lastInsertRowid ?? 0, |
| 127 | }; |
| 128 | }, |
| 129 | get(...params: any[]) { |
| 130 | try { |
| 131 | return stmt.get(...params); |
nothing calls this directly
no outgoing calls
no test coverage detected