Skip to content

Commit

Permalink
feat: add pluto export and import database functionality (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo authored Oct 28, 2023
1 parent db61623 commit 2eb624d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
RxCollection,
RxDatabase,
RxDatabaseCreator,
RxDumpDatabase,
createRxDatabase,
} from "rxdb";
import { RxError } from "rxdb";
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,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -77,6 +79,14 @@ export class Database implements Domain.Pluto {

constructor(private dbOptions: RxDatabaseCreator) {}

async backup() {
return this.db.exportJSON();
}

async import(importData: RxDumpDatabase<PlutoCollections>) {
await this.db.importJSON(importData);
}

static async createEncrypted(name: string, encryptionKey: Uint8Array) {
return new Database({
ignoreDuplicate: true,
Expand Down
19 changes: 19 additions & 0 deletions tests/pluto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 2eb624d

Please sign in to comment.