123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
-
-
- 'use strict';
-
- const Readable = require('stream').Readable;
- const eachAsync = require('../helpers/cursor/eachAsync');
- const helpers = require('../queryhelpers');
- const util = require('util');
- const utils = require('../utils');
-
-
-
- function QueryCursor(query, options) {
- Readable.call(this, { objectMode: true });
-
- this.cursor = null;
- this.query = query;
- const _this = this;
- const model = query.model;
- this._mongooseOptions = {};
- this._transforms = [];
- this.model = model;
- model.hooks.execPre('find', query, () => {
- this._transforms = this._transforms.concat(query._transforms.slice());
- if (options.transform) {
- this._transforms.push(options.transform);
- }
- model.collection.find(query._conditions, options, function(err, cursor) {
- if (_this._error) {
- cursor.close(function() {});
- _this.listeners('error').length > 0 && _this.emit('error', _this._error);
- }
- if (err) {
- return _this.emit('error', err);
- }
- _this.cursor = cursor;
- _this.emit('cursor', cursor);
- });
- });
- }
-
- util.inherits(QueryCursor, Readable);
-
-
-
- QueryCursor.prototype._read = function() {
- const _this = this;
- _next(this, function(error, doc) {
- if (error) {
- return _this.emit('error', error);
- }
- if (!doc) {
- _this.push(null);
- _this.cursor.close(function(error) {
- if (error) {
- return _this.emit('error', error);
- }
- setTimeout(function() {
- _this.emit('close');
- }, 0);
- });
- return;
- }
- _this.push(doc);
- });
- };
-
-
-
- QueryCursor.prototype.map = function(fn) {
- this._transforms.push(fn);
- return this;
- };
-
-
-
- QueryCursor.prototype._markError = function(error) {
- this._error = error;
- return this;
- };
-
-
-
- QueryCursor.prototype.close = function(callback) {
- return utils.promiseOrCallback(callback, cb => {
- this.cursor.close(error => {
- if (error) {
- cb(error);
- return this.listeners('error').length > 0 && this.emit('error', error);
- }
- this.emit('close');
- cb(null);
- });
- }, this.model.events);
- };
-
-
-
- QueryCursor.prototype.next = function(callback) {
- return utils.promiseOrCallback(callback, cb => {
- _next(this, function(error, doc) {
- if (error) {
- return cb(error);
- }
- cb(null, doc);
- });
- }, this.model.events);
- };
-
-
-
- QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
- const _this = this;
- if (typeof opts === 'function') {
- callback = opts;
- opts = {};
- }
- opts = opts || {};
-
- return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback);
- };
-
-
-
- QueryCursor.prototype.addCursorFlag = function(flag, value) {
- const _this = this;
- _waitForCursor(this, function() {
- _this.cursor.addCursorFlag(flag, value);
- });
- return this;
- };
-
-
-
- QueryCursor.prototype.transformNull = function(val) {
- if (arguments.length === 0) {
- val = true;
- }
- this._mongooseOptions.transformNull = val;
- return this;
- };
-
-
-
- function _next(ctx, cb) {
- let callback = cb;
- if (ctx._transforms.length) {
- callback = function(err, doc) {
- if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
- return cb(err, doc);
- }
- cb(err, ctx._transforms.reduce(function(doc, fn) {
- return fn.call(ctx, doc);
- }, doc));
- };
- }
-
- if (ctx._error) {
- return process.nextTick(function() {
- callback(ctx._error);
- });
- }
-
- if (ctx.cursor) {
- return ctx.cursor.next(function(error, doc) {
- if (error) {
- return callback(error);
- }
- if (!doc) {
- return callback(null, null);
- }
-
- const opts = ctx.query._mongooseOptions;
- if (!opts.populate) {
- return opts.lean ?
- callback(null, doc) :
- _create(ctx, doc, null, callback);
- }
-
- const pop = helpers.preparePopulationOptionsMQ(ctx.query,
- ctx.query._mongooseOptions);
- pop.__noPromise = true;
- ctx.query.model.populate(doc, pop, function(err, doc) {
- if (err) {
- return callback(err);
- }
- return opts.lean ?
- callback(null, doc) :
- _create(ctx, doc, pop, callback);
- });
- });
- } else {
- ctx.once('cursor', function() {
- _next(ctx, cb);
- });
- }
- }
-
-
-
- function _waitForCursor(ctx, cb) {
- if (ctx.cursor) {
- return cb();
- }
- ctx.once('cursor', function() {
- cb();
- });
- }
-
-
-
- function _create(ctx, doc, populatedIds, cb) {
- const instance = helpers.createModel(ctx.query.model, doc, ctx.query._fields);
- const opts = populatedIds ?
- { populated: populatedIds } :
- undefined;
-
- instance.init(doc, opts, function(err) {
- if (err) {
- return cb(err);
- }
- cb(null, instance);
- });
- }
-
- module.exports = QueryCursor;
|