meteor-collection-hooks
meteor-collection-hooks copied to clipboard
FR: Ability to add a hook to all collections
Something like
CollectionHooks.after.remove: function (userId, doc) {
// ...
});
that runs after a doc is removed from any collection. Use case:
http://stackoverflow.com/questions/29638910/how-can-you-log-all-mongo-write-commands
I like it. I'll keep this open and I think this will eventually make it in when I have some time
Nice, thanks! See second answer for how to get a list of all collections:
http://stackoverflow.com/questions/10984030/get-meteor-collection-by-name
That second answer is not going to work consistently... A collection can be instantiated anywhere, not just the global scope... That's a poor approach...
But anyway, it's irrelevant as we just leverage the prototype for this stuff
can i do this with default options ?
CollectionHooks.defaults.before.insert = {createdAt: new Date()};
CollectionHooks.before.insert (userId, doc) ->
_.extend doc,
createdAt: new Date()
+1
Any plans of implementing this?
Thanks for making the package public!
[UPDATE] I've implemented this using a superclass on my Collections. Any thoughts on how this might be a good/bad idea while we wait for this feature?
To be honest I don't believe that the approach this package takes, is a good one any longer.
Subclassing Mongo.Collection would be "better", but even better in my world would be to have a functional api like the following.
import addHook from "meteor/zimme:collection-hooks";
const handle = addHook("after", "insert", Posts, function() { console.log("Runs after insert"); });
// Posts now have an after insert callback
handle.remove()
// Posts doesn't have an after insert callback
This way you don't add needless utility functions to Mongo.Collection.
More like the api I've used in https://github.com/meteor-collectionbehaviours/timestamp
I do believe the current api was chosen in the hopes of it getting merged into core some day.