2020-06-20 12:31:38 +02:00
|
|
|
function getOption(name) {
|
|
|
|
const option = require('./repository').getOption(name);
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
if (!option) {
|
2019-11-01 23:05:33 +01:00
|
|
|
throw new Error(`Option ${name} doesn't exist`);
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
return option.value;
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2019-11-10 11:25:41 +01:00
|
|
|
/**
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptionInt(name) {
|
|
|
|
const val = getOption(name);
|
2019-11-03 11:43:04 +01:00
|
|
|
|
|
|
|
const intVal = parseInt(val);
|
|
|
|
|
|
|
|
if (isNaN(intVal)) {
|
|
|
|
throw new Error(`Could not parse "${val}" into integer for option "${name}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return intVal;
|
|
|
|
}
|
|
|
|
|
2019-11-10 11:25:41 +01:00
|
|
|
/**
|
|
|
|
* @return {Promise<boolean>}
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptionBool(name) {
|
|
|
|
const val = getOption(name);
|
2019-11-10 11:25:41 +01:00
|
|
|
|
|
|
|
if (!['true', 'false'].includes(val)) {
|
|
|
|
throw new Error(`Could not parse "${val}" into boolean for option "${name}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val === 'true';
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function setOption(name, value) {
|
|
|
|
const option = require('./repository').getOption(name);
|
2018-01-11 22:45:25 -05:00
|
|
|
|
2020-03-01 20:49:11 +01:00
|
|
|
if (option) {
|
|
|
|
option.value = value;
|
2018-01-11 22:45:25 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
option.save();
|
2020-03-01 20:49:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-06-20 12:31:38 +02:00
|
|
|
createOption(name, value, false);
|
2020-03-01 20:49:11 +01:00
|
|
|
}
|
2018-01-11 22:45:25 -05:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function createOption(name, value, isSynced) {
|
2018-06-10 15:49:22 -04:00
|
|
|
// to avoid circular dependency, need to find better solution
|
|
|
|
const Option = require('../entities/option');
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
new Option({
|
2018-01-28 19:30:14 -05:00
|
|
|
name: name,
|
|
|
|
value: value,
|
2018-05-22 00:22:43 -04:00
|
|
|
isSynced: isSynced
|
|
|
|
}).save();
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptions() {
|
|
|
|
return require('./repository').getEntities("SELECT * FROM options ORDER BY name");
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptionsMap() {
|
2020-06-20 23:09:34 +02:00
|
|
|
return require('./sql').getMap("SELECT name, value FROM options ORDER BY name");
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:48:02 -04:00
|
|
|
module.exports = {
|
|
|
|
getOption,
|
2019-11-03 11:43:04 +01:00
|
|
|
getOptionInt,
|
2019-11-10 11:25:41 +01:00
|
|
|
getOptionBool,
|
2017-11-02 20:48:02 -04:00
|
|
|
setOption,
|
2018-09-06 11:54:04 +02:00
|
|
|
createOption,
|
|
|
|
getOptions,
|
|
|
|
getOptionsMap
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|