PipelineCommand.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. var DocumentSource = require("../pipeline/documentSources/DocumentSource");
  22. /**
  23. * Execute the pipeline.
  24. * @method executePipeline
  25. **/
  26. proto.executePipeline = function executePipeline(pPipeline, pSource, callback){
  27. return pPipeline.run(pSource, callback);
  28. };
  29. /**
  30. * Documents are retrieved until the cursor is exhausted (or another termination condition occurs).
  31. * @method runExecute
  32. **/
  33. proto.runExecute = function runExecute(db, callback){
  34. var pSource = db instanceof DocumentSource ? db : klass.PipelineD.prepareCursorSource(this.pPipeline, db);
  35. return this.executePipeline(this.pPipeline, pSource, callback);
  36. };
  37. /**
  38. * run the command
  39. * @method run
  40. **/
  41. proto.run = function run(db, callback){
  42. if (!this.pPipeline){
  43. return callback(new Error('Pipeline not defined'));
  44. }
  45. this.runExecute(db, callback);
  46. };
  47. return klass;
  48. })();