@@ -7,6 +7,14 @@ type ParamsCallback = (obj: ParamsObject) => void;
77type SqlJsConfig = Partial < EmscriptenModule > ;
88type 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+
1018interface 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
154192declare class Statement {
@@ -290,6 +328,7 @@ declare namespace initSqlJs {
290328 // types
291329 SqlValue ,
292330 StatementIteratorResult ,
331+ UpdateHookCallback ,
293332 } ;
294333
295334 // classes
0 commit comments