@@ -531,6 +531,89 @@ Opens the database specified in the `path` argument of the `DatabaseSync`
531531constructor. This method should only be used when the database is not opened via
532532the constructor. An exception is thrown if the database is already open.
533533
534+ ### ` database.serialize([dbName]) `
535+
536+ <!-- YAML
537+ added: REPLACEME
538+ -->
539+
540+ * ` dbName ` {string} Name of the database to serialize. This can be ` 'main' `
541+ (the default primary database) or any other database that has been added with
542+ [ ` ATTACH DATABASE ` ] [ ] . ** Default:** ` 'main' ` .
543+ * Returns: {Uint8Array} A binary representation of the database.
544+
545+ Serializes the database into a binary representation, returned as a
546+ ` Uint8Array ` . This is useful for saving, cloning, or transferring an in-memory
547+ database. This method is a wrapper around [ ` sqlite3_serialize() ` ] [ ] .
548+
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+
559+ ``` cjs
560+ const { DatabaseSync } = require (' node:sqlite' );
561+
562+ const db = new DatabaseSync (' :memory:' );
563+ db .exec (' CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)' );
564+ db .exec (" INSERT INTO t VALUES (1, 'hello')" );
565+ const buffer = db .serialize ();
566+ console .log (buffer .length ); // Prints the byte length of the database
567+ ```
568+
569+ ### ` database.deserialize(buffer[, options]) `
570+
571+ <!-- YAML
572+ added: REPLACEME
573+ -->
574+
575+ * ` buffer ` {Uint8Array} A binary representation of a database, such as the
576+ output of [ ` database.serialize() ` ] [ ] .
577+ * ` options ` {Object} Optional configuration for the deserialization.
578+ * ` dbName ` {string} Name of the database to deserialize into.
579+ ** Default:** ` 'main' ` .
580+
581+ Loads a serialized database into this connection, replacing the current
582+ database. The deserialized database is writable. Existing prepared statements
583+ are finalized before deserialization is attempted, even if the operation
584+ subsequently fails. This method is a wrapper around
585+ [ ` sqlite3_deserialize() ` ] [ ] .
586+
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+
602+ ``` cjs
603+ const { DatabaseSync } = require (' node:sqlite' );
604+
605+ const original = new DatabaseSync (' :memory:' );
606+ original .exec (' CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)' );
607+ original .exec (" INSERT INTO t VALUES (1, 'hello')" );
608+ const buffer = original .serialize ();
609+ original .close ();
610+
611+ const clone = new DatabaseSync (' :memory:' );
612+ clone .deserialize (buffer);
613+ console .log (clone .prepare (' SELECT value FROM t' ).get ());
614+ // Prints: { value: 'hello' }
615+ ```
616+
534617### ` database.prepare(sql[, options]) `
535618
536619<!-- YAML
@@ -1545,6 +1628,7 @@ callback function to indicate what type of operation is being authorized.
15451628[ `SQLTagStore` ] : #class-sqltagstore
15461629[ `database.applyChangeset()` ] : #databaseapplychangesetchangeset-options
15471630[ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
1631+ [ `database.serialize()` ] : #databaseserializedbname
15481632[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
15491633[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
15501634[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
@@ -1559,12 +1643,14 @@ callback function to indicate what type of operation is being authorized.
15591643[ `sqlite3_create_function_v2()` ] : https://www.sqlite.org/c3ref/create_function.html
15601644[ `sqlite3_create_window_function()` ] : https://www.sqlite.org/c3ref/create_function.html
15611645[ `sqlite3_db_filename()` ] : https://sqlite.org/c3ref/db_filename.html
1646+ [ `sqlite3_deserialize()` ] : https://sqlite.org/c3ref/deserialize.html
15621647[ `sqlite3_exec()` ] : https://www.sqlite.org/c3ref/exec.html
15631648[ `sqlite3_expanded_sql()` ] : https://www.sqlite.org/c3ref/expanded_sql.html
15641649[ `sqlite3_get_autocommit()` ] : https://sqlite.org/c3ref/get_autocommit.html
15651650[ `sqlite3_last_insert_rowid()` ] : https://www.sqlite.org/c3ref/last_insert_rowid.html
15661651[ `sqlite3_load_extension()` ] : https://www.sqlite.org/c3ref/load_extension.html
15671652[ `sqlite3_prepare_v2()` ] : https://www.sqlite.org/c3ref/prepare.html
1653+ [ `sqlite3_serialize()` ] : https://sqlite.org/c3ref/serialize.html
15681654[ `sqlite3_set_authorizer()` ] : https://sqlite.org/c3ref/set_authorizer.html
15691655[ `sqlite3_sql()` ] : https://www.sqlite.org/c3ref/expanded_sql.html
15701656[ `sqlite3changeset_apply()` ] : https://www.sqlite.org/session/sqlite3changeset_apply.html
0 commit comments