QueryCursor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. Readable.call(this, { objectMode: true });
  34. this.cursor = null;
  35. this.query = query;
  36. const _this = this;
  37. const model = query.model;
  38. this._mongooseOptions = {};
  39. this._transforms = [];
  40. this.model = model;
  41. this.options = options || {};
  42. model.hooks.execPre('find', query, () => {
  43. this._transforms = this._transforms.concat(query._transforms.slice());
  44. if (this.options.transform) {
  45. this._transforms.push(options.transform);
  46. }
  47. // Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
  48. // `batchSize` option doesn't work.
  49. if (this.options.batchSize) {
  50. this.options.cursor = options.cursor || {};
  51. this.options.cursor.batchSize = options.batchSize;
  52. }
  53. model.collection.find(query._conditions, this.options, function(err, cursor) {
  54. if (_this._error) {
  55. if (cursor != null) {
  56. cursor.close(function() {});
  57. }
  58. _this.emit('cursor', null);
  59. _this.listeners('error').length > 0 && _this.emit('error', _this._error);
  60. return;
  61. }
  62. if (err) {
  63. return _this.emit('error', err);
  64. }
  65. _this.cursor = cursor;
  66. _this.emit('cursor', cursor);
  67. });
  68. });
  69. }
  70. util.inherits(QueryCursor, Readable);
  71. /*!
  72. * Necessary to satisfy the Readable API
  73. */
  74. QueryCursor.prototype._read = function() {
  75. const _this = this;
  76. _next(this, function(error, doc) {
  77. if (error) {
  78. return _this.emit('error', error);
  79. }
  80. if (!doc) {
  81. _this.push(null);
  82. _this.cursor.close(function(error) {
  83. if (error) {
  84. return _this.emit('error', error);
  85. }
  86. setTimeout(function() {
  87. // on node >= 14 streams close automatically (gh-8834)
  88. const isNotClosedAutomatically = !_this.destroyed;
  89. if (isNotClosedAutomatically) {
  90. _this.emit('close');
  91. }
  92. }, 0);
  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.batchSize > 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('cursor', function(cursor) {
  360. if (cursor == null) {
  361. return;
  362. }
  363. _next(ctx, cb);
  364. });
  365. }
  366. }
  367. /*!
  368. * ignore
  369. */
  370. function _onNext(error, doc) {
  371. if (error) {
  372. return this.callback(error);
  373. }
  374. if (!doc) {
  375. this.ctx._batchExhausted = true;
  376. return _populateBatch.call(this);
  377. }
  378. this.ctx._batchDocs.push(doc);
  379. if (this.ctx._batchDocs.length < this.ctx.options.batchSize) {
  380. this.ctx.cursor.next(_onNext.bind(this));
  381. } else {
  382. _populateBatch.call(this);
  383. }
  384. }
  385. /*!
  386. * ignore
  387. */
  388. function _populateBatch() {
  389. if (!this.ctx._batchDocs.length) {
  390. return this.callback(null, null);
  391. }
  392. const _this = this;
  393. this.ctx.query.model.populate(this.ctx._batchDocs, this.ctx._pop, function(err) {
  394. if (err) {
  395. return _this.callback(err);
  396. }
  397. _nextDoc(_this.ctx, _this.ctx._batchDocs.shift(), _this.ctx._pop, _this.callback);
  398. });
  399. }
  400. /*!
  401. * ignore
  402. */
  403. function _nextDoc(ctx, doc, pop, callback) {
  404. if (ctx.query._mongooseOptions.lean) {
  405. return ctx.model.hooks.execPost('find', ctx.query, [[doc]], err => {
  406. if (err != null) {
  407. return callback(err);
  408. }
  409. callback(null, doc);
  410. });
  411. }
  412. _create(ctx, doc, pop, (err, doc) => {
  413. if (err != null) {
  414. return callback(err);
  415. }
  416. ctx.model.hooks.execPost('find', ctx.query, [[doc]], err => {
  417. if (err != null) {
  418. return callback(err);
  419. }
  420. callback(null, doc);
  421. });
  422. });
  423. }
  424. /*!
  425. * ignore
  426. */
  427. function _waitForCursor(ctx, cb) {
  428. if (ctx.cursor) {
  429. return cb();
  430. }
  431. ctx.once('cursor', function(cursor) {
  432. if (cursor == null) {
  433. return;
  434. }
  435. cb();
  436. });
  437. }
  438. /*!
  439. * Convert a raw doc into a full mongoose doc.
  440. */
  441. function _create(ctx, doc, populatedIds, cb) {
  442. const instance = helpers.createModel(ctx.query.model, doc, ctx.query._fields);
  443. const opts = populatedIds ?
  444. { populated: populatedIds } :
  445. undefined;
  446. instance.init(doc, opts, function(err) {
  447. if (err) {
  448. return cb(err);
  449. }
  450. cb(null, instance);
  451. });
  452. }
  453. module.exports = QueryCursor;