forked from AndrewGrachov/mongomock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cursor.js
62 lines (53 loc) · 1.33 KB
/
Cursor.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
//cowboy cursor mock
var util = require('util');
var _q = require('./util');
var ReadableStream = require('stream').Readable;
function map(fields) {
var keys = Object.keys(fields);
var exclude = keys.every(function(key) {
return fields[key] === -1;
});
var include = keys.every(function(key) {
return !!fields[key] && fields[key] !== -1;
});
if(!exclude && !include) {
throw 'you can either select fields to include, or select fields to exclude';
}
return function(doc) {
var mapped = {};
if (exclude) {
keys.forEach(function (key) {
delete doc[key];
});
return doc;
}
keys.forEach(function (key) {
mapped[key] = doc[key];
});
return mapped;
};
}
var Cursor = function (source, query, fields, options) {
this._fields = fields || {};
this._source =_q(source).find(query, options);
this.index = 0;
ReadableStream.call(this, {objectMode: true});
};
util.inherits(Cursor, ReadableStream);
Cursor.prototype._read = function () {
var self = this;
if (this.index < this._source.length) {
//mock 'reading time'
setTimeout(function () {
self.push(map(self._fields)(self._source[self.index]));
self.index++;
},5);
} else {
self.push(null);
}
};
Cursor.prototype.toArray = function (callback) {
var docs = this._source.map(map(this._fields));
callback(null, docs);
};
module.exports = Cursor;