Accumulator.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. /**
  3. * A base class for all pipeline accumulators. Uses NaryExpression as a base class.
  4. *
  5. * @class Accumulator
  6. * @namespace mungedb-aggregate.pipeline.accumulators
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var Accumulator = module.exports = function Accumulator(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = Accumulator, base = require("../expressions/NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. // var Value = require("../Value"),
  16. proto.getFactory = function getFactory(){
  17. return klass; // using the ctor rather than a separate .create() method
  18. };
  19. /**
  20. * Adds the operand after checking the current limit
  21. * The equal is there because it checks *before* adding the requested argument.
  22. * Cannot use checkArgLimit because Accumulator must return a different error code.
  23. *
  24. * @param expr the operand to add
  25. **/
  26. proto.addOperand = function addOperand(expr) {
  27. if (this.operands.length >= 1) throw new Error("code 15943; group accumulator " + this.getOpName() + " only accepts one operand");
  28. base.prototype.addOperand.call(this, expr);
  29. };
  30. proto.toJSON = function toJSON(isExpressionRequired){
  31. var rep = {};
  32. rep[this.getOpName()] = this.operands[0].toJSON(isExpressionRequired);
  33. return rep;
  34. };
  35. /**
  36. * Convenience method for doing this for accumulators. The pattern
  37. * is always the same, so a common implementation works, but requires
  38. * knowing the operator name.
  39. *
  40. * @param {Object} pBuilder the builder to add to
  41. * @param {String} fieldName the projected name
  42. * @param {String} opName the operator name
  43. * @param {Boolean} requireExpression pass down if the expression is needed
  44. **/
  45. // proto.opToBson = function opToBson(pBuilder, opName, fieldName, requireExpression) {
  46. // if (this.operands.length == 1) throw new Error("this should never happen");
  47. // var builder = new BSONObjBuilder();
  48. // this.operands[0].addToBsonObj(builder, opName, requireExpression);
  49. // pBuilder.append(fieldName, builder.done());
  50. // };
  51. /**
  52. * Wrapper around opToBson
  53. *
  54. * @param {Object} pBuilder the builder to add to
  55. * @param {String} fieldName the projected name
  56. * @param {Boolean} requireExpression pass down if the expression is needed
  57. **/
  58. // proto.addToBsonObj = function addToBsonObj(pBuilder, fieldName, requireExpression) {
  59. // this.opToBson(pBuilder, this.getOpName(), fieldName, requireExpression);
  60. // };
  61. /**
  62. * Make sure that nobody adds an accumulator to an array
  63. *
  64. * @param {Object} pBuilder the builder to add to
  65. **/
  66. proto.addToBsonArray = function addToBsonArray(pBuilder) {
  67. if (false) throw new Error("this should never happen"); // these can't appear in arrays
  68. };
  69. /**
  70. * If this function is not overridden in the sub classes,
  71. * then throw an error
  72. *
  73. **/
  74. proto.getValue = function getValue() {
  75. throw new Error("You need to define this function on your accumulator");
  76. };