ProjectDocumentSource.js 3.8 KB

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