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