closes #3938
parent
4bff714947
commit
b3f63e0a0d
@ -1,104 +1,82 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var async = require('async'),
|
var async = require('async');
|
||||||
prompt = require('prompt'),
|
var prompt = require('prompt');
|
||||||
nconf = require('nconf'),
|
var winston = require('winston');
|
||||||
winston = require('winston'),
|
|
||||||
|
|
||||||
questions = {};
|
var questions = {
|
||||||
|
redis: require('../src/database/redis').questions,
|
||||||
|
mongo: require('../src/database/mongo').questions
|
||||||
|
};
|
||||||
|
|
||||||
function success(err, config, callback) {
|
module.exports = function(config, callback) {
|
||||||
if (!config) {
|
async.waterfall([
|
||||||
return callback(new Error('aborted'));
|
function (next) {
|
||||||
}
|
process.stdout.write('\n');
|
||||||
|
winston.info('Now configuring ' + config.database + ' database:');
|
||||||
var database = (config.redis || config.mongo) ? config.secondary_database : config.database;
|
getDatabaseConfig(config, next);
|
||||||
|
},
|
||||||
function dbQuestionsSuccess(err, databaseConfig) {
|
function (databaseConfig, next) {
|
||||||
if (!databaseConfig) {
|
saveDatabaseConfig(config, databaseConfig, next);
|
||||||
return callback(new Error('aborted'));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translate redis properties into redis object
|
|
||||||
if(database === 'redis') {
|
|
||||||
config.redis = {
|
|
||||||
host: databaseConfig['redis:host'],
|
|
||||||
port: databaseConfig['redis:port'],
|
|
||||||
password: databaseConfig['redis:password'],
|
|
||||||
database: databaseConfig['redis:database']
|
|
||||||
};
|
|
||||||
|
|
||||||
if (config.redis.host.slice(0, 1) === '/') {
|
|
||||||
delete config.redis.port;
|
|
||||||
}
|
|
||||||
} else if (database === 'mongo') {
|
|
||||||
config.mongo = {
|
|
||||||
host: databaseConfig['mongo:host'],
|
|
||||||
port: databaseConfig['mongo:port'],
|
|
||||||
username: databaseConfig['mongo:username'],
|
|
||||||
password: databaseConfig['mongo:password'],
|
|
||||||
database: databaseConfig['mongo:database']
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return callback(new Error('unknown database : ' + database));
|
|
||||||
}
|
|
||||||
|
|
||||||
var allQuestions = questions.redis.concat(questions.mongo);
|
|
||||||
for(var x=0;x<allQuestions.length;x++) {
|
|
||||||
delete config[allQuestions[x].name];
|
|
||||||
}
|
}
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
callback(err, config);
|
function getDatabaseConfig(config, callback) {
|
||||||
|
if (!config) {
|
||||||
|
return callback(new Error('aborted'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(database === 'redis') {
|
if (config.database === 'redis') {
|
||||||
if (config['redis:host'] && config['redis:port']) {
|
if (config['redis:host'] && config['redis:port']) {
|
||||||
dbQuestionsSuccess(null, config);
|
callback(null, config);
|
||||||
} else {
|
} else {
|
||||||
prompt.get(questions.redis, dbQuestionsSuccess);
|
prompt.get(questions.redis, callback);
|
||||||
}
|
}
|
||||||
} else if(database === 'mongo') {
|
} else if (config.database === 'mongo') {
|
||||||
if (config['mongo:host'] && config['mongo:port']) {
|
if (config['mongo:host'] && config['mongo:port']) {
|
||||||
dbQuestionsSuccess(null, config);
|
callback(null, config);
|
||||||
} else {
|
} else {
|
||||||
prompt.get(questions.mongo, dbQuestionsSuccess);
|
prompt.get(questions.mongo, callback);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error('unknown database : ' + database));
|
return callback(new Error('unknown database : ' + config.database));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSecondaryDatabaseModules(config, next) {
|
function saveDatabaseConfig(config, databaseConfig, callback) {
|
||||||
prompt.get({
|
if (!databaseConfig) {
|
||||||
"name": "secondary_db_modules",
|
return callback(new Error('aborted'));
|
||||||
"description": "Which database modules should " + config.secondary_database + " store?",
|
}
|
||||||
"default": nconf.get('secondary_db_modules') || "hash, list, sets, sorted"
|
|
||||||
}, function(err, db) {
|
|
||||||
config.secondary_db_modules = db.secondary_db_modules;
|
|
||||||
success(err, config, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function(err, config, databases, callback) {
|
|
||||||
var allowedDBs = Object.keys(databases);
|
|
||||||
|
|
||||||
allowedDBs.forEach(function(db) {
|
// Translate redis properties into redis object
|
||||||
questions[db] = require('./../src/database/' + db).questions;
|
if (config.database === 'redis') {
|
||||||
});
|
config.redis = {
|
||||||
|
host: databaseConfig['redis:host'],
|
||||||
|
port: databaseConfig['redis:port'],
|
||||||
|
password: databaseConfig['redis:password'],
|
||||||
|
database: databaseConfig['redis:database']
|
||||||
|
};
|
||||||
|
|
||||||
async.waterfall([
|
if (config.redis.host.slice(0, 1) === '/') {
|
||||||
function(next) {
|
delete config.redis.port;
|
||||||
process.stdout.write('\n');
|
|
||||||
winston.info('Now configuring ' + config.database + ' database:');
|
|
||||||
success(err, config, next);
|
|
||||||
},
|
|
||||||
function(config, next) {
|
|
||||||
if (config.secondary_database && allowedDBs.indexOf(config.secondary_database) !== -1) {
|
|
||||||
winston.info('Now configuring ' + config.secondary_database + ' database:');
|
|
||||||
getSecondaryDatabaseModules(config, next);
|
|
||||||
} else {
|
|
||||||
next(err, config);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
], callback);
|
} else if (config.database === 'mongo') {
|
||||||
};
|
config.mongo = {
|
||||||
|
host: databaseConfig['mongo:host'],
|
||||||
|
port: databaseConfig['mongo:port'],
|
||||||
|
username: databaseConfig['mongo:username'],
|
||||||
|
password: databaseConfig['mongo:password'],
|
||||||
|
database: databaseConfig['mongo:database']
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return callback(new Error('unknown database : ' + config.database));
|
||||||
|
}
|
||||||
|
|
||||||
|
var allQuestions = questions.redis.concat(questions.mongo);
|
||||||
|
for (var x=0; x<allQuestions.length; x++) {
|
||||||
|
delete config[allQuestions[x].name];
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, config);
|
||||||
|
}
|
@ -1,56 +1,14 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var nconf = require('nconf'),
|
var nconf = require('nconf');
|
||||||
primaryDBName = nconf.get('database'),
|
var databaseName = nconf.get('database');
|
||||||
secondaryDBName = nconf.get('secondary_database'),
|
var winston = require('winston');
|
||||||
secondaryModules = nconf.get('secondary_db_modules'),
|
|
||||||
winston = require('winston'),
|
|
||||||
async = require('async'),
|
|
||||||
|
|
||||||
ALLOWED_MODULES = ['hash', 'list', 'sets', 'sorted'];
|
if (!databaseName) {
|
||||||
|
|
||||||
if(!primaryDBName) {
|
|
||||||
winston.info('Database type not set! Run ./nodebb setup');
|
winston.info('Database type not set! Run ./nodebb setup');
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupSecondaryDB() {
|
var primaryDB = require('./database/' + databaseName);
|
||||||
var secondaryDB = require('./database/' + secondaryDBName);
|
|
||||||
|
|
||||||
secondaryModules = secondaryModules.split(/,\s*/);
|
|
||||||
|
|
||||||
for (var module in secondaryModules) {
|
|
||||||
if (secondaryModules.hasOwnProperty(module) && ALLOWED_MODULES.indexOf(module) !== -1) {
|
|
||||||
primaryDB[module] = secondaryDB[module];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var primaryDBinit = primaryDB.init,
|
|
||||||
primaryDBclose = primaryDB.close,
|
|
||||||
primaryDBhelpers = primaryDB.helpers;
|
|
||||||
|
|
||||||
primaryDB.init = function(callback) {
|
|
||||||
async.parallel([primaryDBinit, secondaryDB.init], function(err, results) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
primaryDB.close = function(callback) {
|
|
||||||
async.parallel([primaryDBclose, secondaryDB.close], function(err, results) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
primaryDB.helpers = {};
|
|
||||||
primaryDB.helpers[primaryDBName] = primaryDBhelpers[primaryDBName];
|
|
||||||
primaryDB.helpers[secondaryDBName] = secondaryDB.helpers[secondaryDBName];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var primaryDB = require('./database/' + primaryDBName);
|
|
||||||
|
|
||||||
if (secondaryDBName && secondaryModules) {
|
|
||||||
setupSecondaryDB();
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = primaryDB;
|
module.exports = primaryDB;
|
Loading…
Reference in New Issue