ConstantExpression.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // DEPENDENCIES
  15. var Value = require("../Value"),
  16. Expression = require("./Expression");
  17. // PROTOTYPE MEMBERS
  18. proto.getOpName = function getOpName(){
  19. return "$const";
  20. };
  21. /**
  22. * Get the constant value represented by this Expression.
  23. * @method getValue
  24. * @returns the value
  25. **/
  26. proto.getValue = function getValue(){ //TODO: convert this to an instance field rather than a property
  27. return this.value;
  28. };
  29. proto.addDependencies = function addDependencies(deps, path) {
  30. // nothing to do
  31. };
  32. klass.parse = function parse(expr, vps){
  33. return new ConstantExpression(expr);
  34. };
  35. /**
  36. * Get the constant value represented by this Expression.
  37. * @method evaluate
  38. **/
  39. proto.evaluateInternal = function evaluateInternal(vars){
  40. return this.value;
  41. };
  42. proto.optimize = function optimize() {
  43. return this; // nothing to do
  44. };
  45. proto.serialize = function(rawValue){
  46. return rawValue ? {$const: this.value} : this.value;
  47. };
  48. //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
  49. //TODO: proto.addToBsonArray
  50. /** Register Expression */
  51. Expression.registerExpression("$const",klass.parse(ConstantExpression));
  52. Expression.registerExpression("$literal", klass.parse(ConstantExpression)); // alias