ConstantExpression.js 1.6 KB

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