closes #3938
parent
4bff714947
commit
b3f63e0a0d
@ -1,104 +1,82 @@
|
||||
"use strict";
|
||||
|
||||
var async = require('async'),
|
||||
prompt = require('prompt'),
|
||||
nconf = require('nconf'),
|
||||
winston = require('winston'),
|
||||
var async = require('async');
|
||||
var prompt = require('prompt');
|
||||
var winston = require('winston');
|
||||
|
||||
questions = {};
|
||||
var questions = {
|
||||
redis: require('../src/database/redis').questions,
|
||||
mongo: require('../src/database/mongo').questions
|
||||
};
|
||||
|
||||
function success(err, config, callback) {
|
||||
if (!config) {
|
||||
return callback(new Error('aborted'));
|
||||
}
|
||||
|
||||
var database = (config.redis || config.mongo) ? config.secondary_database : config.database;
|
||||
|
||||
function dbQuestionsSuccess(err, databaseConfig) {
|
||||
if (!databaseConfig) {
|
||||
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];
|
||||
module.exports = function(config, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
process.stdout.write('\n');
|
||||
winston.info('Now configuring ' + config.database + ' database:');
|
||||
getDatabaseConfig(config, next);
|
||||
},
|
||||
function (databaseConfig, next) {
|
||||
saveDatabaseConfig(config, databaseConfig, next);
|
||||
}
|
||||
], 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']) {
|
||||
dbQuestionsSuccess(null, config);
|
||||
callback(null, config);
|
||||
} 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']) {
|
||||
dbQuestionsSuccess(null, config);
|
||||
callback(null, config);
|
||||
} else {
|
||||
prompt.get(questions.mongo, dbQuestionsSuccess);
|
||||
prompt.get(questions.mongo, callback);
|
||||
}
|
||||
} else {
|
||||
return callback(new Error('unknown database : ' + database));
|
||||
return callback(new Error('unknown database : ' + config.database));
|
||||
}
|
||||
}
|
||||
|
||||
function getSecondaryDatabaseModules(config, next) {
|
||||
prompt.get({
|
||||
"name": "secondary_db_modules",
|
||||
"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);
|
||||
function saveDatabaseConfig(config, databaseConfig, callback) {
|
||||
if (!databaseConfig) {
|
||||
return callback(new Error('aborted'));
|
||||
}
|
||||
|
||||
allowedDBs.forEach(function(db) {
|
||||
questions[db] = require('./../src/database/' + db).questions;
|
||||
});
|
||||
// Translate redis properties into redis object
|
||||
if (config.database === 'redis') {
|
||||
config.redis = {
|
||||
host: databaseConfig['redis:host'],
|
||||
port: databaseConfig['redis:port'],
|
||||
password: databaseConfig['redis:password'],
|
||||
database: databaseConfig['redis:database']
|
||||
};
|
||||
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
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);
|
||||
}
|
||||
if (config.redis.host.slice(0, 1) === '/') {
|
||||
delete config.redis.port;
|
||||
}
|
||||
], 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";
|
||||
|
||||
var nconf = require('nconf'),
|
||||
primaryDBName = nconf.get('database'),
|
||||
secondaryDBName = nconf.get('secondary_database'),
|
||||
secondaryModules = nconf.get('secondary_db_modules'),
|
||||
winston = require('winston'),
|
||||
async = require('async'),
|
||||
var nconf = require('nconf');
|
||||
var databaseName = nconf.get('database');
|
||||
var winston = require('winston');
|
||||
|
||||
ALLOWED_MODULES = ['hash', 'list', 'sets', 'sorted'];
|
||||
|
||||
if(!primaryDBName) {
|
||||
if (!databaseName) {
|
||||
winston.info('Database type not set! Run ./nodebb setup');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
function setupSecondaryDB() {
|
||||
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();
|
||||
}
|
||||
var primaryDB = require('./database/' + databaseName);
|
||||
|
||||
module.exports = primaryDB;
|
Loading…
Reference in New Issue