Просмотр исходного кода

improve loading configs from DB

Yuki Takei 1 год назад
Родитель
Сommit
b90f865fe7

+ 4 - 6
apps/app/src/server/service/config-manager/config-loader.ts

@@ -42,12 +42,10 @@ export class ConfigLoader implements IConfigLoader<ConfigKey, ConfigValues> {
     const docs = await Config.find().exec();
 
     for (const doc of docs) {
-      if (doc.key in CONFIG_DEFINITIONS) {
-        dbConfig[doc.key as ConfigKey] = {
-          definition: CONFIG_DEFINITIONS[doc.key as ConfigKey],
-          value: doc.value ? JSON.parse(doc.value) : null,
-        };
-      }
+      dbConfig[doc.key as ConfigKey] = {
+        definition: (doc.key in CONFIG_DEFINITIONS) ? CONFIG_DEFINITIONS[doc.key as ConfigKey] : undefined,
+        value: doc.value ? JSON.parse(doc.value) : null,
+      };
     }
 
     logger.debug('loadFromDB', dbConfig);

+ 2 - 2
apps/app/src/server/service/config-manager/config-manager.ts

@@ -79,7 +79,7 @@ export class ConfigManager implements IConfigManager<ConfigKey, ConfigValues>, S
       return this.envConfig[key].value as ConfigValues[K];
     }
 
-    return (this.dbConfig[key] ?? this.envConfig[key]).value as ConfigValues[K];
+    return (this.dbConfig[key] ?? this.envConfig[key])?.value as ConfigValues[K];
   }
 
   private shouldUseEnvOnly(key: ConfigKey): boolean {
@@ -172,7 +172,7 @@ export class ConfigManager implements IConfigManager<ConfigKey, ConfigValues>, S
     const envVars = {} as Record<string, string>;
 
     for (const { definition, value } of Object.values(this.envConfig)) {
-      if (definition.envVarName == null) {
+      if (definition?.envVarName == null) {
         continue;
       }
 

+ 1 - 1
packages/core/src/interfaces/config-manager.ts

@@ -36,8 +36,8 @@ export interface IConfigLoader<K extends string, V extends Record<K, any>> {
 
 // eslint-disable-next-line @typescript-eslint/no-explicit-any
 export type RawConfigData<K extends string, V extends Record<K, any>> = Record<K, {
-  definition: ConfigDefinition<V[K]>;
   value: V[K];
+  definition?: ConfigDefinition<V[K]>;
 }>;
 
 export type UpdateConfigOptions = { skipPubsub?: boolean };