index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 pipelineObj {Array|Object} The list of pipeline document sources in JSON format or object with pipeline and options
  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(pipelineObj, 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. ctx = ctx || {};
  24. var parsePipelineInst;
  25. try {
  26. //Set up the command Object
  27. pipelineObj = (pipelineObj instanceof Array) ? {pipeline: pipelineObj} : pipelineObj;
  28. if (!(pipelineObj instanceof Object)) throw new Error("pipelineObj must be either an Object or an Array");
  29. for (var key in exports.cmdDefaults){
  30. if (exports.cmdDefaults.hasOwnProperty(key) && pipelineObj[key] === undefined){
  31. pipelineObj[key] = exports.cmdDefaults[key];
  32. }
  33. }
  34. parsePipelineInst = exports.pipeline.Pipeline.parseCommand(pipelineObj, ctx);
  35. } catch(ex) {
  36. // Error handling is funky since this can be used multiple different ways
  37. if (callback){
  38. if (inputs) return callback(ex);
  39. else {
  40. return function aggregator(ctx, inputs, callback) {
  41. if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
  42. return callback(ex);
  43. };
  44. }
  45. } else {
  46. throw ex;
  47. }
  48. }
  49. if (pipelineObj.explain){
  50. if (inputs){
  51. ctx.ns = inputs; //NOTE: use the given `inputs` directly; hacking so that the cursor source will be our inputs instead of the context namespace
  52. exports.pipeline.PipelineD.prepareCursorSource(parsePipelineInst, ctx);
  53. }
  54. return parsePipelineInst.writeExplainOps();
  55. }
  56. var aggregator = function aggregator(ctx, inputs, callback) {
  57. if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
  58. var batchSize = pipelineObj.batchSize,
  59. pipelineInst = parsePipelineInst;
  60. parsePipelineInst = null;
  61. if (!callback) {
  62. batchSize = Infinity;
  63. callback = exports.SYNC_CALLBACK;
  64. }
  65. if (!inputs) return callback("arg `inputs` is required");
  66. try {
  67. // rebuild the pipeline on subsequent calls
  68. if (!pipelineInst) {
  69. pipelineInst = exports.pipeline.Pipeline.parseCommand(pipelineObj, ctx);
  70. }
  71. ctx.ns = inputs; //NOTE: use the given `inputs` directly; hacking so that the cursor source will be our inputs instead of the context namespace
  72. exports.pipeline.PipelineD.prepareCursorSource(pipelineInst, ctx);
  73. // run the pipeline against
  74. pipelineInst.stitch();
  75. } catch(err) {
  76. return callback(err);
  77. }
  78. var batch = [];
  79. var runCallback = function aggregated(err, document){
  80. if (!callback) return;
  81. if(err) {
  82. callback(err);
  83. callback = undefined;//we are officially done. make sure the callback doesn't get called anymore
  84. return;
  85. }
  86. if (document === null){
  87. callback(null, batch);
  88. if (batchSize !== Infinity){
  89. callback(null, null); //this is to tell the caller that that we are done aggregating
  90. }
  91. callback = undefined;//we are officially done. make sure the callback doesn't get called anymore
  92. return;
  93. }
  94. batch.push(document);
  95. if (batch.length >= batchSize){
  96. callback(null, batch);
  97. batch = [];
  98. return;
  99. }
  100. };
  101. pipelineInst.run(runCallback);
  102. return batch;
  103. };
  104. if(inputs) return aggregator(ctx, inputs, callback);
  105. return aggregator;
  106. };
  107. // sync callback for aggregate if none was provided
  108. exports.SYNC_CALLBACK = function(err, docs){
  109. if (err) throw err;
  110. return docs;
  111. };
  112. exports.cmdDefaults = {
  113. batchSize: 150,
  114. explain: false
  115. };
  116. // package-style interface; i.e., return a function underneath of the require
  117. exports.aggregate = exports;
  118. //Expose these so that mungedb-aggregate can be extended.
  119. exports.pipeline = require("./pipeline/");
  120. exports.query = require("./query/");
  121. // version info
  122. exports.version = "r2.6.5";
  123. exports.gitVersion = "e99d4fcb4279c0279796f237aa92fe3b64560bf6";
  124. // error code constants
  125. exports.ERRORS = require('./Errors.js');