ConstantExpression.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var ConstantExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** Internal expression for constant values **/
  4. var klass = function ConstantExpression(value){
  5. if(arguments.length !== 1) throw new Error("args expected: value");
  6. this.value = value; //TODO: actually make read-only in terms of JS?
  7. base.call(this);
  8. }, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  9. // PROTOTYPE MEMBERS
  10. proto.getOpName = function getOpName(){
  11. return "$const";
  12. };
  13. /**Get the constant value represented by this Expression.
  14. | @returns the value
  15. **/
  16. proto.getValue = function getValue(){ //TODO: convert this to an instance field rather than a property
  17. return this.value;
  18. };
  19. proto.addDependencies = function addDependencies(deps, path) {
  20. // nothing to do
  21. };
  22. /** Get the constant value represented by this Expression. **/
  23. proto.evaluate = function evaluate(doc){
  24. return this.value;
  25. };
  26. proto.optimize = function optimize() {
  27. return this; // nothing to do
  28. };
  29. proto.toJson = function(isExpressionRequired){
  30. return isExpressionRequired ? {$const: this.value} : this.value;
  31. };
  32. //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
  33. //TODO: proto.addToBsonArray
  34. return klass;
  35. })();