ProjectDocumentSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var ProjectDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A base class for filter document sources
  5. *
  6. * @class ProjectDocumentSource
  7. * @namespace munge.pipepline.documentsource
  8. * @module munge
  9. * @constructor
  10. * @param {ExpressionContext}
  11. **/
  12. var klass = module.exports = ProjectDocumentSource = function ProjectDocumentSource(/*pCtx*/){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  15. this.EO = new ObjectExpression();
  16. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  17. // DEPENDENCIES
  18. var ObjectExpression = require('../expressions/ObjectExpression');
  19. var Value = require('../Value');
  20. var Expression = require('../expressions/Expression');
  21. /**
  22. * Returns the name of project
  23. *
  24. * @return {string} the name of project
  25. **/
  26. klass.projectName = "$project";
  27. proto.getSourceName = function getSourceName() {
  28. return klass.projectName;
  29. };
  30. /**
  31. * Calls base document source eof()
  32. *
  33. * @return {bool} The result of base.pSource.eof()
  34. **/
  35. proto.eof = function eof() {
  36. return this.pSource.eof();
  37. };
  38. /**
  39. * Calls base document source advance()
  40. *
  41. * @return {bool} The result of base.pSource.advance()
  42. **/
  43. proto.advance = function advance() {
  44. return this.pSource.advance();
  45. };
  46. /**
  47. * Builds a new document(object) that represents this base document
  48. *
  49. * @return A document that represents this base document
  50. **/
  51. proto.getCurrent = function getCurrent() {
  52. var inDocument = this.pSource.getCurrent();
  53. if (!inDocument) {
  54. throw new Error('inDocument must be an object');
  55. }
  56. var resultDocument = {};
  57. this.EO.addToDocument(resultDocument, inDocument, /*root=*/inDocument);
  58. return resultDocument;
  59. };
  60. /**
  61. * Optimizes the internal ObjectExpression
  62. *
  63. * @return
  64. **/
  65. proto.optimize = function optimize() {
  66. this.EO.optimize();
  67. };
  68. /**
  69. * Places a $project key inside the builder object with value of this.EO
  70. *
  71. * @method sourceToJson
  72. * @param {builder} An object (was ported from BsonBuilder)
  73. * @return
  74. **/
  75. proto.sourceToJson = function sourceToJson(builder, explain) {
  76. var insides = {};
  77. this.EO.documentToJSON(insides, true);
  78. builder[this.getSourceName()] = insides;
  79. };
  80. /**
  81. * Builds a new ProjectDocumentSource from an object
  82. *
  83. * @method createFromJson
  84. * @return a ProjectDocumentSource instance
  85. **/
  86. klass.createFromJson = function(jsonElement, expCtx) {
  87. if(jsonElement instanceof Object && jsonElement.constructor === Object) {
  88. throw new Error('Error 15969. Specification must be an object but was ' + typeof jsonElement);
  89. }
  90. var objectContext = Expression.ObjectCtx({
  91. isDocumentOk:true,
  92. isTopLevel:true,
  93. isInclusionOk:true
  94. });
  95. var project = new ProjectDocumentSource();
  96. var parsed = Expression.parseObject(jsonElement, objectContext);
  97. var exprObj = parsed;
  98. if(! exprObj instanceof ObjectExpression) {
  99. throw new Error("16402, parseObject() returned wrong type of Expression");
  100. }
  101. if(!exprObj.getFieldCount()) {
  102. throw new Error("16403, $projection requires at least one output field");
  103. }
  104. project.EO = exprObj;
  105. return project;
  106. };
  107. /**
  108. * Adds dependencies to the contained ObjectExpression
  109. *
  110. * @param {deps} An object that is treated as a set of strings
  111. * @return A string that is part of the GetDepsReturn enum
  112. **/
  113. proto.getDependencies = function getDependencies(deps) {
  114. var path = [];
  115. this.EO.addDependencies(deps, path);
  116. return base.GetDepsReturn.EXHAUSTIVE;
  117. };
  118. return klass;
  119. })();