Skip to content

Commit

Permalink
feat(wallet): add DocumentStore.delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Jan 17, 2025
1 parent a262f20 commit 037258e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ export class InMemoryDocumentStore<T> extends InMemoryStore implements DocumentS
}
return EMPTY;
}

delete(): Observable<void> {
if (this.destroyed) return EMPTY;
this.#doc = null;
return of(void 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentStore } from '../types';
import { EMPTY, Observable } from 'rxjs';
import { EMPTY, Observable, from } from 'rxjs';
import { Logger } from 'ts-log';
import { PouchDbStore } from './PouchDbStore';
import { sanitizePouchDbDoc } from './util';
Expand Down Expand Up @@ -34,4 +34,18 @@ export class PouchDbDocumentStore<T extends {}> extends PouchDbStore<T> implemen
set(doc: T): Observable<void> {
return this.forcePut(this.#docId, doc);
}

delete(): Observable<void> {
if (this.destroyed) return EMPTY;
return from(
(async () => {
const _rev = await this.getRev(this.#docId);
if (!_rev) {
// assuming already deleted
return;
}
await this.db.remove({ _id: this.#docId, _rev });
})()
);
}
}
4 changes: 2 additions & 2 deletions packages/wallet/src/persistence/pouchDbStores/PouchDbStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export abstract class PouchDbStore<T extends {}> {
return toPouchDbDoc(obj) as T;
}

async #getRev(docId: string) {
protected async getRev(docId: string) {
const existingDoc = await this.db.get(docId).catch(() => void 0);
return existingDoc?._rev;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export abstract class PouchDbStore<T extends {}> {
.then(async () => {
const pouchDbDoc = {
_id: docId,
_rev: await this.#getRev(docId),
_rev: await this.getRev(docId),
...serializableDoc
};
try {
Expand Down
20 changes: 19 additions & 1 deletion packages/wallet/test/persistence/pouchDbStores.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/* eslint-disable unicorn/consistent-function-scoping */
import { PouchDbCollectionStore, PouchDbDocumentStore, PouchDbKeyValueStore } from '../../src/persistence';
import { assertCompletesWithoutEmitting } from './util';
import { combineLatest, firstValueFrom, mergeMap, share, shareReplay, take, timer, toArray } from 'rxjs';
import {
combineLatest,
defaultIfEmpty,
firstValueFrom,
mergeMap,
share,
shareReplay,
take,
timer,
toArray
} from 'rxjs';
import { dummyLogger as logger } from 'ts-log';
import PouchDB from 'pouchdb';

Expand Down Expand Up @@ -34,6 +44,14 @@ describe('pouchDbStores', () => {
expect(await firstValueFrom(store1.get())).toEqual(doc2);
});

it('delete removes existing document if exists', async () => {
const store1 = new PouchDbDocumentStore<DocType>(dbName, 'docId', logger);
await firstValueFrom(store1.set(doc1));
await firstValueFrom(store1.delete());
await expect(firstValueFrom(store1.get().pipe(defaultIfEmpty(null)))).resolves.toBeNull();
await expect(firstValueFrom(store1.delete())).resolves.not.toThrowError();
});

it('simultaneous set() calls are resolved in series - last value is always persisted', async () => {
const store = new PouchDbDocumentStore<DocType>(dbName, 'docId', logger);
await firstValueFrom(combineLatest([store.set(doc1), timer(1).pipe(mergeMap(() => store.set(doc2)))]));
Expand Down

0 comments on commit 037258e

Please sign in to comment.