Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support findOne and findOneAndUpdate hooks #20

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added findOne
MahdadGhasemian committed Dec 26, 2023
commit 9a3e0f71bffcb87ca9564acd6c5695ba3d00b873
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "soft-delete-plugin-mongoose",
"name": "@mahdad/soft-delete-plugin-mongoose",
"version": "1.0.15",
"description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)",
"main": "dist/index.js",
11 changes: 11 additions & 0 deletions src/soft-delete-plugin.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,17 @@ export const softDeletePlugin = (schema: mongoose.Schema) => {
},
});

// @ts-ignore
schema.pre('findOne',
async function (this, next: (err?: CallbackError) => void) {
if (this.getFilter().isDeleted === true) {
return next();
}
this.setQuery({ ...this.getFilter(), isDeleted: { $ne: true } });
next();
},
);

// @ts-ignore
schema.pre('find',
async function (this, next: (err?: CallbackError) => void) {