@@ -395,56 +395,6 @@ added:
395395This method is used to create SQLite user-defined functions. This method is a
396396wrapper around [ ` sqlite3_create_function_v2() ` ] [ ] .
397397
398- ### ` database.setSqlTraceHook(hook) `
399-
400- <!-- YAML
401- added: REPLACEME
402- -->
403-
404- * ` hook ` {Function|null} The trace function to set, or ` null ` to clear
405- the current hook.
406-
407- Sets a hook that SQLite invokes for every SQL statement executed against the
408- database. The hook receives the expanded SQL string (with bound parameter
409- values substituted) as its only argument. If expansion fails, the source SQL
410- with unsubstituted placeholders is passed instead.
411-
412- This method is a wrapper around [ ` sqlite3_trace_v2() ` ] [ ] .
413-
414- ``` cjs
415- const { DatabaseSync } = require (' node:sqlite' );
416- const db = new DatabaseSync (' :memory:' );
417-
418- db .setSqlTraceHook ((sql ) => console .log (sql));
419-
420- db .exec (' CREATE TABLE t (x INTEGER)' );
421- // Logs: CREATE TABLE t (x INTEGER)
422-
423- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
424- stmt .run (42 );
425- // Logs: INSERT INTO t VALUES (42.0)
426-
427- // Clear the hook
428- db .setSqlTraceHook (null );
429- ```
430-
431- ``` mjs
432- import { DatabaseSync } from ' node:sqlite' ;
433- const db = new DatabaseSync (' :memory:' );
434-
435- db .setSqlTraceHook ((sql ) => console .log (sql));
436-
437- db .exec (' CREATE TABLE t (x INTEGER)' );
438- // Logs: CREATE TABLE t (x INTEGER)
439-
440- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
441- stmt .run (42 );
442- // Logs: INSERT INTO t VALUES (42.0)
443-
444- // Clear the hook
445- db .setSqlTraceHook (null );
446- ```
447-
448398### ` database.setAuthorizer(callback) `
449399
450400<!-- YAML
@@ -1414,6 +1364,66 @@ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
14141364console .log (' Backup completed' , totalPagesTransferred);
14151365```
14161366
1367+ ## Diagnostics channel
1368+
1369+ <!-- YAML
1370+ added: REPLACEME
1371+ -->
1372+
1373+ The ` node:sqlite ` module publishes SQL trace events on the
1374+ [ ` diagnostics_channel ` ] [ ] channel ` sqlite.db.query ` . This allows subscribers
1375+ to observe every SQL statement executed against any ` DatabaseSync ` instance
1376+ without modifying the database code itself. Tracing is zero-cost when there
1377+ are no subscribers.
1378+
1379+ ### Channel ` sqlite.db.query `
1380+
1381+ The message published to this channel is a {string} containing the expanded
1382+ SQL with bound parameter values substituted. If expansion fails, the source
1383+ SQL with unsubstituted placeholders is used instead.
1384+
1385+ ``` cjs
1386+ const dc = require (' node:diagnostics_channel' );
1387+ const { DatabaseSync } = require (' node:sqlite' );
1388+
1389+ function onQuery (sql ) {
1390+ console .log (sql);
1391+ }
1392+
1393+ dc .subscribe (' sqlite.db.query' , onQuery);
1394+
1395+ const db = new DatabaseSync (' :memory:' );
1396+ db .exec (' CREATE TABLE t (x INTEGER)' );
1397+ // Logs: CREATE TABLE t (x INTEGER)
1398+
1399+ const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1400+ stmt .run (42 );
1401+ // Logs: INSERT INTO t VALUES (42.0)
1402+
1403+ dc .unsubscribe (' sqlite.db.query' , onQuery);
1404+ ```
1405+
1406+ ``` mjs
1407+ import dc from ' node:diagnostics_channel' ;
1408+ import { DatabaseSync } from ' node:sqlite' ;
1409+
1410+ function onQuery (sql ) {
1411+ console .log (sql);
1412+ }
1413+
1414+ dc .subscribe (' sqlite.db.query' , onQuery);
1415+
1416+ const db = new DatabaseSync (' :memory:' );
1417+ db .exec (' CREATE TABLE t (x INTEGER)' );
1418+ // Logs: CREATE TABLE t (x INTEGER)
1419+
1420+ const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1421+ stmt .run (42 );
1422+ // Logs: INSERT INTO t VALUES (42.0)
1423+
1424+ dc .unsubscribe (' sqlite.db.query' , onQuery);
1425+ ```
1426+
14171427## ` sqlite.constants `
14181428
14191429<!-- YAML
@@ -1680,6 +1690,7 @@ callback function to indicate what type of operation is being authorized.
16801690[ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
16811691[ `database.serialize()` ] : #databaseserializedbname
16821692[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
1693+ [ `diagnostics_channel` ] : diagnostics_channel.md
16831694[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
16841695[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
16851696[ `sqlite3_backup_step()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
@@ -1703,7 +1714,6 @@ callback function to indicate what type of operation is being authorized.
17031714[ `sqlite3_serialize()` ] : https://sqlite.org/c3ref/serialize.html
17041715[ `sqlite3_set_authorizer()` ] : https://sqlite.org/c3ref/set_authorizer.html
17051716[ `sqlite3_sql()` ] : https://www.sqlite.org/c3ref/expanded_sql.html
1706- [ `sqlite3_trace_v2()` ] : https://www.sqlite.org/c3ref/trace_v2.html
17071717[ `sqlite3changeset_apply()` ] : https://www.sqlite.org/session/sqlite3changeset_apply.html
17081718[ `sqlite3session_attach()` ] : https://www.sqlite.org/session/sqlite3session_attach.html
17091719[ `sqlite3session_changeset()` ] : https://www.sqlite.org/session/sqlite3session_changeset.html
0 commit comments