ConstantExpression.js 1.5 KB

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