-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
43 lines (38 loc) · 1.02 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
var insertCompany = function(company, db, callback) {
var collection = db.collection('companies');
collection.insertMany([
company
], function(err, result) {
if(err) { throw err; }
callback(result);
});
}
var MongoClient = require('mongodb').MongoClient
, Server = require('mongodb').Server;
var db; // shared
var url = 'mongodb://mongo:27017/edr'
MongoClient.connect(url, {
poolSize: 10, reconnectTries: 10
}, function(err, mongoClient) {
if (err) {console.log(err);return;}
db = mongoClient;
});
module.exports = function() {
var stream = require('stream');
var mongoWriter = new stream.Writable({objectMode: true});
mongoWriter._write = function (company, encoding, done) {
// wait for db to be set
function defer() {
if(db === undefined) {
console.log('Waiting for db connection...');
setTimeout(defer, 1000);
} else {
insertCompany(company, client, function(result) {
done();
})
}
}
defer();
}
return mongoWriter;
}