You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/hydrooj/src/model/system.ts

84 lines
3.1 KiB
TypeScript

import { SYSTEM_SETTINGS } from './setting';
import { NumberKeys } from '../typeutils';
import { SystemKeys } from '../interface';
import db from '../service/db';
import * as bus from '../service/bus';
5 years ago
const coll = db.collection('system');
const cache: Record<string, any> = {};
export function get<K extends keyof SystemKeys>(key: K): SystemKeys[K];
export function get(key: string): any;
4 years ago
export function get(key: string): any {
return cache[key];
}
4 years ago
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys,
3 years ago
>(keys: [A, B]): [SystemKeys[A], SystemKeys[B]];
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
3 years ago
>(keys: [A, B, C]): [SystemKeys[A], SystemKeys[B], SystemKeys[C]];
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys,
3 years ago
>(keys: [A, B, C, D]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]];
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys,
3 years ago
>(keys: [A, B, C, D, E]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]];
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys, F extends keyof SystemKeys,
3 years ago
>(keys: [A, B, C, D, E, F]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]];
export function getMany(keys: (keyof SystemKeys)[]): any[];
export function getMany(keys: string[]): any[] {
return keys.map((key) => cache[key]);
4 years ago
}
3 years ago
export async function set<K extends keyof SystemKeys>(_id: K, value: SystemKeys[K], broadcast?: boolean): Promise<SystemKeys[K]>;
export async function set<K>(_id: string, value: K, broadcast?: boolean): Promise<K>;
export async function set(_id: string, value: any, broadcast = true) {
if (broadcast) bus.broadcast('system/setting', { [_id]: value });
const res = await coll.findOneAndUpdate(
{ _id },
{ $set: { value } },
3 years ago
{ upsert: true, returnDocument: 'after' },
);
cache[_id] = res.value.value;
return res.value.value;
}
export async function inc<K extends NumberKeys<SystemKeys>>(_id: K) {
const res = await coll.findOneAndUpdate(
{ _id },
// FIXME NumberKeys<>
// @ts-ignore
{ $inc: { value: 1 } },
3 years ago
{ upsert: true, returnDocument: 'after' },
);
cache[_id] = res.value.value;
return res.value.value;
}
4 years ago
export async function runConfig() {
for (const setting of SYSTEM_SETTINGS) {
if (setting.value) cache[setting.key] = setting.value;
}
const config = await coll.find().toArray();
for (const i of config) cache[i._id] = i.value;
await bus.emit('database/config');
}
bus.on('system/setting', (args) => {
for (const key in args) set(key, args[key], false);
});
global.Hydro.model.system = {
runConfig,
get,
4 years ago
getMany,
inc,
set,
};