Aggregator.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. var PipelineCommand = require('./commands/PipelineCommand');
  3. var Aggregator = module.exports = (function(){
  4. // CONSTRUCTOR
  5. var base = Object, proto, klass = function Aggregator(docSrcs, pipelineArgs){
  6. if (!docSrcs){
  7. throw new Error("mungedb Aggregator requires a pipeline!");
  8. }
  9. if (typeof docSrcs.length !== "number"){
  10. docSrcs = [docSrcs];
  11. }
  12. this.docSrcs = docSrcs;
  13. this.pipelineArgs = pipelineArgs;
  14. };
  15. proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. // PROTOTYPE MEMBERS
  17. proto.execute = function execute(inputs, callback){
  18. //! @ticket #2266: always create a new PipelineCommand just before we use it.
  19. var pipeline = new PipelineCommand(this.docSrcs, this.pipelineArgs);
  20. pipeline.run(inputs,function(err, output){
  21. if (err) return callback(err);
  22. return callback(null, output.result);
  23. });
  24. };
  25. //Expose these so that mungedb-aggregate can be extended.
  26. klass.Pipeline = require("./pipeline/Pipeline");
  27. klass.accumulators = require("./pipeline/accumulators");
  28. klass.documentSources = require("./pipeline/documentSources");
  29. klass.expressions = require("./pipeline/expressions");
  30. klass.commands = require("./commands");
  31. klass.Cursor = require("./Cursor");
  32. return klass;
  33. })();