• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / atala-prism-wallet-sdk-ts / 9416468997

07 Jun 2024 11:46AM UTC coverage: 69.3% (+1.2%) from 68.144%
9416468997

Pull #215

github

web-flow
Merge ff72c3dfc into 78838eaef
Pull Request #215: feat: Backup and Restore

1087 of 1756 branches covered (61.9%)

Branch coverage included in aggregate %.

218 of 264 new or added lines in 29 files covered. (82.58%)

3 existing lines in 2 files now uncovered.

2290 of 3117 relevant lines covered (73.47%)

19.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/pluto/rxdb/Store.ts
1
import { CollectionsOfDatabase, MangoQuery, RxDatabase, RxDatabaseCreator, RxDocument, addRxPlugin, createRxDatabase, removeRxDatabase } from 'rxdb';
2
import { RxDBJsonDumpPlugin } from 'rxdb/plugins/json-dump';
3
import { RxDBQueryBuilderPlugin } from 'rxdb/plugins/query-builder';
4
import { CollectionList, makeCollections } from "./collections";
5
import type { Pluto } from "../Pluto";
6
import { Model } from '../models';
7
import { RxDBEncryptedMigrationPlugin } from '../migration';
8
import { Domain } from '../..';
9
import { RxDBUpdatePlugin } from 'rxdb/plugins/update';
10

11
export class RxdbStore implements Pluto.Store {
12
  private _db?: RxDatabase<CollectionsOfDatabase, any, any>;
13

14
  constructor(
15
    private readonly options: RxDatabaseCreator,
16
    private readonly collections?: CollectionList
17
  ) {
18
    addRxPlugin(RxDBQueryBuilderPlugin);
99✔
19
    addRxPlugin(RxDBJsonDumpPlugin);
99✔
20
    addRxPlugin(RxDBEncryptedMigrationPlugin);
99✔
21
    addRxPlugin(RxDBUpdatePlugin);
99✔
22
  }
23

24

25
  get db() {
26
    if (!this._db) {
1,195!
27
      throw new Error('Start Pluto first.');
×
28
    }
29

30
    return this._db;
1,195✔
31
  }
32

33
  /**
34
   * Start the database and build collections
35
   */
36
  async start(): Promise<void> {
37
    if (!this._db) {
99!
38
      this._db = await createRxDatabase({
99✔
39
        ...this.options,
40
        multiInstance: true
41
      });
42
      const collections = makeCollections(this.collections ?? {});
99✔
43
      await this._db.addCollections(collections);
99✔
44
    }
45
  }
46

47
  async update<T extends Domain.Pluto.Storable>(name: string, model: T): Promise<void> {
48
    const table = this.getCollection(name);
1✔
49
    const row = await table.findOne({
1✔
50
      selector: {
51
        uuid: model.uuid
52
      }
53
    }).exec();
54
    if (row) {
1!
55

56
      //Improve error handling when not found
57
      await row.patch(model);
1✔
58
    }
59
  }
60

61
  async delete(name: string, uuid: string): Promise<void> {
62
    const table = this.getCollection(name);
×
63
    const row = await table.findOne({
×
64
      selector: {
65
        uuid: uuid
66
      }
67
    });
68
    //TODO: Improve error handling, specially when not found
69
    await row?.remove();
×
70
  }
71

72

73
  getCollection(name: string) {
74
    const safeName = name.replace(/([A-Z])/g, "-$1").toLowerCase();
597✔
75
    if (!this.db.collections[safeName]) {
597!
76
      throw new Error("Collection does not exist");
×
77
    }
78

79
    return this.db.collections[safeName];
597✔
80
  }
81

82
  async query<T extends Model>(name: string, query?: MangoQuery<T>) {
83
    const collection = this.getCollection(name);
361✔
84
    const rxQuery = collection.find(query);
361✔
85
    const result: RxDocument[] = await rxQuery.exec();
361✔
86
    const models = result.map(x => x._data);
361✔
87
    return models as any;
361✔
88
  }
89

90
  async insert(name: string, data: any) {
91
    const table = this.getCollection(name);
235✔
92
    await table.insert(data);
235✔
93
  }
94

95
  /**
96
   * Use with caution, this will remove all entries from database
97
   */
98
  async cleanup() {
99
    const storages = Array.from(this.db.storageInstances.values());
1✔
100

101
    for (const storage of storages) {
1✔
102
      await storage.cleanup(Infinity);
8✔
103
    }
104
  }
105

106
  /**
107
   * Use with caution, this will remove all entries from database
108
   * and then destroy the database itself.
109
   */
110
  async clear() {
NEW
111
    await this.cleanup();
×
UNCOV
112
    await removeRxDatabase(this.options.name, this.db.storage);
×
113
  }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc