Skip to content

Commit

Permalink
Do not update parentCollection if the child object has not been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
vparpoil committed Mar 9, 2020
1 parent 3f40ee8 commit d8b77db
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ Mongo.Collection.prototype.cache = function(options){
childCollection.after.update(function(userId, child, changedFields){
if(_.intersection(changedFields, topFields).length){
let pickedChild = _.pick(child, childFields)
parentCollection.update({[referenceField]:child._id}, {$set:{[cacheField]:pickedChild}}, {multi:true})
// test if the object has been modified
let previousPickedChild = _.pick(this.previous, childFields);
if (!_.isEqual(previousPickedChild, pickedChild)){
parentCollection.update({[referenceField]:child._id}, {$set:{[cacheField]:pickedChild}}, {multi:true})
}
}
})

Expand Down

0 comments on commit d8b77db

Please sign in to comment.