Skip to content

Commit 89b4404

Browse files
author
Theo Gravity
authored
Merge pull request #48 from mike-north/sqlite3-debugging-hooks
Expose sqlite3 debugging hooks
2 parents 2d4410d + aeeec71 commit 89b4404

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Database.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99

1010
import fs from 'fs';
1111
import path from 'path';
12+
// eslint-disable-next-line no-unused-vars,import/no-unresolved,import/extensions
13+
import sqlite3 from 'sqlite3'; // import sqlite3 for jsdoc type information only
1214
import Statement from './Statement';
1315
import prepareParams from './utils';
1416

1517
class Database {
16-
1718
/**
1819
* Initializes a new instance of the database client.
19-
* @param driver An instance of SQLite3 driver library.
20-
* @param promiseLibrary ES6 Promise library to use.
20+
* @param {sqlite3.Database} driver An instance of SQLite3 driver library.
21+
* @param {{Promise: PromiseConstructor}} promiseLibrary ES6 Promise library to use.
2122
*/
22-
constructor(driver, { Promise }) {
23+
constructor(driver, promiseLibrary) {
2324
this.driver = driver;
24-
this.Promise = Promise;
25+
this.Promise = promiseLibrary.Promise;
2526
}
2627

2728
/**
@@ -39,6 +40,16 @@ class Database {
3940
});
4041
}
4142

43+
/**
44+
* Register listeners for Sqlite3 events
45+
*
46+
* @param {'trace'|'profile'|'error'|'open'|'close'} eventName
47+
* @param {() => void} listener trigger listener function
48+
*/
49+
on(eventName, listener) {
50+
this.driver.on(eventName, listener);
51+
}
52+
4253
run(sql) {
4354
const params = prepareParams(arguments, { offset: 1 });
4455
const Promise = this.Promise;

src/main.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ declare module 'sqlite' {
5757
prepare(sql: string, ...params: any[]): Promise<Statement>;
5858

5959
migrate(options: { force?: string, table?: string, migrationsPath?: string }): Promise<Database>;
60+
61+
on(event: "trace", listener: (sql: string) => void): void;
62+
on(event: "profile", listener: (sql: string, time: number) => void): void;
63+
on(event: "error", listener: (err: Error) => void): void;
64+
on(event: "open" | "close", listener: () => void): void;
65+
on(event: string, listener: (...args: any[]) => void): void;
6066
}
6167

6268
export function open(filename: string, options?: { mode?: number, verbose?: boolean, promise?: typeof Promise }): Promise<Database>;

0 commit comments

Comments
 (0)