This repository has been archived by the owner on May 3, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace nodeunit to mocha and cleanup!
- Loading branch information
Showing
6 changed files
with
326 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,113 @@ | ||
module.exports = require('./libs/mongoose_q'); | ||
'use strict'; | ||
|
||
var | ||
Q = require('q'), | ||
MONGOOSE_MODEL_STATICS = [ | ||
// mongoose.Model static | ||
'remove', 'ensureIndexes', 'find', 'findById', 'findOne', 'count', 'distinct', | ||
'findOneAndUpdate', 'findByIdAndUpdate', 'findOneAndRemove', 'findByIdAndRemove', | ||
'create', 'update', 'mapReduce', 'aggregate', 'populate', | ||
'geoNear', 'geoSearch', | ||
// mongoose.Document static | ||
'update' | ||
], | ||
MONGOOSE_MODEL_METHODS = [ | ||
// mongoose.Model instance | ||
'save', 'remove', | ||
// mongoose.Document instance | ||
'populate', 'update', 'validate' | ||
], | ||
MONGOOSE_QUERY_METHODS = [ | ||
// mongoose.Query instance | ||
'find', 'exec', 'findOne', 'count', 'distinct', 'update', 'remove', | ||
'findOneAndUpdate', 'findOneAndRemove', 'lean', 'limit', 'skip', 'sort' | ||
], | ||
MONGOOSE_AGGREGATE_METHODS = [ | ||
'exec' | ||
], | ||
apslice = Array.prototype.slice, | ||
debug = console.log.bind(console), | ||
DEBUG = !!process.env.MONGOOSEQ_DEBUG; | ||
|
||
/** | ||
* @module mongooseq | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {object} obj | ||
* @param {Array.<string>} funcNames - original function names to apply Q | ||
* @param {function(string):string} funcNameMapper maps a function name into Q-applied one | ||
* @param {*} [spread=false] use spread for multi-results | ||
*/ | ||
function qualify(obj, funcNames, funcNameMapper, spread) { | ||
funcNames.forEach(function (funcName) { | ||
if (typeof(obj[funcName]) !== 'function') { | ||
DEBUG && debug('***skip*** function not found:', funcName); | ||
return; | ||
} | ||
var mappedFuncName = funcNameMapper(funcName); | ||
DEBUG && debug('wrap function:', funcName, '-->', mappedFuncName); | ||
obj[mappedFuncName] = function () { | ||
var d = Q.defer(); | ||
var args = apslice.call(arguments); | ||
args.push(function (err, result) { | ||
if (err) { | ||
return d.reject(err); | ||
} | ||
// with 'spread' option: returns 'all' result with 'spread' only for multiple result | ||
if (spread && arguments.length > 2) { | ||
return d.resolve(apslice.call(arguments, 1)); | ||
} | ||
// without 'spread' option: returns the 'first' result only and ignores following result | ||
return d.resolve(result); | ||
}); | ||
// fix https://github.com/iolo/mongoose-q/issues/1 | ||
// mongoose patches some instance methods after instantiation. :( | ||
this[funcName].apply(this, args); | ||
return d.promise; | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* add Q wrappers for static/instance functions of mongoose model and query. | ||
* | ||
* @param {mongoose.Mongoose} [mongoose] | ||
* @param {object.<string,*>} [options={}] - prefix and/or suffix for wrappers | ||
* @param {string} [options.prefix=''] | ||
* @param {string} [options.suffix=''] | ||
* @param {function(string):string} [options.mapper] | ||
* @param {boolean} [options.spread=false] | ||
* @returns {mongoose.Mongoose} the same mongoose instance, for convenince | ||
*/ | ||
function mongooseQ(mongoose, options) { | ||
mongoose = mongoose || require('mongoose'); | ||
options = options || {}; | ||
var prefix = options.prefix || ''; | ||
var suffix = options.suffix || 'Q'; | ||
var mapper = options.mapper || function (funcName) { | ||
return prefix + funcName + suffix; | ||
}; | ||
var spread = options.spread; | ||
// avoid duplicated application for custom mapper function... | ||
var applied = require('crypto').createHash('md5').update(mapper.toString()).digest('hex'); | ||
if (mongoose['__q_applied_' + applied]) { | ||
return mongoose; | ||
} | ||
|
||
qualify(mongoose.Model, MONGOOSE_MODEL_STATICS, mapper, spread); | ||
qualify(mongoose.Model.prototype, MONGOOSE_MODEL_METHODS, mapper, spread); | ||
qualify(mongoose.Query.prototype, MONGOOSE_QUERY_METHODS, mapper, spread); | ||
|
||
// see https://github.com/iolo/mongoose-q/issues/6 and | ||
// https://github.com/LearnBoost/mongoose/issues/1910 | ||
var Aggregate = require('mongoose/lib/aggregate'); | ||
qualify(Aggregate.prototype, MONGOOSE_AGGREGATE_METHODS, mapper, spread); | ||
|
||
mongoose['__q_applied_' + applied] = true; | ||
return mongoose; | ||
} | ||
|
||
module.exports = mongooseQ; | ||
module.exports.qualify = qualify; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.