@@ -30,7 +30,9 @@ import sqlite from 'node:sqlite';
3030const sqlite = require (' node:sqlite' );
3131```
3232
33- This module is only available under the ` node: ` scheme.
33+ This module is only available under the ` node: ` scheme. SQL trace events can
34+ be observed via the [ ` diagnostics_channel ` ] [ ] module. See
35+ [ ` 'sqlite.db.query' ` ] [ ] for details.
3436
3537The following example shows the basic usage of the ` node:sqlite ` module to open
3638an in-memory database, write data to the database, and then read the data back.
@@ -1364,72 +1366,6 @@ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
13641366console .log (' Backup completed' , totalPagesTransferred);
13651367```
13661368
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 an {Object} with the following
1382- properties:
1383-
1384- * ` sql ` {string} The expanded SQL with bound parameter values substituted.
1385- If expansion fails, the source SQL with unsubstituted placeholders is used
1386- instead.
1387- * ` database ` {DatabaseSync} The ` DatabaseSync ` instance that executed the
1388- statement.
1389- * ` duration ` {number} The estimated statement run time in nanoseconds.
1390-
1391- ``` cjs
1392- const dc = require (' node:diagnostics_channel' );
1393- const { DatabaseSync } = require (' node:sqlite' );
1394-
1395- function onQuery ({ sql, database, duration }) {
1396- console .log (sql, duration);
1397- }
1398-
1399- dc .subscribe (' sqlite.db.query' , onQuery);
1400-
1401- const db = new DatabaseSync (' :memory:' );
1402- db .exec (' CREATE TABLE t (x INTEGER)' );
1403- // Logs: CREATE TABLE t (x INTEGER) <duration>
1404-
1405- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1406- stmt .run (42 );
1407- // Logs: INSERT INTO t VALUES (42.0) <duration>
1408-
1409- dc .unsubscribe (' sqlite.db.query' , onQuery);
1410- ```
1411-
1412- ``` mjs
1413- import dc from ' node:diagnostics_channel' ;
1414- import { DatabaseSync } from ' node:sqlite' ;
1415-
1416- function onQuery ({ sql, database, duration }) {
1417- console .log (sql, duration);
1418- }
1419-
1420- dc .subscribe (' sqlite.db.query' , onQuery);
1421-
1422- const db = new DatabaseSync (' :memory:' );
1423- db .exec (' CREATE TABLE t (x INTEGER)' );
1424- // Logs: CREATE TABLE t (x INTEGER) <duration>
1425-
1426- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1427- stmt .run (42 );
1428- // Logs: INSERT INTO t VALUES (42.0) <duration>
1429-
1430- dc .unsubscribe (' sqlite.db.query' , onQuery);
1431- ```
1432-
14331369## ` sqlite.constants `
14341370
14351371<!-- YAML
@@ -1696,6 +1632,7 @@ callback function to indicate what type of operation is being authorized.
16961632[ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
16971633[ `database.serialize()` ] : #databaseserializedbname
16981634[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
1635+ [ `'sqlite.db.query'` ] : diagnostics_channel.md#event-sqlitedbquery
16991636[ `diagnostics_channel` ] : diagnostics_channel.md
17001637[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
17011638[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
0 commit comments