index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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} Optional callback if using async extensions, 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) callback = exports.SYNC_CALLBACK;
  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. var results = pipelineInst.run(src, callback === exports.SYNC_CALLBACK ? undefined : function aggregated(err, results){
  47. if(err) return callback(err);
  48. return callback(null, results.result);
  49. });
  50. pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
  51. return results;
  52. };
  53. if(inputs) return aggregator(inputs, callback);
  54. return aggregator;
  55. };
  56. // sync callback for aggregate if none was provided
  57. exports.SYNC_CALLBACK = function(err, docs){
  58. if (err) throw err;
  59. return docs;
  60. };
  61. // package-style interface; i.e., return a function underneath of the require
  62. exports.aggregate = exports;
  63. //Expose these so that mungedb-aggregate can be extended.
  64. exports.Cursor = require("./Cursor");
  65. exports.pipeline = require("./pipeline/");
  66. // version info
  67. exports.version = "r2.4.0-rc0";
  68. exports.gitVersion = "cb8efcd6a2f05d35655ed9f9b947cc4a99ade8db";