index.js 2.8 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,
  22. aggregator = function aggregator(inputs, callback) {
  23. if (!inputs) return callback("arg `inputs` is required");
  24. // rebuild the pipeline on subsequent calls
  25. pipelineInst = exports.pipeline.Pipeline.parseCommand({
  26. pipeline: pipeline
  27. }, ctx);
  28. // use or build input src
  29. try{
  30. ctx.ns = inputs; //NOTE: use the given `inputs` directly; hacking so that the cursor source will be our inputs instead of the context namespace
  31. exports.pipeline.PipelineD.prepareCursorSource(pipelineInst, ctx);
  32. }catch(err){
  33. return callback(err);
  34. }
  35. var runCallback;
  36. if (!callback) {
  37. runCallback = exports.SYNC_CALLBACK;
  38. pipelineInst.SYNC_MODE = true;
  39. } else {
  40. runCallback = function aggregated(err, results){
  41. if(err) return callback(err);
  42. return callback(null, results.result);
  43. };
  44. }
  45. // run the pipeline against
  46. pipelineInst.stitch();
  47. var results = pipelineInst.run(runCallback);
  48. return results ? results.result : undefined;
  49. };
  50. if(inputs) return aggregator(inputs, callback);
  51. return aggregator;
  52. };
  53. // sync callback for aggregate if none was provided
  54. exports.SYNC_CALLBACK = function(err, docs){
  55. if (err) throw err;
  56. };
  57. // package-style interface; i.e., return a function underneath of the require
  58. exports.aggregate = exports;
  59. //Expose these so that mungedb-aggregate can be extended.
  60. exports.Cursor = require("./Cursor");
  61. exports.pipeline = require("./pipeline/");
  62. // version info
  63. exports.version = "r2.5.4";
  64. exports.gitVersion = "ffd52e5f46cf2ba74ba931c78da62d4a7f480d8e";
  65. // error code constants
  66. exports.ERRORS = require('./Errors.js');