wildduck/lib/db.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
2017-07-16 19:37:33 +08:00
const config = require('wild-config');
const tools = require('./tools');
const mongodb = require('mongodb');
const redis = require('redis');
const MongoClient = mongodb.MongoClient;
module.exports.database = false;
module.exports.gridfs = false;
module.exports.users = false;
2017-07-17 21:32:31 +08:00
module.exports.senderDb = false;
module.exports.redis = false;
module.exports.redisConfig = false;
2017-07-17 21:32:31 +08:00
let getDBConnection = (main, config, callback) => {
if (main) {
if (!config) {
return callback(null, main);
}
if (config && !/[:/]/.test(config)) {
return callback(null, main.db(config));
}
}
MongoClient.connect(config, (err, db) => {
if (err) {
return callback(err);
}
2017-07-17 21:32:31 +08:00
return callback(null, db);
});
};
2017-04-24 21:51:50 +08:00
2017-07-17 21:32:31 +08:00
module.exports.connect = callback => {
getDBConnection(false, config.dbs.mongo, (err, db) => {
if (err) {
return callback(err);
2017-04-24 21:51:50 +08:00
}
2017-07-17 21:32:31 +08:00
module.exports.database = db;
getDBConnection(db, config.dbs.gridfs, (err, db) => {
2017-04-24 21:51:50 +08:00
if (err) {
return callback(err);
}
2017-07-17 21:32:31 +08:00
module.exports.gridfs = db;
getDBConnection(db, config.dbs.users, (err, db) => {
if (err) {
return callback(err);
}
module.exports.users = db;
getDBConnection(db, config.dbs.sender, (err, db) => {
if (err) {
return callback(err);
}
module.exports.senderDb = db;
module.exports.redisConfig = tools.redisConfig(config.dbs.redis);
module.exports.redis = redis.createClient(module.exports.redisConfig);
return callback(null, module.exports.database);
});
});
2017-04-24 21:51:50 +08:00
});
});
};