Skip to content

Commit 9ca9b5f

Browse files
authored
🤖 Merge PR DefinitelyTyped#74764 Add method updateHook on Database in sql.js by @vitonsky
1 parent c19b4e9 commit 9ca9b5f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

‎types/sql.js/index.d.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ type ParamsCallback = (obj: ParamsObject) => void;
77
type SqlJsConfig = Partial<EmscriptenModule>;
88
type BindParams = SqlValue[] | ParamsObject | null;
99

10+
type UpdateHookOperation = "insert" | "update" | "delete";
11+
type UpdateHookCallback = (
12+
operation: UpdateHookOperation,
13+
database: string,
14+
table: string,
15+
rowId: number,
16+
) => void;
17+
1018
interface QueryExecResult {
1119
columns: string[];
1220
values: SqlValue[][];
@@ -149,6 +157,36 @@ declare class Database {
149157
* `;`). This limitation does not apply to params as an object.
150158
*/
151159
run(sql: string, params?: BindParams): Database;
160+
161+
/** Registers an update hook with SQLite.
162+
*
163+
* Every time a row is changed by whatever means, the callback is called
164+
* once with the change (`'insert'`, `'update'` or `'delete'`), the database
165+
* name and table name where the change happened and the
166+
* [rowid](https://www.sqlite.org/rowidtable.html)
167+
* of the row that has been changed.
168+
*
169+
* The rowid is cast to a plain number. If it exceeds
170+
* `Number.MAX_SAFE_INTEGER` (2^53 - 1), an error will be thrown.
171+
*
172+
* **Important notes:**
173+
* - The callback **MUST NOT** modify the database in any way
174+
* - Only a single callback can be registered at a time
175+
* - Unregister the callback by passing `null`
176+
* - Not called for some updates like `ON REPLACE CONFLICT` and `TRUNCATE`
177+
* (a `DELETE FROM` without a `WHERE` clause)
178+
*
179+
* See SQLite documentation on
180+
* [sqlite3_update_hook](https://www.sqlite.org/c3ref/update_hook.html)
181+
* for more details
182+
*
183+
* @param callback
184+
* - Callback to be executed when a row changes. Takes the type of change,
185+
* the name of the database, the name of the table, and the row id of the
186+
* changed row.
187+
* - Set to `null` to unregister.
188+
*/
189+
updateHook(callback: UpdateHookCallback | null): Database;
152190
}
153191

154192
declare class Statement {
@@ -290,6 +328,7 @@ declare namespace initSqlJs {
290328
// types
291329
SqlValue,
292330
StatementIteratorResult,
331+
UpdateHookCallback,
293332
};
294333

295334
// classes

‎types/sql.js/sql.js-tests.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ initSqlJs().then(SqlJs => {
2828
+ "CREATE TABLE test_table (id INTEGER PRIMARY KEY, content TEXT);";
2929
db.run(createTableStatement);
3030

31+
// Register handler for database changes
32+
db.updateHook((operation, database, table, rowId) => {
33+
console.log("SQLite database is updated", { operation, database, table, rowId });
34+
});
35+
3136
// Insert 2 records for testing.
3237
const insertRecordStatement = "INSERT INTO test_table (id, content) VALUES (@id, @content);";
3338
db.run(insertRecordStatement, {

0 commit comments

Comments
 (0)