ProjectDocumentSource.js 4.2 KB

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