index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. try{
  35. ctx.ns = inputs; //NOTE: use the given `inputs` directly; not really a "name" but we don't really have collection names in mungedb-aggregate
  36. exports.pipeline.PipelineD.prepareCursorSource(pipelineInst, ctx);
  37. }catch(err){
  38. return callback(err);
  39. }
  40. // run the pipeline against
  41. pipelineInst.stitch();
  42. var results = pipelineInst.run(callback === exports.SYNC_CALLBACK ? undefined : function aggregated(err, results){
  43. if(err) return callback(err);
  44. return callback(null, results.result);
  45. });
  46. pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
  47. return results ? results.result : undefined;
  48. };
  49. if(inputs) return aggregator(inputs, callback);
  50. return aggregator;
  51. };
  52. // sync callback for aggregate if none was provided
  53. exports.SYNC_CALLBACK = function(err, docs){
  54. if (err) throw err;
  55. return docs;
  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');