Comparing version 1.2.1 to 1.3.0
1.3.0 / 2014-11-06 | ||
================== | ||
* added; setTraceFunction() #53 from [jlai](https://github.com/jlai) | ||
1.2.1 / 2014-09-26 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -12,2 +12,3 @@ 'use strict'; | ||
this.collection = col; | ||
this.collectionName = col.collectionName; | ||
} | ||
@@ -14,0 +15,0 @@ |
@@ -53,2 +53,3 @@ 'use strict'; | ||
this._collection = proto._collection || undefined; | ||
this._traceFunction = proto._traceFunction || undefined; | ||
@@ -139,2 +140,3 @@ if (options) { | ||
p._collection = this._collection; | ||
p._traceFunction = this._traceFunction; | ||
@@ -1615,2 +1617,6 @@ return CustomQuery; | ||
debug('find', conds, options); | ||
callback = this._wrapCallback('find', callback, { | ||
conditions: conds | ||
, options: options | ||
}); | ||
@@ -1667,2 +1673,7 @@ this._collection.find(conds, options, utils.tick(callback)); | ||
debug('findOne', conds, options); | ||
callback = this._wrapCallback('findOne', callback, { | ||
conditions: conds | ||
, options: options | ||
}); | ||
this._collection.findOne(conds, options, utils.tick(callback)); | ||
@@ -1715,2 +1726,7 @@ | ||
debug('count', conds, options); | ||
callback = this._wrapCallback('count', callback, { | ||
conditions: conds | ||
, options: options | ||
}); | ||
this._collection.count(conds, options, utils.tick(callback)); | ||
@@ -1795,2 +1811,7 @@ return this; | ||
debug('distinct', conds, options); | ||
callback = this._wrapCallback('distinct', callback, { | ||
conditions: conds | ||
, options: options | ||
}); | ||
this._collection.distinct(this._distinct, conds, options, utils.tick(callback)); | ||
@@ -1945,2 +1966,8 @@ | ||
debug('update', criteria, doc, options); | ||
callback = this._wrapCallback('update', callback, { | ||
conditions: criteria | ||
, doc: doc | ||
, options: options | ||
}); | ||
this._collection.update(criteria, doc, options, utils.tick(callback)); | ||
@@ -2007,2 +2034,7 @@ | ||
debug('remove', conds, options); | ||
callback = this._wrapCallback('remove', callback, { | ||
conditions: conds | ||
, options: options | ||
}); | ||
this._collection.remove(conds, options, utils.tick(callback)); | ||
@@ -2181,2 +2213,7 @@ | ||
debug('findAndModify', conds, doc, opts); | ||
callback = this._wrapCallback('findAndModify', callback, { | ||
conditions: conds | ||
, doc: doc | ||
, options: opts | ||
}); | ||
@@ -2190,2 +2227,59 @@ this._collection | ||
/** | ||
* Wrap callback to add tracing | ||
* | ||
* @param {Function} callback | ||
* @param {Object} [queryInfo] | ||
* @api private | ||
*/ | ||
Query.prototype._wrapCallback = function (method, callback, queryInfo) { | ||
var traceFunction = this._traceFunction || Query.traceFunction; | ||
if (traceFunction) { | ||
queryInfo.collectionName = this._collection.collectionName; | ||
var traceCallback = traceFunction && | ||
traceFunction.call(null, method, queryInfo, this); | ||
var startTime = new Date().getTime(); | ||
return function wrapperCallback (err, result) { | ||
if (traceCallback) { | ||
var millis = new Date().getTime() - startTime; | ||
traceCallback.call(null, err, result, millis); | ||
} | ||
if (callback) { | ||
callback.apply(null, arguments); | ||
} | ||
}; | ||
} | ||
return callback; | ||
} | ||
/** | ||
* Add trace function that gets called when the query is executed. | ||
* The function will be called with (method, queryInfo, query) and | ||
* should return a callback function which will be called | ||
* with (err, result, millis) when the query is complete. | ||
* | ||
* queryInfo is an object containing: { | ||
* collectionName: <name of the collection>, | ||
* conditions: <query criteria>, | ||
* options: <comment, fields, readPreference, etc>, | ||
* doc: [document to update, if applicable] | ||
* } | ||
* | ||
* NOTE: Does not trace stream queries. | ||
* | ||
* @param {Function} traceFunction | ||
* @return {Query} this | ||
* @api public | ||
*/ | ||
Query.prototype.setTraceFunction = function (traceFunction) { | ||
this._traceFunction = traceFunction; | ||
return this; | ||
} | ||
/** | ||
* Executes the query | ||
@@ -2489,2 +2583,15 @@ * | ||
/** | ||
* Set a trace function that will get called whenever a | ||
* query is executed. | ||
* | ||
* See `setTraceFunction()` for details. | ||
* | ||
* @param {Object} conds | ||
* @return {Boolean} | ||
*/ | ||
Query.setGlobalTraceFunction = function (traceFunction) { | ||
Query.traceFunction = traceFunction; | ||
} | ||
/*! | ||
@@ -2503,1 +2610,2 @@ * Exports. | ||
// test utils | ||
{ | ||
"name": "mquery", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Expressive query building for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "lib/mquery.js", |
@@ -107,2 +107,4 @@ #mquery | ||
- [setOptions](#setoptionsoptions) | ||
- [setTraceFunction](#settracefunctionfunc) | ||
- [mquery.setGlobalTraceFunction](#mquerysetglobaltracefunctionfunc) | ||
- [mquery.canMerge](#mquerycanmerge) | ||
@@ -1093,2 +1095,42 @@ - [mquery.use$geoWithin](#mqueryusegeowithin) | ||
###setTraceFunction(func) | ||
Set a function to trace this query. Useful for profiling or logging. | ||
```js | ||
function traceFunction (method, queryInfo, query) { | ||
console.log('starting ' + method + ' query'); | ||
return function (err, result, millis) { | ||
console.log('finished ' + method + ' query in ' + millis + 'ms'); | ||
}; | ||
} | ||
mquery().setTraceFunction(traceFunction).findOne({name: 'Joe'}, cb); | ||
``` | ||
The trace function is passed (method, queryInfo, query) | ||
- method is the name of the method being called (e.g. findOne) | ||
- queryInfo contains information about the query: | ||
- conditions: query conditions/criteria | ||
- options: options such as sort, fields, etc | ||
- doc: document being updated | ||
- query is the query object | ||
The trace function should return a callback function which accepts: | ||
- err: error, if any | ||
- result: result, if any | ||
- millis: time spent waiting for query result | ||
NOTE: stream requests are not traced. | ||
###mquery.setGlobalTraceFunction(func) | ||
Similar to `setTraceFunction()` but automatically applied to all queries. | ||
```js | ||
mquery.setTraceFunction(traceFunction); | ||
``` | ||
###mquery.canMerge(conditions) | ||
@@ -1095,0 +1137,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
208711
21
5484
1221