ProjectDocumentSource.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. var DocumentSource = require('./DocumentSource');
  3. /**
  4. * A base class for filter document sources
  5. * @class ProjectDocumentSource
  6. * @namespace mungedb-aggregate.pipeline.documentSources
  7. * @module mungedb-aggregate
  8. * @constructor
  9. * @param [ctx] {ExpressionContext}
  10. **/
  11. var ProjectDocumentSource = module.exports = function ProjectDocumentSource(ctx, exprObj){
  12. if (arguments.length > 2) throw new Error("up to two args expected");
  13. base.call(this, ctx);
  14. this.OE = ObjectExpression.create();
  15. this._raw = undefined;
  16. this._variables = undefined;
  17. }, klass = ProjectDocumentSource, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. // DEPENDENCIES
  19. var Expression = require('../expressions/Expression'),
  20. ObjectExpression = require('../expressions/ObjectExpression'),
  21. Value = require('../Value'),
  22. Variables = require('../expressions/Variables'),
  23. VariablesIdGenerator = require('../expressions/VariablesIdGenerator'),
  24. VariablesParseState = require('../expressions/VariablesParseState');
  25. klass.projectName = "$project";
  26. /**
  27. * Returns the name of project
  28. * @return {string} the name of project
  29. **/
  30. proto.getSourceName = function getSourceName() {
  31. return klass.projectName;
  32. };
  33. proto.getNext = function getNext(callback) {
  34. if (!callback) throw new Error(this.getSourceName() + ' #getNext() requires callback');
  35. var self = this,
  36. out;
  37. this.source.getNext(function(err, input) {
  38. if (err)
  39. return callback(null, err);
  40. if (input === null) {
  41. out = input;
  42. return callback(null, null);
  43. }
  44. /* create the result document */
  45. out = {};
  46. /**
  47. * Use the ExpressionObject to create the base result.
  48. *
  49. * If we're excluding fields at the top level, leave out the _id if
  50. * it is found, because we took care of it above.
  51. **/
  52. try {
  53. self._variables.setRoot(input);
  54. self.OE.addToDocument(out, input, self._variables);
  55. self._variables.clearRoot();
  56. } catch (ex){
  57. return callback(ex);
  58. }
  59. return callback(null, out);
  60. });
  61. return out;
  62. };
  63. /**
  64. * Optimizes the internal ObjectExpression
  65. * @return
  66. **/
  67. proto.optimize = function optimize() {
  68. this.OE = this.OE.optimize();
  69. };
  70. proto.serialize = function serialize(explain) {
  71. var out = {};
  72. out[this.getSourceName()] = this.OE.serialize(explain);
  73. return out;
  74. };
  75. /**
  76. * Builds a new ProjectDocumentSource from an object
  77. * @method createFromJson
  78. * @return {ProjectDocmentSource} a ProjectDocumentSource instance
  79. **/
  80. klass.createFromJson = function(elem, expCtx) {
  81. if (!(elem instanceof Object) || elem.constructor !== Object) throw new Error('Error 15969. Specification must be an object but was ' + typeof elem);
  82. var objectContext = new Expression.ObjectCtx({
  83. isDocumentOk: true,
  84. isTopLevel: true,
  85. isInclusionOk: true
  86. });
  87. var idGenerator = new VariablesIdGenerator(),
  88. vps = new VariablesParseState(idGenerator),
  89. parsed = Expression.parseObject(elem, objectContext, vps),
  90. exprObj = parsed;
  91. if (!exprObj instanceof ObjectExpression) throw new Error("16402, parseObject() returned wrong type of Expression");
  92. //if (!exprObj.getFieldCount() ) throw new Error("uassert 16403: $project requires at least one output field");
  93. var project = new ProjectDocumentSource(expCtx, exprObj);
  94. project._variables = new Variables(idGenerator.getIdCount());
  95. var projectObj = elem;
  96. project.OE = exprObj;
  97. project._raw = elem;
  98. return project;
  99. };
  100. /**
  101. * Adds dependencies to the contained ObjectExpression
  102. * @param {deps} An object that is treated as a set of strings
  103. * @return A string that is part of the GetDepsReturn enum
  104. **/
  105. proto.getDependencies = function getDependencies(deps) {
  106. var path = [];
  107. this.OE.addDependencies(deps, path);
  108. return base.GetDepsReturn.EXHAUSTIVE_FIELDS;
  109. };
  110. /**
  111. * Returns the object that was used to construct the ProjectDocumentSource
  112. * @return {object} the object that was used to construct the ProjectDocumentSource
  113. **/
  114. proto.getRaw = function getRaw() {
  115. return this._raw;
  116. };