Meteor-Community-Packages / meteor-collection-hooks

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

"Find" hook overwriting "update" hook. #184

Closed drewmoore closed 8 years ago

drewmoore commented 8 years ago

I have found that when assigning both "find" and "update" hooks for a collection that update is always overwritten by the find hook. It seems to only be a problem with update. Other methods are unaffected. I'm also using collection2, autoform, and simpleSchema if that helps at all. Here's an actual code snippet:

    Issues.after.find(function (userId, selector, options, cursor) {
      if (typeof selector === 'string') { selector = { _id: selector }; }
      Interactions.insert({ userId: userId, collectionName: Issues._name,
                            action: 'read', selector: selector,
                            resultCount: cursor.count() }); 
    });

    Issues.after.update(function (userId, doc) {
      Interactions.insert({ userId: userId, collectionName: Issues._name,
                            action: 'update', docId: doc._id }); 
    }, { fetchPrevious: false }); 
mizzao commented 8 years ago

This is because of how the update hook works: it first grabs all relevant documents with the find hook then updates them one by one. This is the only way it can be called for all updated documents.

I don't think there's a way to get around this behavior, except making your find hook a little smarter. I actually take advantage of it in https://github.com/mizzao/meteor-partitioner.

drewmoore commented 8 years ago

Yeah, I ended up writing my own implementation of something similar to this package just for customizing. Thanks, though, this package has some cool stuff going on.