|
| 1 | +/*! |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const MongooseError = require('../error/mongooseError'); |
| 8 | +const Readable = require('stream').Readable; |
| 9 | +const eachAsync = require('../helpers/cursor/eachAsync'); |
| 10 | +const immediate = require('../helpers/immediate'); |
| 11 | +const util = require('util'); |
| 12 | + |
| 13 | +/** |
| 14 | + * An AggregationCursor is a concurrency primitive for processing aggregation |
| 15 | + * results one document at a time. It is analogous to QueryCursor. |
| 16 | + * |
| 17 | + * An AggregationCursor fulfills the Node.js streams3 API, |
| 18 | + * in addition to several other mechanisms for loading documents from MongoDB |
| 19 | + * one at a time. |
| 20 | + * |
| 21 | + * Creating an AggregationCursor executes the model's pre aggregate hooks, |
| 22 | + * but **not** the model's post aggregate hooks. |
| 23 | + * |
| 24 | + * Unless you're an advanced user, do **not** instantiate this class directly. |
| 25 | + * Use [`Aggregate#cursor()`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.cursor()) instead. |
| 26 | + * |
| 27 | + * @param {Aggregate} agg |
| 28 | + * @inherits Readable https://nodejs.org/api/stream.html#class-streamreadable |
| 29 | + * @event `cursor`: Emitted when the cursor is created |
| 30 | + * @event `error`: Emitted when an error occurred |
| 31 | + * @event `data`: Emitted when the stream is flowing and the next doc is ready |
| 32 | + * @event `end`: Emitted when the stream is exhausted |
| 33 | + * @api public |
| 34 | + */ |
| 35 | + |
| 36 | +function AggregationCursor(agg) { |
| 37 | + // set autoDestroy=true because on node 12 it's by default false |
| 38 | + // gh-10902 need autoDestroy to destroy correctly and emit 'close' event |
| 39 | + Readable.call(this, { autoDestroy: true, objectMode: true }); |
| 40 | + |
| 41 | + this.cursor = null; |
| 42 | + this.agg = agg; |
| 43 | + this._transforms = []; |
| 44 | + const model = agg._model; |
| 45 | + delete agg.options.cursor.useMongooseAggCursor; |
| 46 | + this._mongooseOptions = {}; |
| 47 | + |
| 48 | + _init(model, this, agg); |
| 49 | +} |
| 50 | + |
| 51 | +util.inherits(AggregationCursor, Readable); |
| 52 | + |
| 53 | +/*! |
| 54 | + * ignore |
| 55 | + */ |
| 56 | + |
| 57 | +function _init(model, c, agg) { |
| 58 | + if (!model.collection.buffer) { |
| 59 | + model.hooks.execPre('aggregate', agg, function() { |
| 60 | + c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {}); |
| 61 | + c.emit('cursor', c.cursor); |
| 62 | + }); |
| 63 | + } else { |
| 64 | + model.collection.emitter.once('queue', function() { |
| 65 | + model.hooks.execPre('aggregate', agg, function() { |
| 66 | + c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {}); |
| 67 | + c.emit('cursor', c.cursor); |
| 68 | + }); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Necessary to satisfy the Readable API |
| 75 | + * @method _read |
| 76 | + * @memberOf AggregationCursor |
| 77 | + * @instance |
| 78 | + * @api private |
| 79 | + */ |
| 80 | + |
| 81 | +AggregationCursor.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 | +if (Symbol.asyncIterator != null) { |
| 101 | + const msg = 'Mongoose does not support using async iterators with an ' + |
| 102 | + 'existing aggregation cursor. See https://bit.ly/mongoose-async-iterate-aggregation'; |
| 103 | + |
| 104 | + AggregationCursor.prototype[Symbol.asyncIterator] = function() { |
| 105 | + throw new MongooseError(msg); |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Registers a transform function which subsequently maps documents retrieved |
| 111 | + * via the streams interface or `.next()` |
| 112 | + * |
| 113 | + * #### Example: |
| 114 | + * |
| 115 | + * // Map documents returned by `data` events |
| 116 | + * Thing. |
| 117 | + * find({ name: /^hello/ }). |
| 118 | + * cursor(). |
| 119 | + * map(function (doc) { |
| 120 | + * doc.foo = "bar"; |
| 121 | + * return doc; |
| 122 | + * }) |
| 123 | + * on('data', function(doc) { console.log(doc.foo); }); |
| 124 | + * |
| 125 | + * // Or map documents returned by `.next()` |
| 126 | + * const cursor = Thing.find({ name: /^hello/ }). |
| 127 | + * cursor(). |
| 128 | + * map(function (doc) { |
| 129 | + * doc.foo = "bar"; |
| 130 | + * return doc; |
| 131 | + * }); |
| 132 | + * cursor.next(function(error, doc) { |
| 133 | + * console.log(doc.foo); |
| 134 | + * }); |
| 135 | + * |
| 136 | + * @param {Function} fn |
| 137 | + * @return {AggregationCursor} |
| 138 | + * @memberOf AggregationCursor |
| 139 | + * @api public |
| 140 | + * @method map |
| 141 | + */ |
| 142 | + |
| 143 | +Object.defineProperty(AggregationCursor.prototype, 'map', { |
| 144 | + value: function(fn) { |
| 145 | + this._transforms.push(fn); |
| 146 | + return this; |
| 147 | + }, |
| 148 | + enumerable: true, |
| 149 | + configurable: true, |
| 150 | + writable: true |
| 151 | +}); |
| 152 | + |
| 153 | +/** |
| 154 | + * Marks this cursor as errored |
| 155 | + * @method _markError |
| 156 | + * @instance |
| 157 | + * @memberOf AggregationCursor |
| 158 | + * @api private |
| 159 | + */ |
| 160 | + |
| 161 | +AggregationCursor.prototype._markError = function(error) { |
| 162 | + this._error = error; |
| 163 | + return this; |
| 164 | +}; |
| 165 | + |
| 166 | +/** |
| 167 | + * Marks this cursor as closed. Will stop streaming and subsequent calls to |
| 168 | + * `next()` will error. |
| 169 | + * |
| 170 | + * @param {Function} callback |
| 171 | + * @return {Promise} |
| 172 | + * @api public |
| 173 | + * @method close |
| 174 | + * @emits close |
| 175 | + * @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close |
| 176 | + */ |
| 177 | + |
| 178 | +AggregationCursor.prototype.close = async function close() { |
| 179 | + if (typeof arguments[0] === 'function') { |
| 180 | + throw new MongooseError('AggregationCursor.prototype.close() no longer accepts a callback'); |
| 181 | + } |
| 182 | + try { |
| 183 | + await this.cursor.close(); |
| 184 | + } catch (error) { |
| 185 | + this.listeners('error').length > 0 && this.emit('error', error); |
| 186 | + throw error; |
| 187 | + } |
| 188 | + this.emit('close'); |
| 189 | +}; |
| 190 | + |
| 191 | +/** |
| 192 | + * Get the next document from this cursor. Will return `null` when there are |
| 193 | + * no documents left. |
| 194 | + * |
| 195 | + * @return {Promise} |
| 196 | + * @api public |
| 197 | + * @method next |
| 198 | + */ |
| 199 | + |
| 200 | +AggregationCursor.prototype.next = async function next() { |
| 201 | + if (typeof arguments[0] === 'function') { |
| 202 | + throw new MongooseError('AggregationCursor.prototype.next() no longer accepts a callback'); |
| 203 | + } |
| 204 | + return new Promise((resolve, reject) => { |
| 205 | + _next(this, (err, res) => { |
| 206 | + if (err != null) { |
| 207 | + return reject(err); |
| 208 | + } |
| 209 | + resolve(res); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}; |
| 213 | + |
| 214 | +/** |
| 215 | + * Execute `fn` for every document in the cursor. If `fn` returns a promise, |
| 216 | + * will wait for the promise to resolve before iterating on to the next one. |
| 217 | + * Returns a promise that resolves when done. |
| 218 | + * |
| 219 | + * @param {Function} fn |
| 220 | + * @param {Object} [options] |
| 221 | + * @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1. |
| 222 | + * @param {Function} [callback] executed when all docs have been processed |
| 223 | + * @return {Promise} |
| 224 | + * @api public |
| 225 | + * @method eachAsync |
| 226 | + */ |
| 227 | + |
| 228 | +AggregationCursor.prototype.eachAsync = function(fn, opts, callback) { |
| 229 | + const _this = this; |
| 230 | + if (typeof opts === 'function') { |
| 231 | + callback = opts; |
| 232 | + opts = {}; |
| 233 | + } |
| 234 | + opts = opts || {}; |
| 235 | + |
| 236 | + return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback); |
| 237 | +}; |
| 238 | + |
| 239 | +/** |
| 240 | + * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js) |
| 241 | + * You do not need to call this function explicitly, the JavaScript runtime |
| 242 | + * will call it for you. |
| 243 | + * |
| 244 | + * #### Example: |
| 245 | + * |
| 246 | + * // Async iterator without explicitly calling `cursor()`. Mongoose still |
| 247 | + * // creates an AggregationCursor instance internally. |
| 248 | + * const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]); |
| 249 | + * for await (const doc of agg) { |
| 250 | + * console.log(doc.name); |
| 251 | + * } |
| 252 | + * |
| 253 | + * // You can also use an AggregationCursor instance for async iteration |
| 254 | + * const cursor = Model.aggregate([{ $match: { age: { $gte: 25 } } }]).cursor(); |
| 255 | + * for await (const doc of cursor) { |
| 256 | + * console.log(doc.name); |
| 257 | + * } |
| 258 | + * |
| 259 | + * Node.js 10.x supports async iterators natively without any flags. You can |
| 260 | + * 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). |
| 261 | + * |
| 262 | + * **Note:** This function is not set if `Symbol.asyncIterator` is undefined. If |
| 263 | + * `Symbol.asyncIterator` is undefined, that means your Node.js version does not |
| 264 | + * support async iterators. |
| 265 | + * |
| 266 | + * @method [Symbol.asyncIterator] |
| 267 | + * @memberOf AggregationCursor |
| 268 | + * @instance |
| 269 | + * @api public |
| 270 | + */ |
| 271 | + |
| 272 | +if (Symbol.asyncIterator != null) { |
| 273 | + AggregationCursor.prototype[Symbol.asyncIterator] = function() { |
| 274 | + return this.transformNull()._transformForAsyncIterator(); |
| 275 | + }; |
| 276 | +} |
| 277 | + |
| 278 | +/*! |
| 279 | + * ignore |
| 280 | + */ |
| 281 | + |
| 282 | +AggregationCursor.prototype._transformForAsyncIterator = function() { |
| 283 | + if (this._transforms.indexOf(_transformForAsyncIterator) === -1) { |
| 284 | + this.map(_transformForAsyncIterator); |
| 285 | + } |
| 286 | + return this; |
| 287 | +}; |
| 288 | + |
| 289 | +/*! |
| 290 | + * ignore |
| 291 | + */ |
| 292 | + |
| 293 | +AggregationCursor.prototype.transformNull = function(val) { |
| 294 | + if (arguments.length === 0) { |
| 295 | + val = true; |
| 296 | + } |
| 297 | + this._mongooseOptions.transformNull = val; |
| 298 | + return this; |
| 299 | +}; |
| 300 | + |
| 301 | +/*! |
| 302 | + * ignore |
| 303 | + */ |
| 304 | + |
| 305 | +function _transformForAsyncIterator(doc) { |
| 306 | + return doc == null ? { done: true } : { value: doc, done: false }; |
| 307 | +} |
| 308 | + |
| 309 | +/** |
| 310 | + * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#addCursorFlag). |
| 311 | + * Useful for setting the `noCursorTimeout` and `tailable` flags. |
| 312 | + * |
| 313 | + * @param {String} flag |
| 314 | + * @param {Boolean} value |
| 315 | + * @return {AggregationCursor} this |
| 316 | + * @api public |
| 317 | + * @method addCursorFlag |
| 318 | + */ |
| 319 | + |
| 320 | +AggregationCursor.prototype.addCursorFlag = function(flag, value) { |
| 321 | + const _this = this; |
| 322 | + _waitForCursor(this, function() { |
| 323 | + _this.cursor.addCursorFlag(flag, value); |
| 324 | + }); |
| 325 | + return this; |
| 326 | +}; |
| 327 | + |
| 328 | +/*! |
| 329 | + * ignore |
| 330 | + */ |
| 331 | + |
| 332 | +function _waitForCursor(ctx, cb) { |
| 333 | + if (ctx.cursor) { |
| 334 | + return cb(); |
| 335 | + } |
| 336 | + ctx.once('cursor', function() { |
| 337 | + cb(); |
| 338 | + }); |
| 339 | +} |
| 340 | + |
| 341 | +/** |
| 342 | + * Get the next doc from the underlying cursor and mongooseify it |
| 343 | + * (populate, etc.) |
| 344 | + * @param {Any} ctx |
| 345 | + * @param {Function} cb |
| 346 | + * @api private |
| 347 | + */ |
| 348 | + |
| 349 | +function _next(ctx, cb) { |
| 350 | + let callback = cb; |
| 351 | + if (ctx._transforms.length) { |
| 352 | + callback = function(err, doc) { |
| 353 | + if (err || (doc === null && !ctx._mongooseOptions.transformNull)) { |
| 354 | + return cb(err, doc); |
| 355 | + } |
| 356 | + cb(err, ctx._transforms.reduce(function(doc, fn) { |
| 357 | + return fn(doc); |
| 358 | + }, doc)); |
| 359 | + }; |
| 360 | + } |
| 361 | + |
| 362 | + if (ctx._error) { |
| 363 | + return immediate(function() { |
| 364 | + callback(ctx._error); |
| 365 | + }); |
| 366 | + } |
| 367 | + |
| 368 | + if (ctx.cursor) { |
| 369 | + return ctx.cursor.next().then( |
| 370 | + doc => { |
| 371 | + if (!doc) { |
| 372 | + return callback(null, null); |
| 373 | + } |
| 374 | + |
| 375 | + callback(null, doc); |
| 376 | + }, |
| 377 | + err => callback(err) |
| 378 | + ); |
| 379 | + } else { |
| 380 | + ctx.once('cursor', function() { |
| 381 | + _next(ctx, cb); |
| 382 | + }); |
| 383 | + } |
| 384 | +} |
| 385 | + |
| 386 | +module.exports = AggregationCursor; |
0 commit comments