Skip to content

sqlite: add serialize() and deserialize() to DatabaseSync #62575

@thisalihassan

Description

@thisalihassan

What is the problem this feature will solve?

There is currently no way to extract a database's contents as a binary buffer or load a database from one. Users may use this for snapshot, clone, transfer, etc. Popular libraries like better-sqlite3 expose db.serialize() and new Database(buffer) for this purpose.

What is the feature you are proposing to solve the problem?

Add serialize() and deserialize() methods to DatabaseSync:

const { DatabaseSync } = require('node:sqlite');

// serialize: returns the database as a Uint8Array
const db = new DatabaseSync(':memory:');
db.exec("CREATE TABLE t(x TEXT)");
db.exec("INSERT INTO t VALUES ('hello')");
const buffer = db.serialize(); // Uint8Array

// deserialize: loads a buffer into the connection, replacing the current database
const db2 = new DatabaseSync(':memory:');
db2.deserialize(buffer);
db2.prepare('SELECT * FROM t').get(); // { x: 'hello' }

What alternatives have you considered?

  1. VACUUM INTO + fs.readFileSync: Requires disk I/O, a temp file path, and manual cleanup
  2. backup() API

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.sqliteIssues and PRs related to the SQLite subsystem.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions