forked from avinoamr/dbstream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbstream.js
68 lines (54 loc) · 1.57 KB
/
dbstream.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
const stream = require("stream");
const util = require("util");
// Functional API
util.inherits(Cursor, stream.Duplex);
function Cursor() {
Cursor.super_.call(this, { objectMode: true });
}
Cursor.prototype._save = function (obj, callback) {
throw new Error("_save is not implemented");
}
Cursor.prototype._remove = function (obj, callback) {
throw new Error("_remove is not implemented");
}
Cursor.prototype._load = function (size) {
throw new Error("_load is not implemented");
}
Cursor.prototype.remove = function (chunk, encoding, callback) {
return this.write({ $remove: chunk }, encoding, callback);
}
Cursor.prototype._read = function (size) {
if (!this._query) return this.push(null); // nothing to query
return this._load(size);
}
Cursor.prototype._write = function (chunk, encoding, callback) {
return (chunk.$remove)
? this._remove(chunk.$remove, callback)
: this._save(chunk, callback);
}
// Query API
Cursor.prototype.find = function (query) {
this._query = query;
return this;
}
Cursor.prototype.sort = function (key, direction) {
this._sort || (this._sort = []);
this._sort.push({ key: key, direction: direction || 1 });
return this;
}
Cursor.prototype.skip = function (n) {
this._skip = n;
return this;
}
Cursor.prototype.limit = function (n) {
this._limit = n;
return this;
}
Cursor.prototype.copy = function (other) {
this._query = other._query
this._sort = other._sort
this._skip = other._skip
this._limit = other._limit
return this
}
module.exports.Cursor = Cursor;