PipelineCommand.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var PipelineCommand = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Represents a Pipeline Command
  6. *
  7. * @class PipelineCommand
  8. * @namespace mungedb.aggregate.pipeline
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var klass = function PipelineCommand(docSrcs, pipelineArgs){
  13. /* try to parse the command; if this fails, then we didn't run */
  14. //NOTE: this is different from the mongo implementation. It used to be in the 'run' method that we would parse the pipeline,
  15. //but we decided it would be better to be able to save the parsed command
  16. this.pPipeline = klass.Pipeline.parseCommand(docSrcs, pipelineArgs);
  17. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. //Put these on the class so they can be overriden.
  19. klass.Pipeline = require('../pipeline/Pipeline');
  20. klass.PipelineD = require('../pipeline/PipelineD');
  21. /**
  22. * Execute the pipeline.
  23. * @method executePipeline
  24. **/
  25. proto.executePipeline = function executePipeline(pPipeline, pSource, callback){
  26. return pPipeline.run(pSource, callback);
  27. };
  28. /**
  29. * Documents are retrieved until the cursor is exhausted (or another termination condition occurs).
  30. * @method runExecute
  31. **/
  32. proto.runExecute = function runExecute(db, callback){
  33. var pSource = klass.PipelineD.prepareCursorSource(this.pPipeline, db);
  34. return this.executePipeline(this.pPipeline, pSource, callback);
  35. };
  36. /**
  37. * run the command
  38. * @method run
  39. **/
  40. proto.run = function run(db, callback){
  41. if (!this.pPipeline){
  42. return callback(new Error('Pipeline not defined'));
  43. }
  44. this.runExecute(db, callback);
  45. };
  46. return klass;
  47. })();