You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/install/databases.js

83 lines
2.2 KiB
JavaScript

8 years ago
'use strict';
9 years ago
var async = require('async');
var prompt = require('prompt');
var winston = require('winston');
9 years ago
var questions = {
redis: require('../src/database/redis').questions,
mongo: require('../src/database/mongo').questions,
9 years ago
};
module.exports = function (config, callback) {
9 years ago
async.waterfall([
function (next) {
winston.info('\nNow configuring ' + config.database + ' database:');
9 years ago
getDatabaseConfig(config, next);
},
function (databaseConfig, next) {
saveDatabaseConfig(config, databaseConfig, next);
},
9 years ago
], callback);
};
9 years ago
function getDatabaseConfig(config, callback) {
if (!config) {
return callback(new Error('aborted'));
}
9 years ago
if (config.database === 'redis') {
if (config['redis:host'] && config['redis:port']) {
9 years ago
callback(null, config);
} else {
9 years ago
prompt.get(questions.redis, callback);
}
9 years ago
} else if (config.database === 'mongo') {
7 years ago
if ((config['mongo:host'] && config['mongo:port']) || config['mongo:uri']) {
9 years ago
callback(null, config);
} else {
9 years ago
prompt.get(questions.mongo, callback);
}
} else {
9 years ago
return callback(new Error('unknown database : ' + config.database));
}
}
9 years ago
function saveDatabaseConfig(config, databaseConfig, callback) {
if (!databaseConfig) {
return callback(new Error('aborted'));
}
11 years ago
9 years ago
// 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'],
9 years ago
};
9 years ago
if (config.redis.host.slice(0, 1) === '/') {
delete config.redis.port;
}
9 years ago
} 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'],
7 years ago
uri: databaseConfig['mongo:uri'],
9 years ago
};
} else {
return callback(new Error('unknown database : ' + config.database));
}
var allQuestions = questions.redis.concat(questions.mongo);
for (var x = 0; x < allQuestions.length; x += 1) {
9 years ago
delete config[allQuestions[x].name];
}
callback(null, config);
8 years ago
}