json format for setting value

This commit is contained in:
LouisLam 2021-07-31 22:02:30 +08:00
parent c6a66fad79
commit 33d7f8645a

View File

@ -40,9 +40,15 @@ exports.ping = function (hostname) {
} }
exports.setting = async function (key) { exports.setting = async function (key) {
return await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [
key, key,
]) ]);
try {
return JSON.parse(value);
} catch (e) {
return value;
}
} }
exports.setSetting = async function (key, value) { exports.setSetting = async function (key, value) {
@ -53,7 +59,7 @@ exports.setSetting = async function (key, value) {
bean = R.dispense("setting") bean = R.dispense("setting")
bean.key = key; bean.key = key;
} }
bean.value = value; bean.value = JSON.stringify(value);
await R.store(bean) await R.store(bean)
} }
@ -65,7 +71,11 @@ exports.getSettings = async function (type) {
let result = {}; let result = {};
for (let row of list) { for (let row of list) {
try {
result[row.key] = JSON.parse(row.value); result[row.key] = JSON.parse(row.value);
} catch (e) {
result[row.key] = row.value;
}
} }
return result; return result;