@@ -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.
@@ -1281,72 +1283,6 @@ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
12811283console .log (' Backup completed' , totalPagesTransferred);
12821284```
12831285
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 an {Object} with the following
1299- properties:
1300-
1301- * ` sql ` {string} The expanded SQL with bound parameter values substituted.
1302- If expansion fails, the source SQL with unsubstituted placeholders is used
1303- instead.
1304- * ` database ` {DatabaseSync} The ` DatabaseSync ` instance that executed the
1305- statement.
1306- * ` duration ` {number} The estimated statement run time in nanoseconds.
1307-
1308- ``` cjs
1309- const dc = require (' node:diagnostics_channel' );
1310- const { DatabaseSync } = require (' node:sqlite' );
1311-
1312- function onQuery ({ sql, database, duration }) {
1313- console .log (sql, duration);
1314- }
1315-
1316- dc .subscribe (' sqlite.db.query' , onQuery);
1317-
1318- const db = new DatabaseSync (' :memory:' );
1319- db .exec (' CREATE TABLE t (x INTEGER)' );
1320- // Logs: CREATE TABLE t (x INTEGER) <duration>
1321-
1322- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1323- stmt .run (42 );
1324- // Logs: INSERT INTO t VALUES (42.0) <duration>
1325-
1326- dc .unsubscribe (' sqlite.db.query' , onQuery);
1327- ```
1328-
1329- ``` mjs
1330- import dc from ' node:diagnostics_channel' ;
1331- import { DatabaseSync } from ' node:sqlite' ;
1332-
1333- function onQuery ({ sql, database, duration }) {
1334- console .log (sql, duration);
1335- }
1336-
1337- dc .subscribe (' sqlite.db.query' , onQuery);
1338-
1339- const db = new DatabaseSync (' :memory:' );
1340- db .exec (' CREATE TABLE t (x INTEGER)' );
1341- // Logs: CREATE TABLE t (x INTEGER) <duration>
1342-
1343- const stmt = db .prepare (' INSERT INTO t VALUES (?)' );
1344- stmt .run (42 );
1345- // Logs: INSERT INTO t VALUES (42.0) <duration>
1346-
1347- dc .unsubscribe (' sqlite.db.query' , onQuery);
1348- ```
1349-
13501286## ` sqlite.constants `
13511287
13521288<!-- YAML
@@ -1612,6 +1548,7 @@ callback function to indicate what type of operation is being authorized.
16121548[ `database.applyChangeset()` ] : #databaseapplychangesetchangeset-options
16131549[ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
16141550[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
1551+ [ `'sqlite.db.query'` ] : diagnostics_channel.md#event-sqlitedbquery
16151552[ `diagnostics_channel` ] : diagnostics_channel.md
16161553[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
16171554[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
0 commit comments