@@ -1399,28 +1399,76 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
1399
1399
return this ;
1400
1400
} ;
1401
1401
1402
- /** Registers the update hook with SQLite
1403
- @param {function(operation, database, table, rowId) | null } callback
1404
- executed whenever a row in any rowid table is changed
1405
-
1406
- For each changed row, the callback is called once with the change
1407
- ('insert', 'update' or 'delete'), the database name and table name
1408
- where the change happened and the rowid of the row that has been
1409
- changed.
1410
-
1411
- rowid is cast to a plain number, if it exceeds Number.MAX_SAFE_INTEGER
1412
- an error will be thrown.
1413
-
1414
- The callback MUST NOT modify the database in any way.
1415
-
1416
- Only a single callback can be registered. Unregister the callback by
1417
- passing null.
1418
-
1419
- Not called for some updates like ON REPLACE CONFLICT and TRUNCATE (a
1420
- DELETE FROM without a WHERE clause).
1421
-
1422
- See sqlite docs on sqlite3_update_hook for more details.
1423
- */
1402
+ /** Registers an update hook with SQLite.
1403
+ *
1404
+ * Every time a row is changed by whatever means, the callback is called
1405
+ * once with the change (`'insert'`, `'update'` or `'delete'`), the database
1406
+ * name and table name where the change happened and the
1407
+ * [rowid](https://www.sqlite.org/rowidtable.html)
1408
+ * of the row that has been changed.
1409
+ *
1410
+ * The rowid is cast to a plain number. If it exceeds
1411
+ * `Number.MAX_SAFE_INTEGER` (2^53 - 1), an error will be thrown.
1412
+ *
1413
+ * **Important notes:**
1414
+ * - The callback **MUST NOT** modify the database in any way
1415
+ * - Only a single callback can be registered at a time
1416
+ * - Unregister the callback by passing `null`
1417
+ * - Not called for some updates like `ON REPLACE CONFLICT` and `TRUNCATE`
1418
+ * (a `DELETE FROM` without a `WHERE` clause)
1419
+ *
1420
+ * See SQLite documentation on
1421
+ * [sqlite3_update_hook](https://www.sqlite.org/c3ref/update_hook.html)
1422
+ * for more details
1423
+ *
1424
+ * @example
1425
+ * // Create a database and table
1426
+ * var db = new SQL.Database();
1427
+ * db.exec(`
1428
+ * CREATE TABLE users (
1429
+ * id INTEGER PRIMARY KEY, -- this is the rowid column
1430
+ * name TEXT,
1431
+ * active INTEGER
1432
+ * )
1433
+ * `);
1434
+ *
1435
+ * // Register an update hook
1436
+ * var changes = [];
1437
+ * db.updateHook(function(operation, database, table, rowId) {
1438
+ * changes.push({operation, database, table, rowId});
1439
+ * console.log(`${operation} on ${database}.${table} row ${rowId}`);
1440
+ * });
1441
+ *
1442
+ * // Insert a row - triggers the update hook with 'insert'
1443
+ * db.run("INSERT INTO users VALUES (1, 'Alice', 1)");
1444
+ * // Logs: "insert on main.users row 1"
1445
+ *
1446
+ * // Update a row - triggers the update hook with 'update'
1447
+ * db.run("UPDATE users SET active = 0 WHERE id = 1");
1448
+ * // Logs: "update on main.users row 1"
1449
+ *
1450
+ * // Delete a row - triggers the update hook with 'delete'
1451
+ * db.run("DELETE FROM users WHERE id = 1");
1452
+ * // Logs: "delete on main.users row 1"
1453
+ *
1454
+ * // Unregister the update hook
1455
+ * db.updateHook(null);
1456
+ *
1457
+ * // This won't trigger any callback
1458
+ * db.run("INSERT INTO users VALUES (2, 'Bob', 1)");
1459
+ *
1460
+ * @param {function|null } callback -
1461
+ * Callback to be executed whenever a row changes.
1462
+ * Set to `null` to unregister the callback.
1463
+ * @param {string } callback.operation -
1464
+ * 'insert', 'update', or 'delete'
1465
+ * @param {string } callback.database -
1466
+ * database where the change occurred
1467
+ * @param {string } callback.table -
1468
+ * table where the change occurred
1469
+ * @param {number } callback.rowId -
1470
+ * rowid of the changed row
1471
+ */
1424
1472
Database . prototype [ "updateHook" ] = function updateHook ( callback ) {
1425
1473
if ( this . updateHookFunctionPtr ) {
1426
1474
// unregister and cleanup a previously registered update hook
0 commit comments