✨ key value config
This commit is contained in:
parent
e2a22da5c4
commit
39f4aa86da
@ -1,12 +1,15 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne } from "typeorm";
|
||||
import { BaseClass } from "./BaseClass";
|
||||
import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from "typeorm";
|
||||
import { BaseClass, BaseClassWithoutId } from "./BaseClass";
|
||||
import crypto from "crypto";
|
||||
import { Snowflake } from "../util/Snowflake";
|
||||
|
||||
@Entity("config")
|
||||
export class ConfigEntity extends BaseClass {
|
||||
@Column({ type: "simple-json" })
|
||||
value: ConfigValue;
|
||||
export class ConfigEntity extends BaseClassWithoutId {
|
||||
@PrimaryColumn()
|
||||
key: string;
|
||||
|
||||
@Column({ type: "simple-json", nullable: true })
|
||||
value: number | boolean | null | string | undefined;
|
||||
}
|
||||
|
||||
export interface RateLimitOptions {
|
||||
@ -37,14 +40,16 @@ export interface KafkaBroker {
|
||||
export interface ConfigValue {
|
||||
gateway: {
|
||||
endpointClient: string | null;
|
||||
endpoint: string | null;
|
||||
endpointPrivate: string | null;
|
||||
endpointPublic: string | null;
|
||||
};
|
||||
cdn: {
|
||||
endpointClient: string | null;
|
||||
endpoint: string | null;
|
||||
endpointPublic: string | null;
|
||||
endpointPrivate: string | null;
|
||||
};
|
||||
general: {
|
||||
instance_id: string;
|
||||
instanceId: string;
|
||||
};
|
||||
permissions: {
|
||||
user: {
|
||||
@ -149,14 +154,16 @@ export interface ConfigValue {
|
||||
export const DefaultConfigOptions: ConfigValue = {
|
||||
gateway: {
|
||||
endpointClient: null,
|
||||
endpoint: null,
|
||||
endpointPrivate: null,
|
||||
endpointPublic: null,
|
||||
},
|
||||
cdn: {
|
||||
endpointClient: null,
|
||||
endpoint: null,
|
||||
endpointPrivate: null,
|
||||
endpointPublic: null,
|
||||
},
|
||||
general: {
|
||||
instance_id: Snowflake.generate(),
|
||||
instanceId: Snowflake.generate(),
|
||||
},
|
||||
permissions: {
|
||||
user: {
|
||||
|
@ -1,22 +1,66 @@
|
||||
import "missing-native-js-functions";
|
||||
import { ConfigValue, ConfigEntity, DefaultConfigOptions } from "../entities/Config";
|
||||
|
||||
var config: ConfigEntity;
|
||||
var config: ConfigValue;
|
||||
var pairs: ConfigEntity[];
|
||||
|
||||
// TODO: use events to inform about config updates
|
||||
// Config keys are separated with _
|
||||
|
||||
export const Config = {
|
||||
init: async function init() {
|
||||
if (config) return config;
|
||||
config = (await ConfigEntity.findOne({ id: "0" })) || new ConfigEntity({ id: "0" });
|
||||
pairs = await ConfigEntity.find();
|
||||
config = pairsToConfig(pairs);
|
||||
|
||||
return this.set((config.value || {}).merge(DefaultConfigOptions));
|
||||
return this.set((config || {}).merge(DefaultConfigOptions));
|
||||
},
|
||||
get: function get() {
|
||||
return config.value as ConfigValue;
|
||||
return config;
|
||||
},
|
||||
set: function set(val: Partial<ConfigValue>) {
|
||||
if (!config) return;
|
||||
config.value = val.merge(config?.value || {});
|
||||
return config.save();
|
||||
if (!config || !val) return;
|
||||
config = val.merge(config);
|
||||
console.log(config);
|
||||
|
||||
return applyConfig(config);
|
||||
},
|
||||
};
|
||||
|
||||
function applyConfig(val: ConfigValue) {
|
||||
async function apply(obj: any, key = ""): Promise<any> {
|
||||
if (typeof obj === "object" && obj !== null)
|
||||
return Promise.all(Object.keys(obj).map((k) => apply(obj[k], key ? `${key}_${k}` : k)));
|
||||
|
||||
let pair = pairs.find((x) => x.key === key);
|
||||
if (!pair) pair = new ConfigEntity();
|
||||
|
||||
pair.key = key;
|
||||
pair.value = obj;
|
||||
return pair.save();
|
||||
}
|
||||
|
||||
return apply(val);
|
||||
}
|
||||
|
||||
function pairsToConfig(pairs: ConfigEntity[]) {
|
||||
var value: any = {};
|
||||
|
||||
pairs.forEach((p) => {
|
||||
const keys = p.key.split("_");
|
||||
let prev = "";
|
||||
let obj = value;
|
||||
let i = 0;
|
||||
|
||||
for (const key of keys) {
|
||||
if (Number(key) && !obj[prev]) obj = obj[prev] = [];
|
||||
if (i++ === keys.length - 1) obj[key] = p.value;
|
||||
else if (!obj[key]) obj[key] = {};
|
||||
|
||||
prev = key;
|
||||
obj = obj[key];
|
||||
}
|
||||
});
|
||||
|
||||
return value as ConfigValue;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ export function initDatabase() {
|
||||
//
|
||||
entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
|
||||
synchronize: true,
|
||||
logging: true,
|
||||
logging: false,
|
||||
cache: {
|
||||
duration: 1000 * 3, // cache all find queries for 3 seconds
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user