Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Being able to modify selector for remove hooks #23

Closed mizzao closed 10 years ago

mizzao commented 10 years ago

The current API for the remove function seems to be missing a couple of use cases.

Would it be possible to modify the selector if a global remove (i.e., not by _id) was being done on the server side? Also in that case, what would be passed in for doc?

Additionally, not having access to the selector for the before.remove hook makes it impossible to implement a direct remove (for testing purposes.) It's really quite nasty how the direct find and direct insert were implemented too, so I would love to have some alternatives.

mizzao commented 10 years ago

I think the new args field you added in the context may be helpful for dealing with general issues like this as well.

matb33 commented 10 years ago

With the change I just pushed to allow modifying args via this.args, you could do this.args[0] = {}; in the before hook. Untested but should work!

mizzao commented 10 years ago

@matb33 I like what you did with calling the remove hook once for each document, but it doesn't cover something else that I want to do.

I have some behavior that actually modifies the selector for the remove hook before running it; and this changes the documents that will be removed (selecting a more specific set). With the current code that makes a copy of everything in the original selector, it gets quite cumbersome.

It seems like it would make sense to have two remove hooks, one for the function being called and one for each document. Would this be possible or do you see some other way to do it?

mizzao commented 10 years ago

Never mind - just discovered that the find hooks are called for remove as well, and, then the _ids are applied to the remove hook to each document. So one need only modify the find hooks for the right behavior. That's quite ingenious of you, I've got to say.