ProjectDocumentSource.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.base.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.base.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 = base.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. builder[this.projectName] = this.EO;
  77. };
  78. /**
  79. * Builds a new ProjectDocumentSource from an object
  80. *
  81. * @method createFromJson
  82. * @return a ProjectDocumentSource instance
  83. **/
  84. klass.createFromJson = function(jsonElement, expCtx) {
  85. if(! jsonElement instanceof Object) {
  86. throw new Error('Error 15969. Specification must be an object but was ' + typeof jsonElement);
  87. }
  88. var objectContext = Expression.ObjectCtx({
  89. isDocumentOk:true,
  90. isTopLevel:true,
  91. isInclusionOk:true
  92. });
  93. var project = new ProjectDocumentSource();
  94. var parsed = Expression.parseObject(jsonElement, objectContext);
  95. var exprObj = parsed;
  96. if(!exprObj) {
  97. throw new Error("16402, parseObject() returned wrong type of Expression");
  98. }
  99. if(!exprObj.getFieldCount()) {
  100. throw new Error("16403, $projection requires at least one output field");
  101. }
  102. project.EO = exprObj;
  103. return project;
  104. };
  105. /**
  106. * Adds dependencies to the contained ObjectExpression
  107. *
  108. * @param {deps} An object that is treated as a set of strings
  109. * @return A string that is part of the GetDepsReturn enum
  110. **/
  111. proto.getDependencies = function getDependencies(deps) {
  112. var path = [];
  113. this.EO.addDependencies(deps, path);
  114. return base.GetDepsReturn.EXHAUSTIVE;
  115. };
  116. return klass;
  117. })();