forked from Automattic/mongoose
/
querystream.js
184 lines (145 loc) · 3.39 KB
/
querystream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Module dependencies.
*/
var Stream = require('stream').Stream
var utils = require('./utils')
/**
* QueryStream
*
* Returns a stream interface for the `query`.
*
* @param {Query} query
* @return {Stream}
*/
function QueryStream (query) {
Stream.call(this);
this.query = query;
this.readable = true;
this.paused = false;
this._cursor = null;
this._destroyed = null;
this._fields = null;
this._ticks = 0;
// give time to hook up events
var self = this;
process.nextTick(function () {
self._init();
});
}
/**
* Inherit from Stream
* @private
*/
QueryStream.prototype.__proto__ = Stream.prototype;
/**
* Flag stating whether or not this stream is readable.
*/
QueryStream.prototype.readable;
/**
* Flag stating whether or not this stream is paused.
*/
QueryStream.prototype.paused;
/**
* Initialize the query.
* @private
*/
QueryStream.prototype._init = function () {
if (this._destroyed) return;
var query = this.query
, model = query.model
, options = query._optionsForExec(model)
, self = this
try {
query.cast(model);
} catch (err) {
return self.destroy(err);
}
self._fields = utils.clone(options.fields = query._fields);
model.collection.find(query._conditions, options, function (err, cursor) {
if (err) return self.destroy(err);
self._cursor = cursor;
self._next();
});
}
/**
* Pull the next document from the cursor.
* @private
*/
QueryStream.prototype._next = function () {
if (this.paused || this._destroyed) return;
var self = this;
// nextTick is necessary to avoid stack overflows when
// dealing with large result sets. yield occasionally.
if (!(++this._ticks % 20)) {
process.nextTick(function () {
self._cursor.nextObject(function (err, doc) {
self._onNextObject(err, doc);
});
});
} else {
self._cursor.nextObject(function (err, doc) {
self._onNextObject(err, doc);
});
}
}
/**
* Handle each document as its returned from the cursor
* transforming the raw `doc` from -native into a model
* instance.
*
* @private
*/
QueryStream.prototype._onNextObject = function (err, doc) {
if (err) return this.destroy(err);
// when doc is null we hit the end of the cursor
if (!doc) {
return this.destroy();
}
if(this.query.options && this.query.options.lean === true) {
this.emit('data', doc);
this._next();
return;
}
var instance = new this.query.model(undefined, this._fields);
// skip _id for pre-init hooks
delete instance._doc._id;
var self = this;
instance.init(doc, this.query, function (err) {
if (err) return self.destroy(err);
self.emit('data', instance);
self._next();
});
}
/**
* Pauses this stream.
*/
QueryStream.prototype.pause = function () {
this.paused = true;
}
/**
* Resumes this stream.
*/
QueryStream.prototype.resume = function () {
this.paused = false;
this._next();
}
/**
* Destroys the stream, closing the underlying
* cursor. No more events will be emitted.
*/
QueryStream.prototype.destroy = function (err) {
if (this._destroyed) return;
this._destroyed = true;
this.readable = false;
if (this._cursor) {
this._cursor.close();
}
if (err) {
this.emit('error', err);
}
this.emit('close');
}
// TODO - maybe implement the -native raw option to pass binary?
//QueryStream.prototype.setEncoding = function () {
//}
module.exports = exports = QueryStream;