-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Remove hook not triggered when removing documents #1241
Comments
this works as expected. its the same thing for I'll leaving this open for now until I fix the |
I see. Is there any way to hook into all remove operations? |
not presently. |
Ok. Thank you! |
well you cooould if you monkey patched |
Not working with findOneAndRemove |
Also doesn't seem to work when you call Model.remove(_id).exec(); |
This is how I got around the issue:
The remove hook is not fired on findOneAndRemove, however, it works when I call remove in the callback |
Ok but Why ? iam sorry i didnt get why the hook is executed on Model.remove() and not on Query.remove(). Now why should i use the hooks if they arent called everytime ? |
I mean : Should i use the hooks and avoid Query.remove or not use the hooks and always remove manually ? |
@Darksheep42 if you want to use hooks, use |
Would it be possible to add support for |
Not right now - that's very tricky because there's already middleware for |
Would there be any issue other than the name conflict? Couldn't you just call the hook 'remove-query' or something instead of 'remove'? |
Or how about do what you seem to do with update - call the query hook even when the doc version of the function is used. Of course this would break backwards-compatibility, but it's an idea. |
Seems like a reasonable idea, but I'm hesitant to break API unless there's a really good reason to. Right now the remove hook operates on a document, so the context in the middleware is the document. For general schema.pre('remove', { query: true }, function() {
// This is only attached as query middleware
});
schema.pre('remove', { doc: true }, function() {
// This will not run on Query.remove(), only doc.remove()
}); |
I suppose that could work. It's probably better than just using a different name and it doesn't break the API. So I guess if someone did schema.pre('remove', { query: true, doc: true }, function() {
}); The hook would be called on both query and doc requests. I guess a user could do this if they didn't care about the context. If you do this, though, you'd probably want to also change the |
Yeah the tricky bit in that context is "what's the default?" In order to preserve API, we'd have to make it so that update is a query middleware by default, but remove is a document middleware by default. That will be very weird. I think we should just open up a separate issue for 5.0 and worry about it later :) |
How about, just for the next version or some version in the near future, we can just call the hook 'remove-query' or something? It doesn't break the API and AFAIK should be pretty easy to implement. We could really use this feature, as right now you can't capture 100% of remove requests via middleware and we'd much rather do this than enforce certain techniques in our codebase. In 5.0 we can do it the "right way" but I'd love to just see this quick addition before v5. |
Alternative approach: how about we just make a |
So instead of writing schema.pre('remove', function() {
// What is this? A doc hook? A query hook? Idk man
}); you'd write schema.preQuery('remove', function() {
// This is definitely a query hook
});
schema.pre('remove', function() {
// This must be a doc hook
}); So would |
Yeah |
This issue is still open, is it fixed ? |
The issue of pre remove for documents vs queries is tracked in #3054, not implemented yet, PRs welcome. |
The pre/post hooks for
remove
are only triggered when using the document instance'sremove()
method, not when the model's methods are used:The text was updated successfully, but these errors were encountered: