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

Deprecate bypassSchema param, introduce updateOptions #19

Open
wants to merge 2 commits 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
7 changes: 4 additions & 3 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Posts.cache({
<td>The field on the parent where children are cached. Can be a nested field, like <code>'caches.field'</code>, but it can not be in the same top level field as the referenceField. For <code>type:'one'</code>, cacheField will store a single child. For all others, it will store an array of children.</td>
</tr>
<tr>
<td>bypassSchema</td>
<td>Boolean (optional)</td>
<td>If set to true, it will bypass any <a href="https://github.com/aldeed/meteor-collection2">collection2</a> schema that may exist. Otherwise you must add the cacheField to your schema.</td>
<td>updateOptions</td>
<td>Object (optional)</td>
<td>Options that will be passed to <code>Collection.update</code> method, for example: <code>Collection.update(selector, updateExpression, <i>options</i>)</code>. Can be used to bypass <a href="https://github.com/aldeed/meteor-collection2">collection2</a> by passing <code>{bypassCollection2: true}</code>. Otherwise you must add the cacheField to your schema (if you are using collection2).</td>
</tr>
</table>

Expand Down Expand Up @@ -121,9 +121,9 @@ cacheCount() can be used on "inverse" and "many-inverse" relationships
<td>Can be used to filter the counted documents. <code>[referenceField]:parent._id</code> will always be included though.</td>
</tr>
<tr>
<td>bypassSchema</td>
<td>Boolean (optional)</td>
<td>If set to true, it will bypass any <a href="https://github.com/aldeed/meteor-collection2">collection2</a> schema that may exist. Otherwise you must add the cacheField to your schema.</td>
<td>updateOptions</td>
<td>Object (optional)</td>
<td>Options that will be passed to <code>Collection.update</code> method, for example: <code>Collection.update(selector, updateExpression, <i>options</i>)</code>. Can be used to bypass <a href="https://github.com/aldeed/meteor-collection2">collection2</a> by passing <code>{bypassCollection2: true}</code>. Otherwise you must add the cacheField to your schema (if you are using collection2).</td>
</tr>
</table>

Expand Down Expand Up @@ -160,9 +160,9 @@ Meteor.users.cacheField({
<td>The function used to compute the result. If not defined, the default is to return a string of all watched fields concatenated with <code>', '</code><br>The document provided to the function only contains the fields specified in <code>fields</code></td>
</tr>
<tr>
<td>bypassSchema</td>
<td>Boolean (optional)</td>
<td>If set to true, it will bypass any <a href="https://github.com/aldeed/meteor-collection2">collection2</a> schema that may exist. Otherwise you must add the cacheField to your schema.</td>
<td>updateOptions</td>
<td>Object (optional)</td>
<td>Options that will be passed to <code>Collection.update</code> method, for example: <code>Collection.update(selector, updateExpression, <i>options</i>)</code>. Can be used to bypass <a href="https://github.com/aldeed/meteor-collection2">collection2</a> by passing <code>{bypassCollection2: true}</code>. Otherwise you must add the cacheField to your schema (if you are using collection2).</td>
</tr>
</table>

Expand Down Expand Up @@ -311,8 +311,10 @@ Meteor.setTimeout(()=>{
## Testing the package

```
meteor test-packages packages/denormalize --driver-package=practicalmeteor:mocha
cd testing
meteor npm i
meteor npm run test[:watch]
```
(Then open localhost:3000 in your browser)<br>
The package currently has over 120 tests<br>
The package currently has over 130 tests<br>
Note: The "slowness warnings" in the results are just due to the asynchronous tests
63 changes: 31 additions & 32 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ Mongo.Collection.prototype.cache = function(options){
type:Match.OneOf('one', 'many', 'inversed', 'inverse', 'many-inversed', 'many-inverse'),
referenceField:String,
cacheField:String,
bypassSchema:Match.Optional(Boolean)
updateOptions:Match.Optional(Object)
})
if(options.type == 'inverse') options.type = 'inversed' //Not sure which is best, so why not support both and be typo-friendly
if(options.type == 'many-inverse') options.type = 'many-inversed'

//Bypass collection2 schemas
let parentCollection = options.bypassSchema && Package['aldeed:collection2'] ? this._collection : this
let parentCollection = this
let childCollection = options.collection
let type = options.type
let referenceField = options.referenceField
Expand Down Expand Up @@ -86,7 +85,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}}, options.updateOptions)
}
}
}
Expand All @@ -97,27 +96,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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$unset:{[cacheField]:1}})
parentCollection.update(parent._id, {$unset:{[cacheField]:1}}, options.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}}, {...options.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}}, {...options.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}}, {...options.updateOptions, multi:true})
})
}

Expand All @@ -126,9 +125,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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$set:{[cacheField]:[]}})
parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, options.updateOptions)
}
}
addMigration(parentCollection, insert, options)
Expand All @@ -139,16 +138,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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$set:{[cacheField]:[]}})
parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, options.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}}, {...options.updateOptions, multi:true})
})

childCollection.after.update(function(userId, child, changedFields){
Expand All @@ -157,24 +156,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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}})
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, options.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}}}, {...options.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}}, options.updateOptions)
}
addMigration(parentCollection, insert, options)

Expand All @@ -184,17 +183,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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$set:{[cacheField]:[]}})
parentCollection.update(parent._id, {$set:{[cacheField]:[]}}, options.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}}, options.updateOptions)
}
})

Expand All @@ -203,28 +202,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}}}, options.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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}})
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, options.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}}}, options.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}}, options.updateOptions)
}
addMigration(parentCollection, insert, options)

Expand All @@ -233,15 +232,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}}, options.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}}, {...options.updateOptions,multi:true})
}
})

Expand All @@ -251,16 +250,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}}}, {...options.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}}, options.updateOptions)
} else {
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}})
parentCollection.update(parent._id, {$push:{[cacheField]:pickedChild}}, options.updateOptions)
}
})
}
Expand All @@ -270,7 +269,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}}}, {...options.updateOptions, multi:true})
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions cacheCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Mongo.Collection.prototype.cacheCount = function(options) {
cacheField:String,
referenceField:String,
selector:Match.Optional(Object),
bypassSchema:Match.Optional(Boolean)
updateOptions:Match.Optional(Object)
})

let parentCollection = options.bypassSchema && Package['aldeed:collection2'] ? this._collection : this
let parentCollection = this
let childCollection = options.collection
let selector = options.selector || {}
let cacheField = options.cacheField
Expand All @@ -25,13 +25,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()}}, options.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()}}, options.updateOptions)
}

addMigration(parentCollection, insert, options)
Expand Down
17 changes: 12 additions & 5 deletions cacheField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Mongo.Collection.prototype.cacheField = function(options) {
cacheField:String,
fields:[String],
transform:Match.Optional(Function),
bypassSchema:Match.Optional(Boolean)
updateOptions:Match.Optional(Object)
})

let collection = options.bypassSchema && Package['aldeed:collection2'] ? this._collection : this
let collection = this
let cacheField = options.cacheField
let fields = options.fields
let topFields = _.uniq(_.map(fields, field => field.split('.')[0]))
Expand All @@ -25,8 +25,15 @@ Mongo.Collection.prototype.cacheField = function(options) {
throw new Error('watching the cacheField for changes would cause an infinite loop')
}

function insertHook(userid, doc){
collection.update(doc._id, {$set:{[cacheField]:transform(_.pick(doc, fields))}})
function insert(doc) {
const value = transform(_.pick(doc, fields));
if (!_.isUndefined(value)) {
collection.update(doc._id, {$set:{[cacheField]: value}}, options.updateOptions)
}
}

function insertHook(userId, doc){
insert(doc);
}

addMigration(collection, insertHook, options)
Expand All @@ -36,7 +43,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))}})
insert(doc)
})
}
})
Expand Down
8 changes: 4 additions & 4 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Package.describe({
})

Npm.depends({
'lodash': '4.17.4',
'lodash': '4.17.15',
})

Package.onUse(function (api) {
Expand All @@ -34,10 +34,10 @@ Package.onTest(function (api) {
'ecmascript',
'mongo',
'check',
'aldeed:[email protected]',
'matb33:[email protected]',
'practicalmeteor:mocha',
'practicalmeteor:chai'
'meteortesting:mocha'
])

api.addFiles('tests.js', 'server')
})
})
1 change: 1 addition & 0 deletions testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Loading