PipelineCommand.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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(cmdObj){
  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 = Pipeline.parseCommand(cmdObj);
  17. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. var Pipeline = require('../pipeline/Pipeline'),
  19. PipelineD = require('../pipeline/PipelineD');
  20. /**
  21. * Execute the pipeline.
  22. * @method executePipeline
  23. **/
  24. proto.executePipeline = function executePipeline(result, pPipeline, pSource){
  25. return pPipeline.run(result, pSource);
  26. };
  27. /**
  28. * Documents are retrieved until the cursor is exhausted (or another termination condition occurs).
  29. * @method runExecute
  30. **/
  31. proto.runExecute = function runExecute(result, db){
  32. var pSource = PipelineD.prepareCursorSource(this.pPipeline, db);
  33. return this.executePipeline(result, this.pPipeline, pSource);
  34. };
  35. /**
  36. * run the command
  37. * @method run
  38. **/
  39. proto.run = function run(db, result){
  40. if (!this.pPipeline){
  41. return false;
  42. }
  43. return this.runExecute(result, db);
  44. };
  45. return klass;
  46. })();