trilium/src/services/options.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-09-06 17:54:04 +08:00
const utils = require('./utils');
2018-01-29 08:30:14 +08:00
async function getOption(name) {
const option = await require('./repository').getOption(name);
2017-11-03 08:48:02 +08:00
if (!option) {
2019-11-02 06:05:33 +08:00
throw new Error(`Option ${name} doesn't exist`);
2017-11-03 08:48:02 +08:00
}
return option.value;
2017-11-03 08:48:02 +08:00
}
async function setOption(name, value) {
const option = await require('./repository').getOption(name);
if (!option) {
2018-01-29 08:30:14 +08:00
throw new Error(`Option ${name} doesn't exist`);
}
option.value = value;
await option.save();
}
async function createOption(name, value, isSynced) {
// to avoid circular dependency, need to find better solution
const Option = require('../entities/option');
await new Option({
2018-01-29 08:30:14 +08:00
name: name,
value: value,
isSynced: isSynced
}).save();
2017-11-03 08:48:02 +08:00
}
2018-09-06 17:54:04 +08:00
async function getOptions(allowedOptions) {
let options = await require('./repository').getEntities("SELECT * FROM options ORDER BY name");
if (allowedOptions) {
options = options.filter(opt => allowedOptions.includes(opt.name));
}
return options;
}
async function getOptionsMap(allowedOptions) {
const options = await getOptions(allowedOptions);
return utils.toObject(options, opt => [opt.name, opt.value]);
}
2017-11-03 08:48:02 +08:00
module.exports = {
getOption,
setOption,
2018-09-06 17:54:04 +08:00
createOption,
getOptions,
getOptionsMap
2017-11-03 08:48:02 +08:00
};