Skip to content

Commit 4b20c09

Browse files
committed
sqlite: move channel doc
1 parent 9908e0b commit 4b20c09

2 files changed

Lines changed: 69 additions & 67 deletions

File tree

doc/api/diagnostics_channel.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,70 @@ added: v16.18.0
19111911

19121912
Emitted when a new thread is created.
19131913

1914+
#### SQLite
1915+
1916+
<!-- YAML
1917+
added: REPLACEME
1918+
-->
1919+
1920+
> Stability: 1 - Experimental
1921+
1922+
##### Event: `'sqlite.db.query'`
1923+
1924+
* `sql` {string} The expanded SQL with bound parameter values substituted.
1925+
If expansion fails, the source SQL with unsubstituted placeholders is used
1926+
instead.
1927+
* `database` {DatabaseSync} The [`DatabaseSync`][] instance that executed the
1928+
statement.
1929+
* `duration` {number} The estimated statement run time in nanoseconds.
1930+
1931+
Emitted when a SQL statement is executed against a [`DatabaseSync`][] instance.
1932+
This allows subscribers to observe every SQL statement executed without
1933+
modifying the database code itself. Tracing is zero-cost when there are no
1934+
subscribers.
1935+
1936+
```cjs
1937+
const dc = require('node:diagnostics_channel');
1938+
const { DatabaseSync } = require('node:sqlite');
1939+
1940+
function onQuery({ sql, database, duration }) {
1941+
console.log(sql, duration);
1942+
}
1943+
1944+
dc.subscribe('sqlite.db.query', onQuery);
1945+
1946+
const db = new DatabaseSync(':memory:');
1947+
db.exec('CREATE TABLE t (x INTEGER)');
1948+
// Logs: CREATE TABLE t (x INTEGER) <duration>
1949+
1950+
const stmt = db.prepare('INSERT INTO t VALUES (?)');
1951+
stmt.run(42);
1952+
// Logs: INSERT INTO t VALUES (42.0) <duration>
1953+
1954+
dc.unsubscribe('sqlite.db.query', onQuery);
1955+
```
1956+
1957+
```mjs
1958+
import dc from 'node:diagnostics_channel';
1959+
import { DatabaseSync } from 'node:sqlite';
1960+
1961+
function onQuery({ sql, database, duration }) {
1962+
console.log(sql, duration);
1963+
}
1964+
1965+
dc.subscribe('sqlite.db.query', onQuery);
1966+
1967+
const db = new DatabaseSync(':memory:');
1968+
db.exec('CREATE TABLE t (x INTEGER)');
1969+
// Logs: CREATE TABLE t (x INTEGER) <duration>
1970+
1971+
const stmt = db.prepare('INSERT INTO t VALUES (?)');
1972+
stmt.run(42);
1973+
// Logs: INSERT INTO t VALUES (42.0) <duration>
1974+
1975+
dc.unsubscribe('sqlite.db.query', onQuery);
1976+
```
1977+
19141978
[BoundedChannel Channels]: #boundedchannel-channels
19151979
[TracingChannel Channels]: #tracingchannel-channels
19161980
[`'uncaughtException'`]: process.md#event-uncaughtexception
@@ -1924,6 +1988,7 @@ Emitted when a new thread is created.
19241988
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
19251989
[`channel.unsubscribe(onMessage)`]: #channelunsubscribeonmessage
19261990
[`channel.withStoreScope(data)`]: #channelwithstorescopedata
1991+
[`DatabaseSync`]: sqlite.md#class-databasesync
19271992
[`child_process.spawn()`]: child_process.md#child_processspawncommand-args-options
19281993
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
19291994
[`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage

doc/api/sqlite.md

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import sqlite from 'node:sqlite';
3030
const 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

3537
The following example shows the basic usage of the `node:sqlite` module to open
3638
an 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', {
12811283
console.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

Comments
 (0)