For an all-in-one solution combining the open_revs
tip from this answer, here's the TypeScript code I came up with:
const db = new PouchDB('my-db');
async function deletedDocIds(): Promise<string[]> {
const ret: string[] = [];
return new Promise((resolve, reject) => {
db.changes({filter: d => d._deleted})
.on('change', c => ret.push(c.id))
.on('complete', () => resolve(ret))
.on('error', e => reject(e));
});
}
async function deletedDocs() {
const ids = await deletedDocIds();
return Promise.all(ids.map(id => db.get(id, {revs: true, open_revs: 'all'}).then(x => {
const revs = (x[0].ok as any)._revisions;
const lastRev = (revs.start - 1) + '-' + revs.ids[1];
return db.get(id, {rev: lastRev}); // with Pouchdb keys too
})));
}
Calling deletedDocs()
will return a promise of an array of all deleted docs as per the rev just prior to deletion.
N.B., the elements of the array will include PouchDb metadata as well as your document's keys.
N.B. 2, version 6.1.3 of DefinitelyTyped's TypeScript bindings for pouchdb-browser which I'm using here (should work for @types/pouchdb
too though) doesn't seem to know about the _revisions
key, hence the as any
escape hatch.
N.B. 3, this should be trivial to manually translate to plain JS, just delete the type declarations and coercions (:
, as
, and whatever token follows these).