index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. /**
  3. * Used to aggregate `inputs` using a MongoDB-style `pipeline`
  4. *
  5. * If `inputs` is given, it will run the `inputs` through the `pipeline` and call the `callback` with the results.
  6. * If `inputs` is omitted, it will return an "aggregator" function so you can reuse the given `pipeline` against various `inputs`.
  7. *
  8. * NOTE: you should be mindful about reusing the same `pipeline` against disparate `inputs` because document coming in can alter the state of it's `DocumentSource`s
  9. *
  10. * @method aggregate
  11. * @namespace mungedb
  12. * @module mungedb-aggregate
  13. * @param pipeline {Array} The list of pipeline document sources in JSON format
  14. * @param [inputs] {Array} Optional inputs to pass through the `docSrcs` pipeline
  15. * @param callback {Function} Called when done
  16. * @param callback.err {Error} The Error if one occurred
  17. * @param callback.docs {Array} The resulting documents
  18. **/
  19. exports = module.exports = function aggregate(pipeline, inputs, callback) { // function-style interface; i.e., return the utility function directly as the require
  20. var ctx = {}, //not used yet
  21. pipelineInst = exports.pipeline.Pipeline.parseCommand({
  22. pipeline: pipeline
  23. }, ctx),
  24. aggregator = function aggregator(inputs, callback) {
  25. if (!callback) throw new Error("arg `callback` is required");
  26. if (!inputs) return callback("arg `inputs` is required");
  27. // rebuild the pipeline on subsequent calls
  28. if (!pipelineInst) {
  29. pipelineInst = exports.pipeline.Pipeline.parseCommand({
  30. pipeline: pipeline
  31. }, ctx);
  32. }
  33. // use or build input src
  34. var src;
  35. if(inputs instanceof exports.pipeline.documentSources.DocumentSource){
  36. src = inputs;
  37. }else{
  38. try{
  39. pipelineInst.collectionName = inputs; //NOTE: use the given `inputs` directly; not really a "name" but we don't really have collection names in mungedb-aggregate
  40. src = exports.pipeline.PipelineD.prepareCursorSource(pipelineInst, pipelineInst.ctx);
  41. }catch(err){
  42. return callback(err);
  43. }
  44. }
  45. // run the pipeline against the input src
  46. return pipelineInst.run(src, function aggregated(err, results){
  47. pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
  48. if(err) return callback(err);
  49. return callback(null, results.result);
  50. });
  51. };
  52. if(inputs) return aggregator(inputs, callback);
  53. return aggregator;
  54. };
  55. // package-style interface; i.e., return a function underneath of the require
  56. exports.aggregate = exports;
  57. //Expose these so that mungedb-aggregate can be extended.
  58. exports.Cursor = require("./Cursor");
  59. exports.pipeline = require("./pipeline/");
  60. // version info
  61. exports.version = "r2.4.0-rc0";
  62. exports.gitVersion = "cb8efcd6a2f05d35655ed9f9b947cc4a99ade8db";