forked from avinoamr/dbstream-mongo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmongo.js
294 lines (241 loc) · 7.8 KB
/
mongo.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const debug = require('debug')('dbstream-mongo');
const mongodb = require('mongodb-legacy');
const events = require('events');
const extend = require('extend');
const db = require('dbstream');
const util = require('util');
module.exports.mongodb = mongodb;
module.exports.ObjectID = mongodb.ObjectId;
const CLOSE_TIME_OUT = 10 * 60 * 1000; // 10 minutes
util.inherits(Cursor, db.Cursor);
function Cursor(conn) {
Cursor.super_.call(this);
this._conn = conn;
}
Cursor.prototype._save = function (object, callback) {
this._conn.open(function (err, collection) {
if (err) return callback(toError(err));
var conn = this;
var serialized = replace({}, object);
if (typeof serialized.id != 'undefined') {
serialized._id = toObjectID(serialized.id);
delete serialized.id;
}
if (!serialized._id) {
collection.insertOne(serialized, ondone);
} else {
const filter = { '_id': serialized._id }
const options = { upsert: true }
collection.replaceOne(filter, serialized, options, ondone);
}
function ondone(err, result) {
conn.done();
if (err) return callback(toError(err));
if (result.insertedId) {
object.id = fromObjectID(result.insertedId);
}
callback();
}
});
};
Cursor.prototype._remove = function (object, callback) {
if (!object.id) {
var msg = 'Unable to remove object without an ID';
return callback(new Error(msg));
}
var id = toObjectID(object.id);
this._conn.open(function (err, collection) {
if (err) return callback(toError(err));
var conn = this;
collection.deleteOne({ _id: id }, function (err) {
conn.done();
callback(toError(err));
})
});
};
Cursor.prototype._load = function () {
if (this._reading) return;
this._reading = true;
var options = {};
if (this._limit) options.limit = this._limit;
if (this._skip) options.skip = this._skip;
if (this._sort) options.sort = (this._sort || []).map(function (s) {
return [s.key, s.direction];
});
var query = replace({}, this._query);
if (query.id) {
query._id = toObjectID(query.id);
delete query.id;
}
var that = this;
this._conn.open(function (err, collection) {
if (err) return this.emit('error', toError(err));
var conn = this;
collection
.find(query, options)
.stream()
.on('error', function (err) {
conn.done();
that.emit('error', toError(err));
})
.on('end', function () {
conn.done();
that.push(null);
that._reading = false;
})
.on('data', function (obj) {
obj.id = fromObjectID(obj._id);
delete obj._id;
that.push(obj);
});
});
}
// dbstream API requires that objects be modified in-place, and not
// replaced completely with a new reference in order to allow users to access
// the modified object
function replace(obj, other) {
var name;
// remove non-existing keys
for (name in obj) {
if (typeof other[name] == 'undefined') {
delete obj[name];
}
}
// set new keys
for (name in other) {
obj[name] = other[name];
}
return obj;
}
var connections = {};
function closeClient(url, options) {
if (!connections[url] || !connections[url].client) {
debug('connection does not exist');
return;
}
connections[url].clients -= 1;
if (connections[url].clients > 0) {
debug('cannot close connection due to active clients for', url);
return; // still has other open clients
}
// already scheduled to be closed
if (connections[url].closeTimeout) {
debug('closing connection later, timeout already exists for', url);
return;
}
let closeTimeOut = CLOSE_TIME_OUT
const timeOut = options._closeTimeOut
if (timeOut && timeOut >= 0) {
closeTimeOut = timeOut
}
connections[url].closeTimeout = setTimeout(function () {
debug('timeout reached, closing', url);
connections[url].client.close()
delete connections[url];
}, closeTimeOut);
}
function getClient(url, options, callback) {
let retry = options.retry
let maxRetries = options.maxRetries
delete options.retry
delete options.maxRetries
// not connected at all
if (!connections[url]) {
debug('connecting to', url);
connections[url] = { callbacks: [callback], clients: 1 }
mongodb.MongoClient.connect(url, options, function ready(err, client) {
if (!err && client) {
connections[url].client = client;
}
var shouldRetry = retry < maxRetries
var isError = (err && err.err) // err isn't a Error instance
if (isError && err.err.toString().match('timed out') && shouldRetry) {
retry += 1
return mongodb.MongoClient.connect(url, options, ready)
}
connections[url].callbacks.forEach(function (callback) {
callback.call(null, toError(err), client);
})
});
return;
}
clearTimeout(connections[url].closeTimeout);
delete connections[url].closeTimeout;
debug('adding clients to', url, ' - clients count:', connections[url].clients + 1);
connections[url].clients += 1;
// already running, subscribe to get the connection callback
if (!connections[url].client) {
connections[url].callbacks.push(callback);
return
}
// already connected
callback(null, connections[url].client);
}
module.exports.connect = function (url, options) {
const collection = options.collection;
delete options.collection;
if (!collection) {
throw new Error('options.collection is required');
}
// Default maxRetries value is 1 for backwards compatibility
options.maxRetries || (options.maxRetries = 1);
// Set the number of the current try
options.retry = 1;
var conn = new events.EventEmitter();
conn.open = function (callback) {
getClient(url, options, function (err, client) {
if (err) {
callback.call(conn, err, null)
return
}
const db = client.db()
callback.call(conn, null, db.collection(collection));
})
}
conn.done = function () {
closeClient(url, options);
}
util.inherits(_Cursor, Cursor);
function _Cursor() {
_Cursor.super_.call(this, conn);
}
conn.Cursor = _Cursor;
return conn;
}
module.exports.destroy = function () {
for (const url in connections) {
debug('closing', url);
clearTimeout(connections[url].closeTimeout)
delete connections[url].closeTimeout;
if (connections[url].client) {
connections[url].client.close();
}
delete connections[url];
debug('deleted', url);
}
}
function toObjectID(id) {
if (id.$in) {
id.$in = id.$in.map(toObjectID);
return id;
}
if (typeof id == 'string' && id.length == 24 && mongodb.ObjectId.isValid(id)) {
return new mongodb.ObjectId(id);
} else {
return id;
}
}
function fromObjectID(id) {
if (mongodb.ObjectId.isValid(id)) {
return id.toString();
} else {
return id;
}
}
function toError(err) {
if (!err) return;
if (err instanceof Error) return err;
// mongo sdk might return an object with the format { err: message }
// instead of a proper Javascript Error instance
return extend(new Error(err.message || err.err), err);
}