From 9e3c8196f1f77e5f9c6cf8c2b8d49ec3a0cf947a Mon Sep 17 00:00:00 2001 From: Florian Bienefelt Date: Fri, 10 Dec 2021 15:16:54 +0100 Subject: [PATCH] Allow passing update options with caches --- cache.js | 61 ++++++++++++++++++++++++++------------------------- cacheCount.js | 8 ++++--- cacheField.js | 8 ++++--- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/cache.js b/cache.js index 0fbb581..eabcdbc 100644 --- a/cache.js +++ b/cache.js @@ -1,4 +1,4 @@ -import _ from 'lodash' +import _, { update } from 'lodash' import {addMigration, migrate, autoMigrate} from './migrations.js' export {migrate, autoMigrate} @@ -35,6 +35,7 @@ Mongo.Collection.prototype.cache = function(options){ let referenceField = options.referenceField let cacheField = options.cacheField let watchedFields = options.fields + let updateOptions = options.updateOptions if(referenceField.split(/[.:]/)[0] == cacheField.split(/[.:]/)[0]){ throw new Error('referenceField and cacheField must not share the same top field') @@ -86,7 +87,7 @@ Mongo.Collection.prototype.cache = function(options){ if(_.get(parent, referenceField)){ let child = childCollection.findOne(_.get(parent, referenceField), childOpts) if(child){ - parentCollection.update(parent._id, {$set:{[cacheField]:child}}) + parentCollection.update(parent._id, {$set:{[cacheField]:child}}, updateOptions) } } } @@ -97,27 +98,27 @@ Mongo.Collection.prototype.cache = function(options){ if(_.includes(changedFields, referenceField.split('.')[0])){ let child = _.get(parent, referenceField) && childCollection.findOne(_.get(parent, referenceField), childOpts) if(child){ - parentCollection.update(parent._id, {$set:{[cacheField]:child}}) + parentCollection.update(parent._id, {$set:{[cacheField]:child}}, updateOptions) } else { - parentCollection.update(parent._id, {$unset:{[cacheField]:1}}) + parentCollection.update(parent._id, {$unset:{[cacheField]:1}}, updateOptions) } } }) childCollection.after.insert(function(userId, child){ let pickedChild = _.pick(child, childFields) - parentCollection.update({[referenceField]:child._id}, {$set:{[cacheField]:pickedChild}}, {multi:true}) + parentCollection.update({[referenceField]:child._id}, {$set:{[cacheField]:pickedChild}}, {...updateOptions, multi:true}) }) 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}) + parentCollection.update({[referenceField]:child._id}, {$set:{[cacheField]:pickedChild}}, {...updateOptions,multi:true}) } }) childCollection.after.remove(function(userId, child){ - parentCollection.update({[referenceField]:child._id}, {$unset:{[cacheField]:1}}, {multi:true}) + parentCollection.update({[referenceField]:child._id}, {$unset:{[cacheField]:1}}, {...updateOptions,multi:true}) }) } @@ -126,9 +127,9 @@ Mongo.Collection.prototype.cache = function(options){ let references = getNestedReferences(parent) if(references.length){ let children = childCollection.find({_id:{$in:references}}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } else { - parentCollection.update(parent._id, {$set:{[cacheField]:[]}}) + parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, updateOptions) } } addMigration(parentCollection, insert, options) @@ -139,16 +140,16 @@ Mongo.Collection.prototype.cache = function(options){ let references = getNestedReferences(parent) if(references.length){ let children = childCollection.find({_id:{$in:references}}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } else { - parentCollection.update(parent._id, {$set:{[cacheField]:[]}}) + parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, updateOptions) } } }) childCollection.after.insert(function(userId, child){ let pickedChild = _.pick(child, childFields) - parentCollection.update({[referencePath]:child._id}, {$push:{[cacheField]:pickedChild}}, {multi:true}) + parentCollection.update({[referencePath]:child._id}, {$push:{[cacheField]:pickedChild}}, {...updateOptions,multi:true}) }) childCollection.after.update(function(userId, child, changedFields){ @@ -157,16 +158,16 @@ Mongo.Collection.prototype.cache = function(options){ parentCollection.find({[referencePath]:child._id}, parentOpts).forEach(parent => { let index = _.findIndex(_.get(parent, cacheField), {_id:child._id}) if(index > -1){ - parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}) + parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}, updateOptions) } else { - parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}) + parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, updateOptions) } }) } }) childCollection.after.remove(function(userId, child){ - parentCollection.update({[referencePath]:child._id}, {$pull:{[cacheField]:{_id:child._id}}}, {multi:true}) + parentCollection.update({[referencePath]:child._id}, {$pull:{[cacheField]:{_id:child._id}}}, {...updateOptions,multi:true}) }) } @@ -174,7 +175,7 @@ Mongo.Collection.prototype.cache = function(options){ else if(type == 'inversed'){ let insert = function insert(userId, parent){ let children = childCollection.find({[referenceField]:parent._id}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } addMigration(parentCollection, insert, options) @@ -184,9 +185,9 @@ Mongo.Collection.prototype.cache = function(options){ if(_.includes(changedFields, referenceField.split('.')[0])){ if(_.get(parent, referenceField)){ let children = childCollection.find({[referenceField]:parent._id}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } else { - parentCollection.update(parent._id, {$set:{[cacheField]:[]}}) + parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, updateOptions) } } }) @@ -194,7 +195,7 @@ Mongo.Collection.prototype.cache = function(options){ childCollection.after.insert(function(userId, child){ let pickedChild = _.pick(child, childFields) if(_.get(child, referenceField)){ - parentCollection.update({_id:_.get(child, referenceField)}, {$push:{[cacheField]:pickedChild}}) + parentCollection.update({_id:_.get(child, referenceField)}, {$push:{[cacheField]:pickedChild}}, updateOptions) } }) @@ -203,28 +204,28 @@ Mongo.Collection.prototype.cache = function(options){ let pickedChild = _.pick(child, childFields) let previousId = this.previous && _.get(this.previous, referenceField) if(previousId && previousId !== _.get(child, referenceField)){ - parentCollection.update({_id:previousId}, {$pull:{[cacheField]:{_id:child._id}}}) + parentCollection.update({_id:previousId}, {$pull:{[cacheField]:{_id:child._id}}}, updateOptions) } parentCollection.find({_id:_.get(child, referenceField)}, parentOpts).forEach(parent => { let index = _.findIndex(_.get(parent, cacheField), {_id:child._id}) if(index > -1){ - parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}) + parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}, updateOptions) } else { - parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}) + parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, updateOptions) } }) } }) childCollection.after.remove(function(userId, child){ - parentCollection.update({_id:_.get(child, referenceField)}, {$pull:{[cacheField]:{_id:child._id}}}) + parentCollection.update({_id:_.get(child, referenceField)}, {$pull:{[cacheField]:{_id:child._id}}}, updateOptions) }) } else if(type == 'many-inversed'){ let insert = function insert(userId, parent){ let children = childCollection.find({[referencePath]:parent._id}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } addMigration(parentCollection, insert, options) @@ -233,7 +234,7 @@ Mongo.Collection.prototype.cache = function(options){ parentCollection.after.update(function(userId, parent, changedFields){ if(_.includes(changedFields, referencePath.split('.')[0])){ let children = childCollection.find({[referencePath]:parent._id}, childOpts).fetch() - parentCollection.update(parent._id, {$set:{[cacheField]:children}}) + parentCollection.update(parent._id, {$set:{[cacheField]:children}}, updateOptions) } }) @@ -241,7 +242,7 @@ Mongo.Collection.prototype.cache = function(options){ let references = getNestedReferences(child) if(references.length){ let pickedChild = _.pick(child, childFields) - parentCollection.update({_id:{$in:references}}, {$push:{[cacheField]:pickedChild}}, {multi:true}) + parentCollection.update({_id:{$in:references}}, {$push:{[cacheField]:pickedChild}}, {...updateOptions,multi:true}) } }) @@ -251,16 +252,16 @@ Mongo.Collection.prototype.cache = function(options){ let previousIds = this.previous && getNestedReferences(this.previous) previousIds = _.difference(previousIds, references) if(previousIds.length){ - parentCollection.update({_id:{$in:previousIds}}, {$pull:{[cacheField]:{_id:child._id}}}, {multi:true}) + parentCollection.update({_id:{$in:previousIds}}, {$pull:{[cacheField]:{_id:child._id}}}, {...updateOptions,multi:true}) } if(references.length){ let pickedChild = _.pick(child, childFields) parentCollection.find({_id:{$in:references}}, parentOpts).forEach(parent => { let index = _.findIndex(_.get(parent, cacheField), {_id:child._id}) if(index > -1){ - parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}) + parentCollection.update(parent._id, {$set:{[cacheField + '.' + index]:pickedChild}}, updateOptions) } else { - parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}) + parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, updateOptions) } }) } @@ -270,7 +271,7 @@ Mongo.Collection.prototype.cache = function(options){ childCollection.after.remove(function(userId, child){ let references = getNestedReferences(child) if(references.length){ - parentCollection.update({_id:{$in:references}}, {$pull:{[cacheField]:{_id:child._id}}}, {multi:true}) + parentCollection.update({_id:{$in:references}}, {$pull:{[cacheField]:{_id:child._id}}}, {...updateOptions,multi:true}) } }) } diff --git a/cacheCount.js b/cacheCount.js index 4a26557..eb6c403 100644 --- a/cacheCount.js +++ b/cacheCount.js @@ -7,7 +7,8 @@ Mongo.Collection.prototype.cacheCount = function(options) { cacheField:String, referenceField:String, selector:Match.Optional(Object), - bypassSchema:Match.Optional(Boolean) + bypassSchema:Match.Optional(Boolean), + updateOptions: Match.Optional(Object) }) let parentCollection = options.bypassSchema && Package['aldeed:collection2'] ? this._collection : this @@ -15,6 +16,7 @@ Mongo.Collection.prototype.cacheCount = function(options) { let selector = options.selector || {} let cacheField = options.cacheField let referenceField = options.referenceField + let updateOptions = options.updateOptions let watchedFields = _.union([referenceField], _.keys(selector)) if(referenceField.split(/[.:]/)[0] == cacheField.split(/[.:]/)[0]){ @@ -25,13 +27,13 @@ Mongo.Collection.prototype.cacheCount = function(options) { let ref = _.get(child, referenceField) if(ref){ let select = _.merge(selector, {[referenceField]:ref}) - parentCollection.update({_id:ref}, {$set:{[cacheField]:childCollection.find(select).count()}}) + parentCollection.update({_id:ref}, {$set:{[cacheField]:childCollection.find(select).count()}}, updateOptions) } } function insert(userId, parent){ let select = _.merge(selector, {[referenceField]:parent._id}) - parentCollection.update(parent._id, {$set:{[cacheField]:childCollection.find(select).count()}}) + parentCollection.update(parent._id, {$set:{[cacheField]:childCollection.find(select).count()}}, updateOptions) } addMigration(parentCollection, insert, options) diff --git a/cacheField.js b/cacheField.js index 610babb..46c9d6c 100644 --- a/cacheField.js +++ b/cacheField.js @@ -7,7 +7,8 @@ Mongo.Collection.prototype.cacheField = function(options) { cacheField:String, fields:[String], transform:Match.Optional(Function), - bypassSchema:Match.Optional(Boolean) + bypassSchema:Match.Optional(Boolean), + updateOptions: Match.Optional(Object) }) let collection = options.bypassSchema && Package['aldeed:collection2'] ? this._collection : this @@ -15,6 +16,7 @@ Mongo.Collection.prototype.cacheField = function(options) { let fields = options.fields let topFields = _.uniq(_.map(fields, field => field.split('.')[0])) let transform = options.transform + let updateOptions = options.updateOptions if(!transform) { transform = function(doc) { return _.compact(_.map(fields, field => _.get(doc, field))).join(', ') @@ -26,7 +28,7 @@ Mongo.Collection.prototype.cacheField = function(options) { } function insertHook(userid, doc){ - collection.update(doc._id, {$set:{[cacheField]:transform(_.pick(doc, fields))}}) + collection.update(doc._id, {$set:{[cacheField]:transform(_.pick(doc, fields))}}, updateOptions) } addMigration(collection, insertHook, options) @@ -36,7 +38,7 @@ Mongo.Collection.prototype.cacheField = function(options) { collection.after.update((userId, doc, changedFields) => { if(_.intersection(changedFields, topFields).length){ Meteor.defer(()=>{ - collection.update(doc._id, {$set:{[cacheField]:transform(_.pick(doc, fields))}}) + collection.update(doc._id, {$set:{[cacheField]:transform(_.pick(doc, fields))}}, updateOptions) }) } })