Browse Source

Merge pull request #10 in EAGLE6/mungedb-aggregate from bugfix/EAGLESIX-2127-fix-bug-in-ctx-arg to develop

* commit '24e4a00c61fe760f405e0de7dae4423e80742246':
  EAGLESIX-2127: fix bug in aggregate when ctx is omitted and inputs is a doc src
Kyle Davis 11 years ago
parent
commit
2623bd2b0a
1 changed files with 8 additions and 6 deletions
  1. 8 6
      lib/index.js

+ 8 - 6
lib/index.js

@@ -11,19 +11,21 @@
  * @method aggregate
  * @namespace mungedb
  * @module mungedb-aggregate
- * @param pipeline  {Array}  The list of pipeline document sources in JSON format
- * @param [inputs]  {Array}  Optional inputs to pass through the `docSrcs` pipeline
- * @param [callback]               {Function}                                 Optional callback if using async extensions, called when done
+ * @param pipeline  {Array}   The list of pipeline document sources in JSON format
+ * @param [ctx]     {Object}  Optional context object to pass through to pipeline
+ * @param [inputs]  {Array}   Optional inputs to pass through the `docSrcs` pipeline
+ * @param [callback]             {Function}                                 Optional callback if using async extensions, called when done
  * @param   callback.err           {Error}                                    The Error if one occurred
  * @param   callback.docs          {Array}                                    The resulting documents
  **/
 exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) {	// function-style interface; i.e., return the utility function directly as the require
-	if (ctx instanceof Array) callback = inputs, inputs = ctx, ctx = {};
+	var DocumentSource = exports.pipeline.documentSources.DocumentSource;
+	if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
 	var pipelineInst = exports.pipeline.Pipeline.parseCommand({
 			pipeline: pipeline
 		}, ctx),
 		aggregator = function aggregator(ctx, inputs, callback) {
-			if (ctx instanceof Array) callback = inputs,  inputs = ctx, ctx = {};
+			if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
 			if (!callback) callback = exports.SYNC_CALLBACK;
 			if (!inputs) return callback("arg `inputs` is required");
 
@@ -36,7 +38,7 @@ exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) {
 
 			// use or build input src
 			var src;
-			if(inputs instanceof exports.pipeline.documentSources.DocumentSource){
+			if(inputs instanceof DocumentSource){
 				src = inputs;
 			}else{
 				try{