Skip to content

Commit 04a8fd9

Browse files
committed
sqlite: move channel doc
1 parent abfd152 commit 04a8fd9

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.
@@ -1364,72 +1366,6 @@ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
13641366
console.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

Comments
 (0)