iagate-querykit
Version:
QueryKit: lightweight TypeScript query toolkit with models, views, triggers, events, scheduler and adapters (better-sqlite3).
29 lines (28 loc) • 1.03 kB
JavaScript
import { createRequire } from 'node:module';
export class BetterSqlite3Executor {
db;
constructor(dbFilePath) {
const mocked = globalThis.__vitest_mocks__?.betterSqlite3;
const req = createRequire(import.meta.url);
/* c8 ignore next */
const mod = mocked || req('better-sqlite3');
this.db = new mod(dbFilePath);
}
executeQuerySync(sql, bindings = []) {
const stmt = this.db.prepare(sql);
if (/^select\s/i.test(sql)) {
const data = stmt.all(...bindings);
return { data };
}
const info = stmt.run(...bindings);
return { data: [], affectedRows: info.changes, lastInsertId: info.lastInsertRowid };
}
async executeQuery(sql, bindings = []) {
return this.executeQuerySync(sql, bindings);
}
runSync(sql, bindings = []) {
const stmt = this.db.prepare(sql);
const info = stmt.run(...bindings);
return { changes: info.changes, lastInsertRowid: info.lastInsertRowid };
}
}