index.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 [ctx] {Object} Optional context object to pass through to pipeline
  15. * @param [inputs] {Array} Optional inputs to pass through the `docSrcs` pipeline
  16. * @param [callback] {Function} Optional callback if using async extensions, called when done
  17. * @param callback.err {Error} The Error if one occurred
  18. * @param callback.docs {Array} The resulting documents
  19. **/
  20. exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) { // function-style interface; i.e., return the utility function directly as the require
  21. var DocumentSource = exports.pipeline.documentSources.DocumentSource;
  22. if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
  23. var pipelineInst = exports.pipeline.Pipeline.parseCommand({
  24. pipeline: pipeline
  25. }, ctx),
  26. aggregator = function aggregator(ctx, inputs, callback) {
  27. if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
  28. if (!callback) callback = exports.SYNC_CALLBACK;
  29. if (!inputs) return callback("arg `inputs` is required");
  30. // rebuild the pipeline on subsequent calls
  31. if (!pipelineInst) {
  32. pipelineInst = exports.pipeline.Pipeline.parseCommand({
  33. pipeline: pipeline
  34. }, ctx);
  35. }
  36. // use or build input src
  37. var src;
  38. if(inputs instanceof DocumentSource){
  39. src = inputs;
  40. }else{
  41. try{
  42. pipelineInst.collectionName = inputs; //NOTE: use the given `inputs` directly; not really a "name" but we don't really have collection names in mungedb-aggregate
  43. src = exports.pipeline.PipelineD.prepareCursorSource(pipelineInst, pipelineInst.ctx);
  44. }catch(err){
  45. return callback(err);
  46. }
  47. }
  48. // run the pipeline against the input src
  49. var results = pipelineInst.run(src, callback === exports.SYNC_CALLBACK ? undefined : function aggregated(err, results){
  50. if(err) return callback(err);
  51. return callback(null, results.result);
  52. });
  53. pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
  54. return results;
  55. };
  56. if(inputs) return aggregator(ctx, inputs, callback);
  57. return aggregator;
  58. };
  59. // sync callback for aggregate if none was provided
  60. exports.SYNC_CALLBACK = function(err, docs){
  61. if (err) throw err;
  62. return docs;
  63. };
  64. // package-style interface; i.e., return a function underneath of the require
  65. exports.aggregate = exports;
  66. //Expose these so that mungedb-aggregate can be extended.
  67. exports.Cursor = require("./Cursor");
  68. exports.pipeline = require("./pipeline/");
  69. // version info
  70. exports.version = "r2.4.0-rc0";
  71. exports.gitVersion = "cb8efcd6a2f05d35655ed9f9b947cc4a99ade8db";