Updated mongoose

This commit is contained in:
Dobie Wollert
2015-11-24 22:08:58 -08:00
parent 71a05fb732
commit 8b5827c970
618 changed files with 122299 additions and 37754 deletions

View File

@ -1,12 +1,13 @@
/* eslint no-empty: 1 */
/*!
* Module dependencies.
*/
var Stream = require('stream').Stream
var utils = require('./utils')
var helpers = require('./queryhelpers')
var K = function(k){ return k }
var Stream = require('stream').Stream;
var utils = require('./utils');
var helpers = require('./queryhelpers');
var K = function(k) { return k; };
/**
* Provides a Node.js 0.8 style [ReadStream](http://nodejs.org/docs/v0.8.21/api/stream.html#stream_readable_stream) interface for Queries.
@ -49,7 +50,7 @@ var K = function(k){ return k }
* @api public
*/
function QueryStream (query, options) {
function QueryStream(query, options) {
Stream.call(this);
this.query = query;
@ -67,7 +68,7 @@ function QueryStream (query, options) {
// give time to hook up events
var self = this;
process.nextTick(function () {
process.nextTick(function() {
self._init();
});
}
@ -107,13 +108,13 @@ var T_CONT = 2;
* @api private
*/
QueryStream.prototype._init = function () {
QueryStream.prototype._init = function() {
if (this._destroyed) return;
var query = this.query
, model = query.model
, options = query._optionsForExec(model)
, self = this
var query = this.query,
model = query.model,
options = query._optionsForExec(model),
self = this;
try {
query.cast(model);
@ -124,12 +125,12 @@ QueryStream.prototype._init = function () {
self._fields = utils.clone(query._fields);
options.fields = query._castFields(self._fields);
model.collection.find(query._conditions, options, function (err, cursor) {
model.collection.find(query._conditions, options, function(err, cursor) {
if (err) return self.destroy(err);
self._cursor = cursor;
self._next();
});
}
};
/**
* Trampoline for pulling the next doc from cursor.
@ -138,7 +139,7 @@ QueryStream.prototype._init = function () {
* @api private
*/
QueryStream.prototype._next = function _next () {
QueryStream.prototype._next = function _next() {
if (this.paused || this._destroyed) {
return this._running = false;
}
@ -155,7 +156,7 @@ QueryStream.prototype._next = function _next () {
// avoid stack overflows with large result sets.
// trampoline instead of recursion.
while (this.__next()) {}
}
};
/**
* Pulls the next doc from the cursor.
@ -164,14 +165,14 @@ QueryStream.prototype._next = function _next () {
* @api private
*/
QueryStream.prototype.__next = function () {
QueryStream.prototype.__next = function() {
if (this.paused || this._destroyed)
return this._running = false;
var self = this;
self._inline = T_INIT;
self._cursor.nextObject(function cursorcb (err, doc) {
self._cursor.nextObject(function cursorcb(err, doc) {
self._onNextObject(err, doc);
});
@ -185,7 +186,7 @@ QueryStream.prototype.__next = function () {
// the trampoline anymore.
this._inline = T_IDLE;
}
}
};
/**
* Transforms raw `doc`s returned from the cursor into a model instance.
@ -195,7 +196,7 @@ QueryStream.prototype.__next = function () {
* @api private
*/
QueryStream.prototype._onNextObject = function _onNextObject (err, doc) {
QueryStream.prototype._onNextObject = function _onNextObject(err, doc) {
if (this._destroyed) return;
if (this.paused) {
@ -212,28 +213,37 @@ QueryStream.prototype._onNextObject = function _onNextObject (err, doc) {
return this.destroy();
}
var opts = this.query.options;
var opts = this.query._mongooseOptions;
if (!opts.populate) {
return true === opts.lean
? emit(this, doc)
: createAndEmit(this, doc);
return true === opts.lean ?
emit(this, doc) :
createAndEmit(this, null, doc);
}
var self = this;
var pop = helpers.preparePopulationOptions(self.query, self.query.options);
var pop = helpers.preparePopulationOptionsMQ(self.query, self.query._mongooseOptions);
self.query.model.populate(doc, pop, function (err, doc) {
// Hack to work around gh-3108
pop.forEach(function(option) {
delete option.model;
});
self.query.model.populate(doc, pop, function(err, doc) {
if (err) return self.destroy(err);
return true === opts.lean
? emit(self, doc)
: createAndEmit(self, doc);
})
}
return true === opts.lean ?
emit(self, doc) :
createAndEmit(self, pop, doc);
});
};
function createAndEmit (self, doc) {
var instance = new self.query.model(undefined, self._fields, true);
instance.init(doc, function (err) {
function createAndEmit(self, populatedIds, doc) {
var instance = helpers.createModel(self.query.model, doc, self._fields);
var opts = populatedIds ?
{ populated: populatedIds } :
undefined;
instance.init(doc, opts, function(err) {
if (err) return self.destroy(err);
emit(self, instance);
});
@ -243,7 +253,7 @@ function createAndEmit (self, doc) {
* Emit a data event and manage the trampoline state
*/
function emit (self, doc) {
function emit(self, doc) {
self.emit('data', self._transform(doc));
// trampoline management
@ -263,9 +273,9 @@ function emit (self, doc) {
* @api public
*/
QueryStream.prototype.pause = function () {
QueryStream.prototype.pause = function() {
this.paused = true;
}
};
/**
* Resumes this stream.
@ -273,7 +283,7 @@ QueryStream.prototype.pause = function () {
* @api public
*/
QueryStream.prototype.resume = function () {
QueryStream.prototype.resume = function() {
this.paused = false;
if (!this._cursor) {
@ -290,7 +300,7 @@ QueryStream.prototype.resume = function () {
// outside QueryStream control, need manual restart
return this._next();
}
}
};
/**
* Destroys the stream, closing the underlying cursor. No more events will be emitted.
@ -299,7 +309,7 @@ QueryStream.prototype.resume = function () {
* @api public
*/
QueryStream.prototype.destroy = function (err) {
QueryStream.prototype.destroy = function(err) {
if (this._destroyed) return;
this._destroyed = true;
this._running = false;
@ -314,7 +324,7 @@ QueryStream.prototype.destroy = function (err) {
}
this.emit('close');
}
};
/**
* Pipes this query stream into another stream. This method is inherited from NodeJS Streams.