wildduck/lib/db.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
const config = require('config');
const tools = require('./tools');
const mongodb = require('mongodb');
const redis = require('redis');
const MongoClient = mongodb.MongoClient;
2017-04-25 03:59:38 +08:00
module.exports.senderDb = false;
module.exports.database = false;
module.exports.redis = false;
module.exports.redisConfig = false;
module.exports.connect = callback => {
MongoClient.connect(config.mongo, (err, database) => {
if (err) {
return callback(err);
}
module.exports.database = database;
module.exports.redisConfig = tools.redisConfig(config.redis);
module.exports.redis = redis.createClient(module.exports.redisConfig);
2017-04-24 21:51:50 +08:00
2017-04-25 03:59:38 +08:00
if (!config.sender.enabled) {
2017-04-24 21:51:50 +08:00
return callback(null, database);
}
2017-04-25 03:59:38 +08:00
if (!config.sender.mongo) {
module.exports.senderDb = database;
2017-04-24 21:51:50 +08:00
return callback(null, database);
}
2017-04-25 03:59:38 +08:00
MongoClient.connect(config.sender.mongo, (err, forwarderDatabase) => {
2017-04-24 21:51:50 +08:00
if (err) {
database.close();
return callback(err);
}
2017-04-25 03:59:38 +08:00
module.exports.senderDb = forwarderDatabase;
2017-04-24 21:51:50 +08:00
return callback(null, database);
});
});
};