From 515095ecfb951c095b50bfafdb83d28bdeb5c28d Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 29 Jun 2022 14:57:40 +0800 Subject: [PATCH] Move all settings code from `util-server.js` into `settings.js` --- server/settings.js | 104 ++++++++++++++++++++++++++++++++++++++++++ server/util-server.js | 67 +++------------------------ 2 files changed, 111 insertions(+), 60 deletions(-) create mode 100644 server/settings.js diff --git a/server/settings.js b/server/settings.js new file mode 100644 index 00000000..3f165f55 --- /dev/null +++ b/server/settings.js @@ -0,0 +1,104 @@ +const { R } = require("redbean-node"); +const { log } = require("../src/util"); + +class Settings { + + cacheList = []; + + /** + * Retrieve value of setting based on key + * @param {string} key Key of setting to retrieve + * @returns {Promise} Value + */ + static async get(key) { + let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ + key, + ]); + + try { + const v = JSON.parse(value); + log.debug("util", `Get Setting: ${key}: ${v}`); + return v; + } catch (e) { + return value; + } + } + + /** + * Sets the specified setting to specified value + * @param {string} key Key of setting to set + * @param {any} value Value to set to + * @param {?string} type Type of setting + * @returns {Promise} + */ + static async set(key, value, type = null) { + let bean = await R.findOne("setting", " `key` = ? ", [ + key, + ]); + if (!bean) { + bean = R.dispense("setting"); + bean.key = key; + } + bean.type = type; + bean.value = JSON.stringify(value); + await R.store(bean); + } + + /** + * Get settings based on type + * @param {string} type The type of setting + * @returns {Promise} + */ + static async getSettings(type) { + let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [ + type, + ]); + + let result = {}; + + for (let row of list) { + try { + result[row.key] = JSON.parse(row.value); + } catch (e) { + result[row.key] = row.value; + } + } + + return result; + } + + /** + * Set settings based on type + * @param {string} type Type of settings to set + * @param {Object} data Values of settings + * @returns {Promise} + */ + static async setSettings(type, data) { + let keyList = Object.keys(data); + + let promiseList = []; + + for (let key of keyList) { + let bean = await R.findOne("setting", " `key` = ? ", [ + key + ]); + + if (bean == null) { + bean = R.dispense("setting"); + bean.type = type; + bean.key = key; + } + + if (bean.type === type) { + bean.value = JSON.stringify(data[key]); + promiseList.push(R.store(bean)); + } + } + + await Promise.all(promiseList); + } +} + +module.exports = { + Settings, +}; diff --git a/server/util-server.js b/server/util-server.js index 87f9151d..f6a0e396 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -12,6 +12,7 @@ const chroma = require("chroma-js"); const { badgeConstants } = require("./config"); const mssql = require("mssql"); const { NtlmClient } = require("axios-ntlm"); +const { Settings } = require("./settings"); // From ping-lite exports.WIN = /^win/.test(process.platform); @@ -260,17 +261,7 @@ exports.mssqlQuery = function (connectionString, query) { * @returns {Promise} Value */ exports.setting = async function (key) { - let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ - key, - ]); - - try { - const v = JSON.parse(value); - log.debug("util", `Get Setting: ${key}: ${v}`); - return v; - } catch (e) { - return value; - } + return await Settings.get(key); }; /** @@ -281,70 +272,26 @@ exports.setting = async function (key) { * @returns {Promise} */ exports.setSetting = async function (key, value, type = null) { - let bean = await R.findOne("setting", " `key` = ? ", [ - key, - ]); - if (!bean) { - bean = R.dispense("setting"); - bean.key = key; - } - bean.type = type; - bean.value = JSON.stringify(value); - await R.store(bean); + await Settings.set(key, value, type); }; /** * Get settings based on type - * @param {?string} type The type of setting + * @param {string} type The type of setting * @returns {Promise} */ exports.getSettings = async function (type) { - let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [ - type, - ]); - - let result = {}; - - for (let row of list) { - try { - result[row.key] = JSON.parse(row.value); - } catch (e) { - result[row.key] = row.value; - } - } - - return result; + return await Settings.getSettings(type); }; /** * Set settings based on type - * @param {?string} type Type of settings to set + * @param {string} type Type of settings to set * @param {Object} data Values of settings * @returns {Promise} */ exports.setSettings = async function (type, data) { - let keyList = Object.keys(data); - - let promiseList = []; - - for (let key of keyList) { - let bean = await R.findOne("setting", " `key` = ? ", [ - key - ]); - - if (bean == null) { - bean = R.dispense("setting"); - bean.type = type; - bean.key = key; - } - - if (bean.type === type) { - bean.value = JSON.stringify(data[key]); - promiseList.push(R.store(bean)); - } - } - - await Promise.all(promiseList); + await Settings.setSettings(type, data); }; // ssl-checker by @dyaa