index.js 3.2 KB

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