From 2eb624de33e1685a54645f8958164a8dd0bd29d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Rib=C3=B3?= Date: Sat, 28 Oct 2023 18:35:22 +0200 Subject: [PATCH] feat: add pluto export and import database functionality (#25) --- src/index.ts | 12 +++++++++++- tests/pluto.test.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 84a46953..08adb140 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { RxCollection, RxDatabase, RxDatabaseCreator, + RxDumpDatabase, createRxDatabase, } from "rxdb"; import { RxError } from "rxdb"; @@ -13,7 +14,7 @@ import { addRxPlugin } from "rxdb"; import { RxDBMigrationPlugin } from "rxdb/plugins/migration"; import { RxDBQueryBuilderPlugin } from "rxdb/plugins/query-builder"; import { v4 as uuidv4 } from "uuid"; - +import { RxDBJsonDumpPlugin } from "rxdb/plugins/json-dump"; import MessageSchema, { MessageColletion, MessageMethods, @@ -42,6 +43,7 @@ import { RxDBDevModePlugin } from "rxdb/plugins/dev-mode"; addRxPlugin(RxDBMigrationPlugin); addRxPlugin(RxDBQueryBuilderPlugin); //addRxPlugin(RxDBDevModePlugin); +addRxPlugin(RxDBJsonDumpPlugin); export * from "./schemas/Message"; export * from "./schemas/DID"; @@ -77,6 +79,14 @@ export class Database implements Domain.Pluto { constructor(private dbOptions: RxDatabaseCreator) {} + async backup() { + return this.db.exportJSON(); + } + + async import(importData: RxDumpDatabase) { + await this.db.importJSON(importData); + } + static async createEncrypted(name: string, encryptionKey: Uint8Array) { return new Database({ ignoreDuplicate: true, diff --git a/tests/pluto.test.ts b/tests/pluto.test.ts index 1af5f5c0..53d9ecbc 100644 --- a/tests/pluto.test.ts +++ b/tests/pluto.test.ts @@ -432,6 +432,25 @@ describe("Pluto + Dexie encrypted integration for browsers", () => { expect((await db.getAllMediators()).length).toBe(1); }); + it("Should go a backup of all the database and restore it", async () => { + const host = Domain.DID.fromString("did:prism:333333"); + const mediator = Domain.DID.fromString("did:prism:444444"); + const routing = Domain.DID.fromString("did:prism:555555"); + expect((await db.getAllMediators()).length).toBe(0); + await db.storeMediator(mediator, host, routing); + expect((await db.getAllMediators()).length).toBe(1); + const backup = await db.backup(); + + const restored = await Database.createEncrypted( + `${databaseName}${randomUUID()}`, + defaultPassword + ); + await restored.start(); + await restored.import(backup); + + expect((await restored.getAllMediators()).length).toBe(1); + }); + it("Should throw an error when an incomplete did is loaded from db", async () => { const did = Domain.DID.fromString("did:prism:65432133");