|
| 1 | +/** |
| 2 | + * @module @photostructure/knex-sqlite |
| 3 | + * |
| 4 | + * Knex.js dialect for @photostructure/sqlite. Extends the built-in |
| 5 | + * better-sqlite3 client, adapting the connection and query layer to use |
| 6 | + * @photostructure/sqlite's DatabaseSync driver. |
| 7 | + * |
| 8 | + * @see https://github.com/photostructure/knex-sqlite |
| 9 | + * @see https://github.com/photostructure/node-sqlite |
| 10 | + * @license MIT |
| 11 | + */ |
| 12 | + |
| 13 | +const Client_BetterSQLite3 = require('knex/lib/dialects/better-sqlite3'); |
| 14 | + |
| 15 | +class Client_PhotoStructureSQLite extends Client_BetterSQLite3 { |
| 16 | + _driver() { |
| 17 | + // Load @photostructure/sqlite instead of better-sqlite3 |
| 18 | + return require('@photostructure/sqlite'); |
| 19 | + } |
| 20 | + |
| 21 | + async acquireRawConnection() { |
| 22 | + const driver = this.driver; |
| 23 | + const options = this.connectionSettings.options || {}; |
| 24 | + |
| 25 | + // Create the database connection with @photostructure/sqlite |
| 26 | + const db = new driver.DatabaseSync( |
| 27 | + this.connectionSettings.filename, |
| 28 | + { |
| 29 | + readonly: !!options.readonly, |
| 30 | + readBigInts: !!options.safeIntegers, |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + // Enhance the database with better-sqlite3-style methods |
| 35 | + // (adds .pragma(), .transaction(), .pluck(), .raw(), .expand()) |
| 36 | + const enhancedDb = driver.enhance(db); |
| 37 | + |
| 38 | + // Wrap prepare() to add the .reader property that better-sqlite3 provides. |
| 39 | + // Knex uses .reader to decide between .all() (for SELECTs) and .run() |
| 40 | + // (for INSERT/UPDATE/DELETE). We use stmt.columns().length > 0 which |
| 41 | + // matches better-sqlite3's native behavior (sqlite3_column_count >= 1) |
| 42 | + // and correctly handles RETURNING clauses. |
| 43 | + const originalPrepare = enhancedDb.prepare.bind(enhancedDb); |
| 44 | + enhancedDb.prepare = (sql, prepareOptions) => { |
| 45 | + const stmt = originalPrepare(sql, prepareOptions); |
| 46 | + |
| 47 | + Object.defineProperty(stmt, 'reader', { |
| 48 | + value: stmt.columns().length > 0, |
| 49 | + enumerable: true, |
| 50 | + configurable: true, |
| 51 | + writable: false |
| 52 | + }); |
| 53 | + |
| 54 | + return stmt; |
| 55 | + }; |
| 56 | + |
| 57 | + return enhancedDb; |
| 58 | + } |
| 59 | + |
| 60 | + // Override _query to handle the difference between better-sqlite3's safeIntegers() |
| 61 | + // and @photostructure/sqlite's setReadBigInts() |
| 62 | + async _query(connection, obj) { |
| 63 | + if (!obj.sql) throw new Error('The query is empty'); |
| 64 | + |
| 65 | + if (!connection) { |
| 66 | + throw new Error('No connection provided'); |
| 67 | + } |
| 68 | + |
| 69 | + const statement = connection.prepare(obj.sql); |
| 70 | + |
| 71 | + const safeIntegers = this._optSafeIntegers(obj.options); |
| 72 | + if (safeIntegers !== undefined) { |
| 73 | + // @photostructure/sqlite uses setReadBigInts() instead of safeIntegers() |
| 74 | + if (typeof statement.setReadBigInts === 'function') { |
| 75 | + statement.setReadBigInts(safeIntegers); |
| 76 | + } else if (typeof statement.safeIntegers === 'function') { |
| 77 | + // Fallback for better-sqlite3 compatibility |
| 78 | + statement.safeIntegers(safeIntegers); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const bindings = this._formatBindings(obj.bindings); |
| 83 | + |
| 84 | + if (statement.reader) { |
| 85 | + // @photostructure/sqlite expects variadic arguments, not an array |
| 86 | + const response = await statement.all(...bindings); |
| 87 | + obj.response = response; |
| 88 | + return obj; |
| 89 | + } |
| 90 | + |
| 91 | + // @photostructure/sqlite expects variadic arguments, not an array |
| 92 | + const response = await statement.run(...bindings); |
| 93 | + obj.response = response; |
| 94 | + obj.context = { |
| 95 | + lastID: response.lastInsertRowid, |
| 96 | + changes: response.changes, |
| 97 | + }; |
| 98 | + |
| 99 | + return obj; |
| 100 | + } |
| 101 | + |
| 102 | + _optSafeIntegers(options) { |
| 103 | + if ( |
| 104 | + options && |
| 105 | + typeof options === 'object' && |
| 106 | + typeof options.safeIntegers === 'boolean' |
| 107 | + ) { |
| 108 | + return options.safeIntegers; |
| 109 | + } |
| 110 | + return undefined; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +Object.assign(Client_PhotoStructureSQLite.prototype, { |
| 115 | + driverName: '@photostructure/sqlite', |
| 116 | +}); |
| 117 | + |
| 118 | +module.exports = Client_PhotoStructureSQLite; |
0 commit comments