Skip to content

Commit

Permalink
feat(purge): handle missing doc, return something
Browse files Browse the repository at this point in the history
  • Loading branch information
espy authored and AlbaHerrerias committed Dec 7, 2022
1 parent a8b5e00 commit 693ea5c
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/node_modules/pouchdb-core/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,33 +966,41 @@ AbstractPouchDB.prototype.purge = adapterFun('_purge', function (docId, rev) {
}
var self = this;

// TODO: this should probably return some information
// about the completed purge
return new Promise((resolve) => {
self._getRevisionTree(docId, async (whatever, revs) => {
if (!revs) {
throw new Error('The target document doesn’t exist (has no revisions)');
}
// `findPathToLeaf` throws if rev is invalid
const path = findPathToLeaf(revs, rev);
console.log("purge path", path);

await (async () =>
new Promise((resolve) =>
self._purge(docId, path, (err) => {
if (err) {
console.error("error", err);
} else {
console.log("purge successful");
}
resolve();
})
))();

await (async () =>
new Promise((resolve) =>
// TODO: The purge itself returns nothing, is that ok?
// CouchDB returns the successfully purged revisions
self._purge(docId, path, (err) => {
if (err) {
console.error("error", err);
} else {
console.log("purge successful");
}
resolve();
})
))();
let result;
try {
// If the doc had conflicts and still exists, return it
result = await self.get(docId);
console.log(
"revs_info after purge",
await self.get(docId, { revs_info: true })
);
} catch (err) {}

resolve();
} catch (err) {
// If the doc was conflict-free, it is now gone. Return the `not_found` error,
// as this is the expected happy path result of a `get()` on a purged doc
result = err;
}
resolve(result);
});
});
});
Expand Down

0 comments on commit 693ea5c

Please sign in to comment.