Meteor-Community-Packages / meteor-collection-hooks

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

Generate document that would result from the update in "before.update" #53

Closed gwendall closed 10 years ago

gwendall commented 10 years ago

I would like to perform operations depending on an embedded array (in this specific case, to calculate counts, averages, etc), which is getting changed through $addToSet.

Isn't there a magical function that would take (doc, modifier) as parameters and generate the final document pre-update (ie before the update is actually done)?

mizzao commented 10 years ago

Even if such a magical function existed (it does in Minimongo), this would require two database operations where we previously used just one - first to fetch the document, second to actually update it. Atomicity guarantees are not preserved across these operations, and the performance hit would not be worth adding this feature.

Why don't you just put this logic in your app? I don't think it belongs in this package.

matb33 commented 10 years ago

I'd end up having another hook that checks if the fieldname modified by $addToSet was changed, and if it did, run my calcs. In fact I do this for some analytics in one of my apps. I don't really see any issue with it... it keeps the responsibilities separate so I could potentially have this analytic calculation queued and done at a later time on another server (if it came to that)