Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing update options with caches #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash'
import _, { update } from 'lodash'
import {addMigration, migrate, autoMigrate} from './migrations.js'

export {migrate, autoMigrate}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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})
})
}

Expand All @@ -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)
Expand All @@ -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){
Expand All @@ -157,24 +158,24 @@ 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})
})
}


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)

Expand All @@ -184,17 +185,17 @@ 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)
}
}
})

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)
}
})

Expand All @@ -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)

Expand All @@ -233,15 +234,15 @@ 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)
}
})

childCollection.after.insert(function(userId, child){
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})
}
})

Expand All @@ -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)
}
})
}
Expand All @@ -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})
}
})
}
Expand Down
8 changes: 5 additions & 3 deletions cacheCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ 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
let childCollection = options.collection
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]){
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions cacheField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ 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
let cacheField = options.cacheField
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(', ')
Expand All @@ -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)
Expand All @@ -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)
})
}
})
Expand Down