database.deserialize(buffer[, options])


将序列化的数据库加载到此连接中,替换当前数据库。反序列化后的数据库是可写的。在尝试反序列化之前,会先完成现有的预处理语句,即使操作随后失败。如果在数据库回调堆栈上调用此方法(例如用户定义函数、聚合函数、授权器或变更集过滤器或冲突处理器),会抛出 ERR_INVALID_STATE 错误。此方法是 sqlite3_deserialize() 的一个封装。

🌐 Loads a serialized database into this connection, replacing the current database. The deserialized database is writable. Existing prepared statements are finalized before deserialization is attempted, even if the operation subsequently fails. An ERR_INVALID_STATE error is thrown if the method is called while a database callback is on the stack, for example a user-defined function, an aggregate function, an authorizer, or a changeset filter or conflict handler. This method is a wrapper around sqlite3_deserialize().

import { Database } from 'node:sqlite';

const original = new Database(':memory:');
original.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
original.exec("INSERT INTO t VALUES (1, 'hello')");
const buffer = original.serialize();
original.close();

const clone = new Database(':memory:');
clone.deserialize(buffer);
using query = clone.prepare('SELECT value FROM t');
console.log(query.get());
// Prints: { value: 'hello' }const { Database } = require('node:sqlite');

const original = new Database(':memory:');
original.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
original.exec("INSERT INTO t VALUES (1, 'hello')");
const buffer = original.serialize();
original.close();

const clone = new Database(':memory:');
clone.deserialize(buffer);
using query = clone.prepare('SELECT value FROM t');
console.log(query.get());
// Prints: { value: 'hello' }