Meteor-Community-Packages / meteor-collection-hooks

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

How do we detect insert vs update in .before.upsert? #169

Closed trusktr closed 8 years ago

trusktr commented 8 years ago

?

ghost commented 8 years ago

How do we do anything in before.upsert? There's no doc argument to validate individual docs, like with the other hooks. For example, how do we do the upsert in the following?

    collection.before.insert((userId, doc) => {
      checkAgainstSchema(doc, schema)
    })
    collection.before.update((userId, doc, fieldNames, modifier, options) => {
      checkAgainstSchema(doc, schema)
    })
    collection.before.upsert(function(userId, selector, modifier, options) {
        // ???
    })
ghost commented 8 years ago

@matb33

matb33 commented 8 years ago

Can you accomplish what you need with allow/deny? http://docs.meteor.com/#/basic/Mongo-Collection-allow

ghost commented 8 years ago

I could, but I think the hooks are a nicer pattern for this, in general. I found a solution: use LocalCollection from minimongo. We can get the documents (if any) using the selector, then transform them to their new state with LocalCollection._modify, and finally check the modified documents before they are actually sent to the DB.