2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2014-04-14 15:03:11 -04:00
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
const prompt = require('prompt');
|
|
|
|
|
const winston = require('winston');
|
2014-04-14 15:03:11 -04:00
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
const questions = {
|
2015-12-21 12:02:10 +02:00
|
|
|
redis: require('../src/database/redis').questions,
|
2017-02-17 19:31:21 -07:00
|
|
|
mongo: require('../src/database/mongo').questions,
|
2018-08-08 14:13:48 -05:00
|
|
|
postgres: require('../src/database/postgres').questions,
|
2015-12-21 12:02:10 +02:00
|
|
|
};
|
2014-04-14 15:03:11 -04:00
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
module.exports = async function (config) {
|
2021-02-03 23:59:08 -07:00
|
|
|
winston.info(`\nNow configuring ${config.database} database:`);
|
2020-07-31 12:49:25 -04:00
|
|
|
const databaseConfig = await getDatabaseConfig(config);
|
|
|
|
|
return saveDatabaseConfig(config, databaseConfig);
|
2015-12-21 12:02:10 +02:00
|
|
|
};
|
2014-04-14 15:03:11 -04:00
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
async function getDatabaseConfig(config) {
|
2015-12-21 12:02:10 +02:00
|
|
|
if (!config) {
|
2020-07-31 12:49:25 -04:00
|
|
|
throw new Error('invalid config, aborted');
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-21 12:02:10 +02:00
|
|
|
if (config.database === 'redis') {
|
2014-04-14 15:03:11 -04:00
|
|
|
if (config['redis:host'] && config['redis:port']) {
|
2020-07-31 12:49:25 -04:00
|
|
|
return config;
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
2021-10-18 17:34:26 -04:00
|
|
|
return await prompt.get(questions.redis);
|
2015-12-21 12:02:10 +02:00
|
|
|
} else if (config.database === 'mongo') {
|
2017-10-23 15:09:13 -04:00
|
|
|
if ((config['mongo:host'] && config['mongo:port']) || config['mongo:uri']) {
|
2020-07-31 12:49:25 -04:00
|
|
|
return config;
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
2021-10-18 17:34:26 -04:00
|
|
|
return await prompt.get(questions.mongo);
|
2018-08-08 14:13:48 -05:00
|
|
|
} else if (config.database === 'postgres') {
|
|
|
|
|
if (config['postgres:host'] && config['postgres:port']) {
|
2020-07-31 12:49:25 -04:00
|
|
|
return config;
|
2018-08-08 14:13:48 -05:00
|
|
|
}
|
2021-10-18 17:34:26 -04:00
|
|
|
return await prompt.get(questions.postgres);
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
2021-02-03 23:59:08 -07:00
|
|
|
throw new Error(`unknown database : ${config.database}`);
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
function saveDatabaseConfig(config, databaseConfig) {
|
2015-12-21 12:02:10 +02:00
|
|
|
if (!databaseConfig) {
|
2020-07-31 12:49:25 -04:00
|
|
|
throw new Error('invalid config, aborted');
|
2015-12-21 12:02:10 +02:00
|
|
|
}
|
2014-05-20 14:44:50 -04:00
|
|
|
|
2015-12-21 12:02:10 +02:00
|
|
|
// Translate redis properties into redis object
|
|
|
|
|
if (config.database === 'redis') {
|
|
|
|
|
config.redis = {
|
|
|
|
|
host: databaseConfig['redis:host'],
|
|
|
|
|
port: databaseConfig['redis:port'],
|
|
|
|
|
password: databaseConfig['redis:password'],
|
2017-02-17 19:31:21 -07:00
|
|
|
database: databaseConfig['redis:database'],
|
2015-12-21 12:02:10 +02:00
|
|
|
};
|
2014-04-14 15:03:11 -04:00
|
|
|
|
2015-12-21 12:02:10 +02:00
|
|
|
if (config.redis.host.slice(0, 1) === '/') {
|
|
|
|
|
delete config.redis.port;
|
2014-04-14 15:03:11 -04:00
|
|
|
}
|
2015-12-21 12:02:10 +02:00
|
|
|
} else if (config.database === 'mongo') {
|
|
|
|
|
config.mongo = {
|
|
|
|
|
host: databaseConfig['mongo:host'],
|
|
|
|
|
port: databaseConfig['mongo:port'],
|
|
|
|
|
username: databaseConfig['mongo:username'],
|
|
|
|
|
password: databaseConfig['mongo:password'],
|
2017-02-17 19:31:21 -07:00
|
|
|
database: databaseConfig['mongo:database'],
|
2017-10-23 15:09:13 -04:00
|
|
|
uri: databaseConfig['mongo:uri'],
|
2015-12-21 12:02:10 +02:00
|
|
|
};
|
2018-08-08 14:13:48 -05:00
|
|
|
} else if (config.database === 'postgres') {
|
|
|
|
|
config.postgres = {
|
|
|
|
|
host: databaseConfig['postgres:host'],
|
|
|
|
|
port: databaseConfig['postgres:port'],
|
|
|
|
|
username: databaseConfig['postgres:username'],
|
|
|
|
|
password: databaseConfig['postgres:password'],
|
|
|
|
|
database: databaseConfig['postgres:database'],
|
2019-09-07 18:27:51 +01:00
|
|
|
ssl: databaseConfig['postgres:ssl'],
|
2018-08-08 14:13:48 -05:00
|
|
|
};
|
2015-12-21 12:02:10 +02:00
|
|
|
} else {
|
2021-02-03 23:59:08 -07:00
|
|
|
throw new Error(`unknown database : ${config.database}`);
|
2015-12-21 12:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
const allQuestions = questions.redis.concat(questions.mongo).concat(questions.postgres);
|
2021-02-04 00:06:15 -07:00
|
|
|
for (let x = 0; x < allQuestions.length; x += 1) {
|
2015-12-21 12:02:10 +02:00
|
|
|
delete config[allQuestions[x].name];
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 12:49:25 -04:00
|
|
|
return config;
|
2017-02-18 02:30:48 -07:00
|
|
|
}
|