OutDocumentSource.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var DocumentSource = require('./DocumentSource');
  3. /**
  4. * @class OutDocumentSource
  5. * @namespace mungedb-aggregate.pipeline.documentSources
  6. * @module mungedb-aggregate
  7. * @constructor
  8. * @param [ctx] {ExpressionContext}
  9. **/
  10. var OutDocumentSource = module.exports = function OutDocumentSource(outputNs, ctx){
  11. if (arguments.length > 2) throw new Error("up to two arg expected");
  12. base.call(this, ctx);
  13. // defaults
  14. this._done = false;
  15. this._outputNs = outputNs;
  16. this._collectionName = "";
  17. }, klass = OutDocumentSource, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. klass.outName = "$out";
  19. proto.getSourceName = function() {
  20. return klass.outName;
  21. };
  22. proto.getNext = function(callback) {
  23. if (!callback) throw new Error(this.getSourceName() + ' #getNext() requires callback');
  24. return this.source.getNext(callback);
  25. };
  26. proto.serialize = function(explain) {
  27. var doc = {},
  28. srcNm = this.getSourceName();
  29. doc[srcNm] = this._collectionName;
  30. return doc;
  31. };
  32. proto.getOutputNs = function() {
  33. return this._outputNs;
  34. };
  35. klass.createFromJson = function(jsonElement, ctx) {
  36. if (typeof jsonElement !== 'string')
  37. throw new Error('code 16990; $out only supports a string argument, not ' + typeof jsonElement);
  38. var out = new OutDocumentSource(null, ctx); // TODO: outputNs
  39. out._collectionName = jsonElement;
  40. return out;
  41. };
  42. proto.getDependencies = function(deps) {
  43. deps.needWholeDocument = true;
  44. return DocumentSource.GetDepsReturn.EXHAUSTIVE;
  45. };