Skip to content

Commit 607d8c3

Browse files
committed
doc: added mjs samples for sqlite
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com>
1 parent 86127c0 commit 607d8c3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/api/sqlite.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ Serializes the database into a binary representation, returned as a
546546
`Uint8Array`. This is useful for saving, cloning, or transferring an in-memory
547547
database. This method is a wrapper around [`sqlite3_serialize()`][].
548548

549+
```mjs
550+
import { DatabaseSync } from 'node:sqlite';
551+
552+
const db = new DatabaseSync(':memory:');
553+
db.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
554+
db.exec("INSERT INTO t VALUES (1, 'hello')");
555+
const buffer = db.serialize();
556+
console.log(buffer.length); // Prints the byte length of the database
557+
```
558+
549559
```cjs
550560
const { DatabaseSync } = require('node:sqlite');
551561

@@ -574,6 +584,21 @@ are finalized before deserialization is attempted, even if the operation
574584
subsequently fails. This method is a wrapper around
575585
[`sqlite3_deserialize()`][].
576586

587+
```mjs
588+
import { DatabaseSync } from 'node:sqlite';
589+
590+
const original = new DatabaseSync(':memory:');
591+
original.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
592+
original.exec("INSERT INTO t VALUES (1, 'hello')");
593+
const buffer = original.serialize();
594+
original.close();
595+
596+
const clone = new DatabaseSync(':memory:');
597+
clone.deserialize(buffer);
598+
console.log(clone.prepare('SELECT value FROM t').get());
599+
// Prints: { value: 'hello' }
600+
```
601+
577602
```cjs
578603
const { DatabaseSync } = require('node:sqlite');
579604

0 commit comments

Comments
 (0)