Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async ({ deleted, doc }) => {
const index = docs?.findIndex(({ _id }) => doc._id === _id);
const found = index !== -1;
// Document was deleted or it does not match the selector?
if (deleted || (selector && !matchesSelector(doc, selector))) {
if (found) {
// Remove.
docs.splice(index, 1);
const { length } = docs;
// At the limit?
if (length + 1 === limit) {
const {
docs: [replacementDoc]
} = await db.find({
...options,
limit: 1,
skip: (options.skip || 0) + length
});
if (replacementDoc) {
docs.push(replacementDoc);
}