2017-03-27 15:36:45 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
const config = require('wild-config');
|
2017-03-27 15:36:45 +08:00
|
|
|
const mongodb = require('mongodb');
|
2017-10-03 16:18:23 +08:00
|
|
|
const Redis = require('ioredis');
|
2023-03-10 21:47:47 +08:00
|
|
|
const redisUrl = require('./redis-url');
|
|
|
|
const log = require('npmlog');
|
|
|
|
const packageData = require('../package.json');
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
const MongoClient = mongodb.MongoClient;
|
|
|
|
|
|
|
|
module.exports.database = false;
|
2017-07-15 03:16:09 +08:00
|
|
|
module.exports.gridfs = false;
|
|
|
|
module.exports.users = false;
|
2017-07-17 21:32:31 +08:00
|
|
|
module.exports.senderDb = false;
|
2023-03-10 21:47:47 +08:00
|
|
|
|
2017-07-17 21:32:31 +08:00
|
|
|
let getDBConnection = (main, config, callback) => {
|
|
|
|
if (main) {
|
|
|
|
if (!config) {
|
2017-12-21 19:31:01 +08:00
|
|
|
return callback(null, false);
|
2017-07-17 21:32:31 +08:00
|
|
|
}
|
|
|
|
if (config && !/[:/]/.test(config)) {
|
|
|
|
return callback(null, main.db(config));
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 18:45:02 +08:00
|
|
|
MongoClient.connect(
|
|
|
|
config,
|
|
|
|
{
|
|
|
|
useNewUrlParser: true,
|
2020-01-03 19:30:42 +08:00
|
|
|
useUnifiedTopology: true
|
2018-06-12 18:45:02 +08:00
|
|
|
},
|
|
|
|
(err, db) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if (main && db.s && db.s.options && db.s.options.dbName) {
|
|
|
|
db = db.db(db.s.options.dbName);
|
|
|
|
}
|
|
|
|
return callback(null, db);
|
2017-12-21 19:31:01 +08:00
|
|
|
}
|
2018-06-12 18:45:02 +08:00
|
|
|
);
|
2017-07-17 21:32:31 +08:00
|
|
|
};
|
2017-04-24 21:51:50 +08:00
|
|
|
|
2017-07-17 21:32:31 +08:00
|
|
|
module.exports.connect = callback => {
|
2023-03-16 19:31:50 +08:00
|
|
|
const REDIS_CONF = Object.assign(
|
|
|
|
{
|
|
|
|
// some defaults
|
|
|
|
maxRetriesPerRequest: null,
|
|
|
|
showFriendlyErrorStack: true,
|
|
|
|
retryStrategy(times) {
|
|
|
|
const delay = !times ? 1000 : Math.min(2 ** times * 500, 15 * 1000);
|
|
|
|
log.info('Redis', 'Connection retry times=%s delay=%s', times, delay);
|
|
|
|
return delay;
|
|
|
|
},
|
|
|
|
connectionName: `${packageData.name}@${packageData.version}[${process.pid}]`
|
|
|
|
},
|
2023-03-31 17:10:26 +08:00
|
|
|
typeof config.dbs.redis === 'string' ? redisUrl(config.dbs.redis) : config.dbs.redis || {}
|
2023-03-16 19:31:50 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
module.exports.redisConfig = REDIS_CONF;
|
|
|
|
module.exports.queueConf = {
|
|
|
|
connection: Object.assign({ connectionName: `${REDIS_CONF.connectionName}[notify]` }, REDIS_CONF),
|
|
|
|
prefix: `wd:bull`
|
|
|
|
};
|
|
|
|
module.exports.redis = new Redis(REDIS_CONF);
|
|
|
|
|
2017-07-17 21:32:31 +08:00
|
|
|
getDBConnection(false, config.dbs.mongo, (err, db) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2017-04-24 21:51:50 +08:00
|
|
|
}
|
2017-12-21 19:31:01 +08:00
|
|
|
|
|
|
|
if (db.s && db.s.options && db.s.options.dbName) {
|
|
|
|
module.exports.database = db.db(db.s.options.dbName);
|
|
|
|
} else {
|
|
|
|
module.exports.database = db;
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:17:34 +08:00
|
|
|
getDBConnection(db, config.dbs.gridfs, (err, gdb) => {
|
2017-04-24 21:51:50 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-21 19:31:01 +08:00
|
|
|
module.exports.gridfs = gdb || module.exports.database;
|
|
|
|
|
2017-12-20 21:17:34 +08:00
|
|
|
getDBConnection(db, config.dbs.users, (err, udb) => {
|
2017-07-17 21:32:31 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-21 19:31:01 +08:00
|
|
|
module.exports.users = udb || module.exports.database;
|
|
|
|
|
2017-12-20 21:17:34 +08:00
|
|
|
getDBConnection(db, config.dbs.sender, (err, sdb) => {
|
2017-07-17 21:32:31 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-21 19:31:01 +08:00
|
|
|
module.exports.senderDb = sdb || module.exports.database;
|
2017-07-17 21:32:31 +08:00
|
|
|
|
2023-03-10 21:47:47 +08:00
|
|
|
callback();
|
2017-07-17 21:32:31 +08:00
|
|
|
});
|
|
|
|
});
|
2017-04-24 21:51:50 +08:00
|
|
|
});
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
};
|