Source

classes/settings.ts

import { prisma } from "../../prisma";
import { RequestContext } from "./requestContext";

/**
 * Static class handling the mod settings
 */
class Settings {
    /**
     * Do NOT instantiate settings
     * @throws {Error}
     * @deprecated
     */
    constructor() {
        throw new Error("Cannot instantiate settings class");
    }

    /**
     * Gets the setting from database
     * @param {string} setting
     * @param {RequestContext} ctx
     * @return {Promise<string>}
     */
    static async get(setting: string, ctx?: RequestContext): Promise<string | undefined> {
        const res = await prisma.setting.findUnique({
            where: {
                name: setting
            }
        });
        return res?.value;
    }

    /**
     * Sets the setting
     * @param {string} setting
     * @param {string} value
     * @param {RequestContext} ctx
     * @return {Promise<void>}
     */
    static async set(setting: string, value: string, ctx?: RequestContext): Promise<void> {
        await prisma.setting.upsert({
            where: {
                name: setting
            },
            create: {
                name: setting,
                value
            },
            update: {
                value
            }
        });
    }
};

export default Settings;