ConstantExpression.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. /**
  3. * Internal expression for constant values
  4. * @class ConstantExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var ConstantExpression = module.exports = function ConstantExpression(value){
  10. if (arguments.length !== 1) throw new Error("args expected: value");
  11. this.value = value; //TODO: actually make read-only in terms of JS?
  12. base.call(this);
  13. }, klass = ConstantExpression, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // PROTOTYPE MEMBERS
  15. proto.getOpName = function getOpName(){
  16. return "$const";
  17. };
  18. /**
  19. * Get the constant value represented by this Expression.
  20. * @method getValue
  21. * @returns the value
  22. **/
  23. proto.getValue = function getValue(){ //TODO: convert this to an instance field rather than a property
  24. return this.value;
  25. };
  26. proto.addDependencies = function addDependencies(deps, path) {
  27. // nothing to do
  28. };
  29. /**
  30. * Get the constant value represented by this Expression.
  31. * @method evaluate
  32. **/
  33. proto.evaluate = function evaluate(doc){
  34. return this.value;
  35. };
  36. proto.optimize = function optimize() {
  37. return this; // nothing to do
  38. };
  39. proto.toJSON = function(isExpressionRequired){
  40. return isExpressionRequired ? {$const: this.value} : this.value;
  41. };
  42. //TODO: proto.addToBsonObj --- may be required for $project to work -- my hope is that we can implement toJSON methods all around and use that instead
  43. //TODO: proto.addToBsonArray