server-ts: Port options service

This commit is contained in:
Elian Doran 2024-02-17 01:24:37 +02:00
parent 06287da9d8
commit 4b1d2c6bad
No known key found for this signature in database
2 changed files with 14 additions and 15 deletions

View File

@ -11,6 +11,8 @@ import BAttribute = require('./entities/battribute');
* There's a similar frontend cache Froca, and share cache Shaca. * There's a similar frontend cache Froca, and share cache Shaca.
*/ */
class Becca { class Becca {
loaded!: boolean;
notes!: Record<string, BNote>; notes!: Record<string, BNote>;
attributes!: Record<string, BAttribute>; attributes!: Record<string, BAttribute>;
/** Points from attribute type-name to list of attributes */ /** Points from attribute type-name to list of attributes */

View File

@ -1,22 +1,21 @@
const becca = require('../becca/becca.js'); import becca = require('../becca/becca');
const sql = require('./sql'); import { OptionRow } from '../becca/entities/rows';
import sql = require('./sql');
/** @returns {string|null} */ function getOptionOrNull(name: string): string | null {
function getOptionOrNull(name) {
let option; let option;
if (becca.loaded) { if (becca.loaded) {
option = becca.getOption(name); option = becca.getOption(name);
} else { } else {
// e.g. in initial sync becca is not loaded because DB is not initialized // e.g. in initial sync becca is not loaded because DB is not initialized
option = sql.getRow("SELECT * FROM options WHERE name = ?", [name]); option = sql.getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
} }
return option ? option.value : null; return option ? option.value : null;
} }
/** @returns {string} */ function getOption(name: string): string {
function getOption(name) {
const val = getOptionOrNull(name); const val = getOptionOrNull(name);
if (val === null) { if (val === null) {
@ -26,8 +25,7 @@ function getOption(name) {
return val; return val;
} }
/** @returns {int} */ function getOptionInt(name: string, defaultValue?: number): number {
function getOptionInt(name, defaultValue = undefined) {
const val = getOption(name); const val = getOption(name);
const intVal = parseInt(val); const intVal = parseInt(val);
@ -43,8 +41,7 @@ function getOptionInt(name, defaultValue = undefined) {
return intVal; return intVal;
} }
/** @returns {boolean} */ function getOptionBool(name: string): boolean {
function getOptionBool(name) {
const val = getOption(name); const val = getOption(name);
if (!['true', 'false'].includes(val)) { if (!['true', 'false'].includes(val)) {
@ -54,7 +51,7 @@ function getOptionBool(name) {
return val === 'true'; return val === 'true';
} }
function setOption(name, value) { function setOption(name: string, value: string | boolean) {
if (value === true || value === false) { if (value === true || value === false) {
value = value.toString(); value = value.toString();
} }
@ -71,9 +68,9 @@ function setOption(name, value) {
} }
} }
function createOption(name, value, isSynced) { function createOption(name: string, value: string, isSynced: boolean) {
// to avoid circular dependency, need to find a better solution // to avoid circular dependency, need to find a better solution
const BOption = require('../becca/entities/boption.js'); const BOption = require('../becca/entities/boption');
new BOption({ new BOption({
name: name, name: name,
@ -87,7 +84,7 @@ function getOptions() {
} }
function getOptionMap() { function getOptionMap() {
const map = {}; const map: Record<string, string> = {};
for (const option of Object.values(becca.options)) { for (const option of Object.values(becca.options)) {
map[option.name] = option.value; map[option.name] = option.value;