@@ -372,56 +372,6 @@ added:
372372This method is used to create SQLite user-defined functions. This method is a
373373wrapper around [ ` sqlite3_create_function_v2() ` ] [ ] .
374374
375- ### ` database.setSqlTraceHook(hook) `
376-
377- <!-- YAML
378- added: REPLACEME
379- -->
380-
381- * ` hook ` {Function|null} The trace function to set, or ` null ` to clear
382- the current hook.
383-
384- Sets a hook that SQLite invokes for every SQL statement executed against the
385- database. The hook receives the expanded SQL string (with bound parameter
386- values substituted) as its only argument. If expansion fails, the source SQL
387- with unsubstituted placeholders is passed instead.
388-
389- This method is a wrapper around [ ` sqlite3_trace_v2() ` ] [ ] .
390-
391- ``` cjs
392- const { DatabaseSync } = require (' node:sqlite' );
393- const db = new DatabaseSync (' :memory:' );
394-
395- db .setSqlTraceHook ((sql ) => console .log (sql));
396-
397- db .exec (' CREATE TABLE t (x INTEGER)' );
398- // Logs: CREATE TABLE t (x INTEGER)
399-
400- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
401- stmt .run (42 );
402- // Logs: INSERT INTO t VALUES (42.0)
403-
404- // Clear the hook
405- db .setSqlTraceHook (null );
406- ```
407-
408- ``` mjs
409- import { DatabaseSync } from ' node:sqlite' ;
410- const db = new DatabaseSync (' :memory:' );
411-
412- db .setSqlTraceHook ((sql ) => console .log (sql));
413-
414- db .exec (' CREATE TABLE t (x INTEGER)' );
415- // Logs: CREATE TABLE t (x INTEGER)
416-
417- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
418- stmt .run (42 );
419- // Logs: INSERT INTO t VALUES (42.0)
420-
421- // Clear the hook
422- db .setSqlTraceHook (null );
423- ```
424-
425375### ` database.setAuthorizer(callback) `
426376
427377<!-- YAML
@@ -1331,6 +1281,66 @@ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
13311281console .log (' Backup completed' , totalPagesTransferred);
13321282```
13331283
1284+ ## Diagnostics channel
1285+
1286+ <!-- YAML
1287+ added: REPLACEME
1288+ -->
1289+
1290+ The ` node:sqlite ` module publishes SQL trace events on the
1291+ [ ` diagnostics_channel ` ] [ ] channel ` sqlite.db.query ` . This allows subscribers
1292+ to observe every SQL statement executed against any ` DatabaseSync ` instance
1293+ without modifying the database code itself. Tracing is zero-cost when there
1294+ are no subscribers.
1295+
1296+ ### Channel ` sqlite.db.query `
1297+
1298+ The message published to this channel is a {string} containing the expanded
1299+ SQL with bound parameter values substituted. If expansion fails, the source
1300+ SQL with unsubstituted placeholders is used instead.
1301+
1302+ ``` cjs
1303+ const dc = require (' node:diagnostics_channel' );
1304+ const { DatabaseSync } = require (' node:sqlite' );
1305+
1306+ function onQuery (sql ) {
1307+ console .log (sql);
1308+ }
1309+
1310+ dc .subscribe (' sqlite.db.query' , onQuery);
1311+
1312+ const db = new DatabaseSync (' :memory:' );
1313+ db .exec (' CREATE TABLE t (x INTEGER)' );
1314+ // Logs: CREATE TABLE t (x INTEGER)
1315+
1316+ const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1317+ stmt .run (42 );
1318+ // Logs: INSERT INTO t VALUES (42.0)
1319+
1320+ dc .unsubscribe (' sqlite.db.query' , onQuery);
1321+ ```
1322+
1323+ ``` mjs
1324+ import dc from ' node:diagnostics_channel' ;
1325+ import { DatabaseSync } from ' node:sqlite' ;
1326+
1327+ function onQuery (sql ) {
1328+ console .log (sql);
1329+ }
1330+
1331+ dc .subscribe (' sqlite.db.query' , onQuery);
1332+
1333+ const db = new DatabaseSync (' :memory:' );
1334+ db .exec (' CREATE TABLE t (x INTEGER)' );
1335+ // Logs: CREATE TABLE t (x INTEGER)
1336+
1337+ const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1338+ stmt .run (42 );
1339+ // Logs: INSERT INTO t VALUES (42.0)
1340+
1341+ dc .unsubscribe (' sqlite.db.query' , onQuery);
1342+ ```
1343+
13341344## ` sqlite.constants `
13351345
13361346<!-- YAML
@@ -1596,6 +1606,7 @@ callback function to indicate what type of operation is being authorized.
15961606[ `database.applyChangeset()` ] : #databaseapplychangesetchangeset-options
15971607[ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
15981608[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
1609+ [ `diagnostics_channel` ] : diagnostics_channel.md
15991610[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
16001611[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
16011612[ `sqlite3_backup_step()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
@@ -1617,7 +1628,6 @@ callback function to indicate what type of operation is being authorized.
16171628[ `sqlite3_prepare_v2()` ] : https://www.sqlite.org/c3ref/prepare.html
16181629[ `sqlite3_set_authorizer()` ] : https://sqlite.org/c3ref/set_authorizer.html
16191630[ `sqlite3_sql()` ] : https://www.sqlite.org/c3ref/expanded_sql.html
1620- [ `sqlite3_trace_v2()` ] : https://www.sqlite.org/c3ref/trace_v2.html
16211631[ `sqlite3changeset_apply()` ] : https://www.sqlite.org/session/sqlite3changeset_apply.html
16221632[ `sqlite3session_attach()` ] : https://www.sqlite.org/session/sqlite3session_attach.html
16231633[ `sqlite3session_changeset()` ] : https://www.sqlite.org/session/sqlite3session_changeset.html
0 commit comments