Skip to content

Commit 3def263

Browse files
committed
doc: Separate CJS/ESM snippet for applyChangeset
Separate `applyChangeset`'s documentation to CJS and MJS examples. This also allows adding the correct `import`/`require`, and make the snippet executable without any additional "prep work". Fixes: #60394
1 parent 12cab63 commit 3def263

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

doc/api/sqlite.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,29 @@ added:
563563
An exception is thrown if the database is not
564564
open. This method is a wrapper around [`sqlite3changeset_apply()`][].
565565

566-
```js
566+
```mjs
567+
import { DatabaseSync } from 'node:sqlite';
568+
569+
const sourceDb = new DatabaseSync(':memory:');
570+
const targetDb = new DatabaseSync(':memory:');
571+
572+
sourceDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
573+
targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
574+
575+
const session = sourceDb.createSession();
576+
577+
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
578+
insert.run(1, 'hello');
579+
insert.run(2, 'world');
580+
581+
const changeset = session.changeset();
582+
targetDb.applyChangeset(changeset);
583+
// Now that the changeset has been applied, targetDb contains the same data as sourceDb.
584+
```
585+
586+
```cjs
587+
const { DatabaseSync } = require('node:sqlite');
588+
567589
const sourceDb = new DatabaseSync(':memory:');
568590
const targetDb = new DatabaseSync(':memory:');
569591

0 commit comments

Comments
 (0)