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