@@ -1504,28 +1504,76 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
15041504 return this;
15051505 };
15061506
1507- /** Registers the update hook with SQLite
1508- @param {function(operation, database, table, rowId) | null} callback
1509- executed whenever a row in any rowid table is changed
1510-
1511- For each changed row, the callback is called once with the change
1512- ('insert', 'update' or 'delete'), the database name and table name
1513- where the change happened and the rowid of the row that has been
1514- changed.
1515-
1516- rowid is cast to a plain number, if it exceeds Number.MAX_SAFE_INTEGER
1517- an error will be thrown.
1518-
1519- The callback MUST NOT modify the database in any way.
1520-
1521- Only a single callback can be registered. Unregister the callback by
1522- passing null.
1523-
1524- Not called for some updates like ON REPLACE CONFLICT and TRUNCATE (a
1525- DELETE FROM without a WHERE clause).
1526-
1527- See sqlite docs on sqlite3_update_hook for more details.
1528- */
1507+ /** Registers an update hook with SQLite.
1508+ *
1509+ * Every time a row is changed by whatever means, the callback is called
1510+ * once with the change (`'insert'`, `'update'` or `'delete'`), the database
1511+ * name and table name where the change happened and the
1512+ * [rowid](https://www.sqlite.org/rowidtable.html)
1513+ * of the row that has been changed.
1514+ *
1515+ * The rowid is cast to a plain number. If it exceeds
1516+ * `Number.MAX_SAFE_INTEGER` (2^53 - 1), an error will be thrown.
1517+ *
1518+ * **Important notes:**
1519+ * - The callback **MUST NOT** modify the database in any way
1520+ * - Only a single callback can be registered at a time
1521+ * - Unregister the callback by passing `null`
1522+ * - Not called for some updates like `ON REPLACE CONFLICT` and `TRUNCATE`
1523+ * (a `DELETE FROM` without a `WHERE` clause)
1524+ *
1525+ * See SQLite documentation on
1526+ * [sqlite3_update_hook](https://www.sqlite.org/c3ref/update_hook.html)
1527+ * for more details
1528+ *
1529+ * @example
1530+ * // Create a database and table
1531+ * var db = new SQL.Database();
1532+ * db.exec(`
1533+ * CREATE TABLE users (
1534+ * id INTEGER PRIMARY KEY, -- this is the rowid column
1535+ * name TEXT,
1536+ * active INTEGER
1537+ * )
1538+ * `);
1539+ *
1540+ * // Register an update hook
1541+ * var changes = [];
1542+ * db.updateHook(function(operation, database, table, rowId) {
1543+ * changes.push({operation, database, table, rowId});
1544+ * console.log(`${operation} on ${database}.${table} row ${rowId}`);
1545+ * });
1546+ *
1547+ * // Insert a row - triggers the update hook with 'insert'
1548+ * db.run("INSERT INTO users VALUES (1, 'Alice', 1)");
1549+ * // Logs: "insert on main.users row 1"
1550+ *
1551+ * // Update a row - triggers the update hook with 'update'
1552+ * db.run("UPDATE users SET active = 0 WHERE id = 1");
1553+ * // Logs: "update on main.users row 1"
1554+ *
1555+ * // Delete a row - triggers the update hook with 'delete'
1556+ * db.run("DELETE FROM users WHERE id = 1");
1557+ * // Logs: "delete on main.users row 1"
1558+ *
1559+ * // Unregister the update hook
1560+ * db.updateHook(null);
1561+ *
1562+ * // This won't trigger any callback
1563+ * db.run("INSERT INTO users VALUES (2, 'Bob', 1)");
1564+ *
1565+ * @param {function|null} callback -
1566+ * Callback to be executed whenever a row changes.
1567+ * Set to `null` to unregister the callback.
1568+ * @param {string} callback.operation -
1569+ * 'insert', 'update', or 'delete'
1570+ * @param {string} callback.database -
1571+ * database where the change occurred
1572+ * @param {string} callback.table -
1573+ * table where the change occurred
1574+ * @param {number} callback.rowId -
1575+ * rowid of the changed row
1576+ */
15291577 Database.prototype["updateHook"] = function updateHook(callback) {
15301578 if (this.updateHookFunctionPtr) {
15311579 // unregister and cleanup a previously registered update hook
0 commit comments