跳到主要内容

restorePGliteDatabase()

function restorePGliteDatabase(
source,
target,
options?
): Promise<PGliteRestoreResult>;

Defined in: packages/rxdb-adapter-pglite/src/backup/restore-pglite-database.ts:403

把 RxDBAdapterPGlite.backup 产出的归档恢复成一个新的 PGlite 数据库。

Parameters​

ParameterTypeDescription
sourceReadableStream<Uint8Array<ArrayBufferLike>>归档字节流
targetPGliteRestoreTarget尚未连接的目标实例与其 adapter 选项
optionsPGliteRestoreOptions取消信号与阶段回调

Returns​

Promise<PGliteRestoreResult>

结束标记、manifest,以及内存目标的数据库句柄

Remarks​

只写入空目标,不做合并或覆盖。兼容性(引擎大版本、扩展、系统表 / 变更编码版本、实体结构指纹、 加密认证域)在写入第一个字节之前判定;归档摘要要读到结尾才知道,所以数据先写进暂存区, 通过摘要与内容核对之后才提交:

  • IndexedDB 目标:独占 Web Lock 挡住并发连接与并发恢复,持久标记挡住「恢复中途页面被关」后的 下一次连接(报 restore_incomplete,需 cleanupIncompletePGliteRestore)。数据在 验证通过后一次性刷入 IndexedDB,任何失败都删除目标与标记,目标回到从未恢复过的状态。
  • 内存目标:没有可以「之后再打开」的位置,恢复出的实例经 PGliteRestoreResult.database 交给目标 adapter 领取。

目标 RxDB 实例会被 init()(计算实体集合),但不会被连接。输入流在失败时被取消,成功时只释放读锁。

Throws​

RxDBBackupError 见 RxDBBackupErrorCode;cleanup_pending 表示失败后连清理也没做完

Example​

const target = { rxdb, options: { store: 'idb' } };
await restorePGliteDatabase(file.stream(), target);
rxdb.adapter('pglite', db => new RxDBAdapterPGlite(db, target.options));
await rxdb.connect('pglite');