QueryCursor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const Readable = require('stream').Readable;
  6. const promiseOrCallback = require('../helpers/promiseOrCallback');
  7. const eachAsync = require('../helpers/cursor/eachAsync');
  8. const helpers = require('../queryhelpers');
  9. const immediate = require('../helpers/immediate');
  10. const util = require('util');
  11. /**
  12. * A QueryCursor is a concurrency primitive for processing query results
  13. * one document at a time. A QueryCursor fulfills the Node.js streams3 API,
  14. * in addition to several other mechanisms for loading documents from MongoDB
  15. * one at a time.
  16. *
  17. * QueryCursors execute the model's pre `find` hooks before loading any documents
  18. * from MongoDB, and the model's post `find` hooks after loading each document.
  19. *
  20. * Unless you're an advanced user, do **not** instantiate this class directly.
  21. * Use [`Query#cursor()`](/docs/api.html#query_Query-cursor) instead.
  22. *
  23. * @param {Query} query
  24. * @param {Object} options query options passed to `.find()`
  25. * @inherits Readable
  26. * @event `cursor`: Emitted when the cursor is created
  27. * @event `error`: Emitted when an error occurred
  28. * @event `data`: Emitted when the stream is flowing and the next doc is ready
  29. * @event `end`: Emitted when the stream is exhausted
  30. * @api public
  31. */
  32. function QueryCursor(query, options) {
  33. // set autoDestroy=true because on node 12 it's by default false
  34. // gh-10902 need autoDestroy to destroy correctly and emit 'close' event
  35. Readable.call(this, { autoDestroy: true, objectMode: true });
  36. this.cursor = null;
  37. this.query = query;
  38. const _this = this;
  39. const model = query.model;
  40. this._mongooseOptions = {};
  41. this._transforms = [];
  42. this.model = model;
  43. this.options = options || {};
  44. model.hooks.execPre('find', query, (err) => {
  45. if (err != null) {
  46. _this._markError(err);
  47. _this.listeners('error').length > 0 && _this.emit('error', err);
  48. return;
  49. }
  50. this._transforms = this._transforms.concat(query._transforms.slice());
  51. if (this.options.transform) {
  52. this._transforms.push(options.transform);
  53. }
  54. // Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
  55. // `batchSize` option doesn't work.
  56. if (this.options.batchSize) {
  57. this.options.cursor = options.cursor || {};
  58. this.options.cursor.batchSize = options.batchSize;
  59. // Max out the number of documents we'll populate in parallel at 5000.
  60. this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
  61. }
  62. model.collection.find(query._conditions, this.options, (err, cursor) => {
  63. if (err != null) {
  64. _this._markError(err);
  65. _this.listeners('error').length > 0 && _this.emit('error', _this._error);
  66. return;
  67. }
  68. if (_this._error) {
  69. cursor.close(function() {});
  70. _this.listeners('error').length > 0 && _this.emit('error', _this._error);
  71. }
  72. _this.cursor = cursor;
  73. _this.emit('cursor', cursor);
  74. });
  75. });
  76. }
  77. util.inherits(QueryCursor, Readable);
  78. /*!
  79. * Necessary to satisfy the Readable API
  80. */
  81. QueryCursor.prototype._read = function() {
  82. const _this = this;
  83. _next(this, function(error, doc) {
  84. if (error) {
  85. return _this.emit('error', error);
  86. }
  87. if (!doc) {
  88. _this.push(null);
  89. _this.cursor.close(function(error) {
  90. if (error) {
  91. return _this.emit('error', error);
  92. }
  93. });
  94. return;
  95. }
  96. _this.push(doc);
  97. });
  98. };
  99. /**
  100. * Registers a transform function which subsequently maps documents retrieved
  101. * via the streams interface or `.next()`
  102. *
  103. * ####Example
  104. *
  105. * // Map documents returned by `data` events
  106. * Thing.
  107. * find({ name: /^hello/ }).
  108. * cursor().
  109. * map(function (doc) {
  110. * doc.foo = "bar";
  111. * return doc;
  112. * })
  113. * on('data', function(doc) { console.log(doc.foo); });
  114. *
  115. * // Or map documents returned by `.next()`
  116. * const cursor = Thing.find({ name: /^hello/ }).
  117. * cursor().
  118. * map(function (doc) {
  119. * doc.foo = "bar";
  120. * return doc;
  121. * });
  122. * cursor.next(function(error, doc) {
  123. * console.log(doc.foo);
  124. * });
  125. *
  126. * @param {Function} fn
  127. * @return {QueryCursor}
  128. * @api public
  129. * @method map
  130. */
  131. QueryCursor.prototype.map = function(fn) {
  132. this._transforms.push(fn);
  133. return this;
  134. };
  135. /*!
  136. * Marks this cursor as errored
  137. */
  138. QueryCursor.prototype._markError = function(error) {
  139. this._error = error;
  140. return this;
  141. };
  142. /**
  143. * Marks this cursor as closed. Will stop streaming and subsequent calls to
  144. * `next()` will error.
  145. *
  146. * @param {Function} callback
  147. * @return {Promise}
  148. * @api public
  149. * @method close
  150. * @emits close
  151. * @see MongoDB driver cursor#close http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
  152. */
  153. QueryCursor.prototype.close = function(callback) {
  154. return promiseOrCallback(callback, cb => {
  155. this.cursor.close(error => {
  156. if (error) {
  157. cb(error);
  158. return this.listeners('error').length > 0 && this.emit('error', error);
  159. }
  160. this.emit('close');
  161. cb(null);
  162. });
  163. }, this.model.events);
  164. };
  165. /**
  166. * Get the next document from this cursor. Will return `null` when there are
  167. * no documents left.
  168. *
  169. * @param {Function} callback
  170. * @return {Promise}
  171. * @api public
  172. * @method next
  173. */
  174. QueryCursor.prototype.next = function(callback) {
  175. return promiseOrCallback(callback, cb => {
  176. _next(this, function(error, doc) {
  177. if (error) {
  178. return cb(error);
  179. }
  180. cb(null, doc);
  181. });
  182. }, this.model.events);
  183. };
  184. /**
  185. * Execute `fn` for every document in the cursor. If `fn` returns a promise,
  186. * will wait for the promise to resolve before iterating on to the next one.
  187. * Returns a promise that resolves when done.
  188. *
  189. * ####Example
  190. *
  191. * // Iterate over documents asynchronously
  192. * Thing.
  193. * find({ name: /^hello/ }).
  194. * cursor().
  195. * eachAsync(async function (doc, i) {
  196. * doc.foo = doc.bar + i;
  197. * await doc.save();
  198. * })
  199. *
  200. * @param {Function} fn
  201. * @param {Object} [options]
  202. * @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1.
  203. * @param {Function} [callback] executed when all docs have been processed
  204. * @return {Promise}
  205. * @api public
  206. * @method eachAsync
  207. */
  208. QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
  209. const _this = this;
  210. if (typeof opts === 'function') {
  211. callback = opts;
  212. opts = {};
  213. }
  214. opts = opts || {};
  215. return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback);
  216. };
  217. /**
  218. * The `options` passed in to the `QueryCursor` constructor.
  219. *
  220. * @api public
  221. * @property options
  222. */
  223. QueryCursor.prototype.options;
  224. /**
  225. * Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
  226. * Useful for setting the `noCursorTimeout` and `tailable` flags.
  227. *
  228. * @param {String} flag
  229. * @param {Boolean} value
  230. * @return {AggregationCursor} this
  231. * @api public
  232. * @method addCursorFlag
  233. */
  234. QueryCursor.prototype.addCursorFlag = function(flag, value) {
  235. const _this = this;
  236. _waitForCursor(this, function() {
  237. _this.cursor.addCursorFlag(flag, value);
  238. });
  239. return this;
  240. };
  241. /*!
  242. * ignore
  243. */
  244. QueryCursor.prototype.transformNull = function(val) {
  245. if (arguments.length === 0) {
  246. val = true;
  247. }
  248. this._mongooseOptions.transformNull = val;
  249. return this;
  250. };
  251. /*!
  252. * ignore
  253. */
  254. QueryCursor.prototype._transformForAsyncIterator = function() {
  255. if (this._transforms.indexOf(_transformForAsyncIterator) === -1) {
  256. this.map(_transformForAsyncIterator);
  257. }
  258. return this;
  259. };
  260. /**
  261. * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js).
  262. * You do not need to call this function explicitly, the JavaScript runtime
  263. * will call it for you.
  264. *
  265. * ####Example
  266. *
  267. * // Works without using `cursor()`
  268. * for await (const doc of Model.find([{ $sort: { name: 1 } }])) {
  269. * console.log(doc.name);
  270. * }
  271. *
  272. * // Can also use `cursor()`
  273. * for await (const doc of Model.find([{ $sort: { name: 1 } }]).cursor()) {
  274. * console.log(doc.name);
  275. * }
  276. *
  277. * Node.js 10.x supports async iterators natively without any flags. You can
  278. * enable async iterators in Node.js 8.x using the [`--harmony_async_iteration` flag](https://github.com/tc39/proposal-async-iteration/issues/117#issuecomment-346695187).
  279. *
  280. * **Note:** This function is not if `Symbol.asyncIterator` is undefined. If
  281. * `Symbol.asyncIterator` is undefined, that means your Node.js version does not
  282. * support async iterators.
  283. *
  284. * @method Symbol.asyncIterator
  285. * @memberOf Query
  286. * @instance
  287. * @api public
  288. */
  289. if (Symbol.asyncIterator != null) {
  290. QueryCursor.prototype[Symbol.asyncIterator] = function() {
  291. return this.transformNull()._transformForAsyncIterator();
  292. };
  293. }
  294. /*!
  295. * ignore
  296. */
  297. function _transformForAsyncIterator(doc) {
  298. return doc == null ? { done: true } : { value: doc, done: false };
  299. }
  300. /*!
  301. * Get the next doc from the underlying cursor and mongooseify it
  302. * (populate, etc.)
  303. */
  304. function _next(ctx, cb) {
  305. let callback = cb;
  306. if (ctx._transforms.length) {
  307. callback = function(err, doc) {
  308. if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
  309. return cb(err, doc);
  310. }
  311. cb(err, ctx._transforms.reduce(function(doc, fn) {
  312. return fn.call(ctx, doc);
  313. }, doc));
  314. };
  315. }
  316. if (ctx._error) {
  317. return immediate(function() {
  318. callback(ctx._error);
  319. });
  320. }
  321. if (ctx.cursor) {
  322. if (ctx.query._mongooseOptions.populate && !ctx._pop) {
  323. ctx._pop = helpers.preparePopulationOptionsMQ(ctx.query,
  324. ctx.query._mongooseOptions);
  325. ctx._pop.__noPromise = true;
  326. }
  327. if (ctx.query._mongooseOptions.populate && ctx.options._populateBatchSize > 1) {
  328. if (ctx._batchDocs && ctx._batchDocs.length) {
  329. // Return a cached populated doc
  330. return _nextDoc(ctx, ctx._batchDocs.shift(), ctx._pop, callback);
  331. } else if (ctx._batchExhausted) {
  332. // Internal cursor reported no more docs. Act the same here
  333. return callback(null, null);
  334. } else {
  335. // Request as many docs as batchSize, to populate them also in batch
  336. ctx._batchDocs = [];
  337. return ctx.cursor.next(_onNext.bind({ ctx, callback }));
  338. }
  339. } else {
  340. return ctx.cursor.next(function(error, doc) {
  341. if (error) {
  342. return callback(error);
  343. }
  344. if (!doc) {
  345. return callback(null, null);
  346. }
  347. if (!ctx.query._mongooseOptions.populate) {
  348. return _nextDoc(ctx, doc, null, callback);
  349. }
  350. ctx.query.model.populate(doc, ctx._pop, function(err, doc) {
  351. if (err) {
  352. return callback(err);
  353. }
  354. return _nextDoc(ctx, doc, ctx._pop, callback);
  355. });
  356. });
  357. }
  358. } else {
  359. ctx.once('error', cb);
  360. ctx.once('cursor', function(cursor) {
  361. ctx.removeListener('error', cb);
  362. if (cursor == null) {
  363. return;
  364. }
  365. _next(ctx, cb);
  366. });
  367. }
  368. }
  369. /*!
  370. * ignore
  371. */
  372. function _onNext(error, doc) {
  373. if (error) {
  374. return this.callback(error);
  375. }
  376. if (!doc) {
  377. this.ctx._batchExhausted = true;
  378. return _populateBatch.call(this);
  379. }
  380. this.ctx._batchDocs.push(doc);
  381. if (this.ctx._batchDocs.length < this.ctx.options._populateBatchSize) {
  382. // If both `batchSize` and `_populateBatchSize` are huge, calling `next()` repeatedly may
  383. // cause a stack overflow. So make sure we clear the stack regularly.
  384. if (this.ctx._batchDocs.length > 0 && this.ctx._batchDocs.length % 1000 === 0) {
  385. return immediate(() => this.ctx.cursor.next(_onNext.bind(this)));
  386. }
  387. this.ctx.cursor.next(_onNext.bind(this));
  388. } else {
  389. _populateBatch.call(this);
  390. }
  391. }
  392. /*!
  393. * ignore
  394. */
  395. function _populateBatch() {
  396. if (!this.ctx._batchDocs.length) {
  397. return this.callback(null, null);
  398. }
  399. const _this = this;
  400. this.ctx.query.model.populate(this.ctx._batchDocs, this.ctx._pop, function(err) {
  401. if (err) {
  402. return _this.callback(err);
  403. }
  404. _nextDoc(_this.ctx, _this.ctx._batchDocs.shift(), _this.ctx._pop, _this.callback);
  405. });
  406. }
  407. /*!
  408. * ignore
  409. */
  410. function _nextDoc(ctx, doc, pop, callback) {
  411. if (ctx.query._mongooseOptions.lean) {
  412. return ctx.model.hooks.execPost('find', ctx.query, [[doc]], err => {
  413. if (err != null) {
  414. return callback(err);
  415. }
  416. callback(null, doc);
  417. });
  418. }
  419. ctx.query._completeOne(doc, null, (err, doc) => {
  420. if (err != null) {
  421. return callback(err);
  422. }
  423. ctx.model.hooks.execPost('find', ctx.query, [[doc]], err => {
  424. if (err != null) {
  425. return callback(err);
  426. }
  427. callback(null, doc);
  428. });
  429. });
  430. }
  431. /*!
  432. * ignore
  433. */
  434. function _waitForCursor(ctx, cb) {
  435. if (ctx.cursor) {
  436. return cb();
  437. }
  438. ctx.once('cursor', function(cursor) {
  439. if (cursor == null) {
  440. return;
  441. }
  442. cb();
  443. });
  444. }
  445. module.exports = QueryCursor;