forked from sparkfun/phant-stream-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (131 loc) · 3.26 KB
/
index.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
/**
* phant-stream-mongodb
* https://github.com/sparkfun/phant-stream-mongodb
*
* Copyright (c) 2014 SparkFun Electronics
* Licensed under the GPL v3 license.
*/
'use strict';
/**** Module dependencies ****/
var util = require('util'),
events = require('events'),
Promise = require('bluebird'),
DB = require('./lib/db'),
Readable = require('./lib/readable'),
Writable = require('./lib/writable');
/**** Make PhantStream an event emitter ****/
util.inherits(PhantStream, events.EventEmitter);
/**** app prototype ****/
var app = PhantStream.prototype;
/**** Expose PhantStream ****/
exports = module.exports = PhantStream;
function PhantStream(options) {
if (! (this instanceof PhantStream)) {
return new PhantStream(options);
}
events.EventEmitter.call(this);
// apply the options
util._extend(this, options || {});
// convert cap to int
this.cap = parseInt(this.cap);
// make sure cap is numeric
if(isNaN(this.cap)) {
this.cap = 0;
}
this.db = DB({
url: this.url
});
}
app.moduleName = 'Stream MongoDB';
app.cap = 50 * 1024 * 1024; // 50mb
app.pageSize = 250; // 250 items per page
app.url = 'mongodb://localhost/test';
app.db = false;
app.readStream = function(id, page) {
return Readable({
mongo: this.db,
id: id,
page: parseInt(page),
pageSize: parseInt(this.pageSize)
});
};
app.objectReadStream = app.readStream;
app.writeStream = function(id) {
return Writable({
mongo: this.db,
id: id,
cap: this.cap
});
};
app.write = function(id, data) {
this.writeStream(id).end(data);
};
app.clear = function(id) {
var self = this;
if(!this.db) {
this.emit('error', 'MongoDB connection failed');
return;
}
this.db.connect().then(function(db) {
db.dropCollection(id, function(err, result) {
if(err) {
self.emit('error', err);
}
});
}).catch(function(err) {
self.emit('error', err);
});
};
app.stats = function(id, callback) {
var self = this;
if(!this.db) {
this.emit('error', 'MongoDB connection failed');
return;
}
this.db.connect().then(function(db) {
db.collection(id, {strict:true}, function(err, col) {
// collection doesn't exist, return empty stats
if(err) {
return callback(null, {
cap: self.cap,
used: 0,
pageCount: 0,
remaining: self.cap
});
}
// get stats from MongoDB
col.stats(function(err, s) {
// grabbing collection stats failed, return empty stats
if(err) {
return callback(null, {
cap: self.cap,
used: 0,
pageCount: 0,
remaining: self.cap
});
}
// build stats object
var stats = {
cap: self.cap,
used: s.size,
pageCount: Math.ceil(s.count / self.pageSize)
};
stats.remaining = stats.cap - stats.used;
if(stats.remaining < 0) {
stats.remaining = 0;
}
callback(null, stats);
});
});
}).catch(function(err) {
// log err
self.emit('error', err);
// return default stats object
return callback(null, {
cap: self.cap,
used: 0,
pageCount: 0,
remaining: self.cap
});
});
};