فهرست منبع

implement getConfigFromDB & getConfigFromEnvVars

utsushiiro 7 سال پیش
والد
کامیت
09004c2f80
1فایلهای تغییر یافته به همراه74 افزوده شده و 39 حذف شده
  1. 74 39
      src/server/service/config-manager.js

+ 74 - 39
src/server/service/config-manager.js

@@ -21,8 +21,8 @@ class ConfigManager {
   /**
    * get a config specified by namespace & key
    *
-   * Basically, search a specified config from configs loaded from database at first
-   * and then from configs loaded from env vars.
+   * Basically, search a specified config from configs loaded from the database at first
+   * and then from configs loaded from the environment variables.
    *
    * In some case, this search method changes.
    *
@@ -38,11 +38,63 @@ class ConfigManager {
     return this.defaultSearch(namespace, key);
   }
 
+  /**
+   * get a config specified by namespace & key from configs loaded from the database
+   *
+   * **Do not use this unless absolutely necessary. Use getConfig instead.**
+   */
+  getConfigFromDB(namespace, key) {
+    return this.searchOnlyFromDBConfigs(namespace, key);
+  }
+
+  /**
+   * get a config specified by namespace & key from configs loaded from the environment variables
+   *
+   * **Do not use this unless absolutely necessary. Use getConfig instead.**
+   */
+  getConfigFromEnvVars(namespace, key) {
+    return this.searchOnlyFromEnvVarConfigs(namespace, key);
+  }
+
+  /**
+   * update configs by a iterable object consisting of several objects with ns, key, value fields
+   *
+   * For example:
+   * ```
+   *  updateConfigs(
+   *   [{
+   *     ns:    'some namespace 1',
+   *     key:   'some key 1',
+   *     value: 'some value 1'
+   *   }, {
+   *     ns:    'some namespace 2',
+   *     key:   'some key 2',
+   *     value: 'some value 2'
+   *   }]
+   *  );
+   * ```
+   */
+  async updateConfigs(configs) {
+    const results = [];
+    for (const config of configs) {
+      results.push(
+        this.configModel.findOneAndUpdate(
+          { ns: config.ns, key: config.key },
+          { ns: config.ns, key: config.key, value: JSON.stringify(config.value) },
+          { upsert: true, }
+        ).exec()
+      );
+    }
+    await Promise.all(results);
+
+    await this.loadConfigs();
+  }
+
   /**
    * private api
    *
-   * Search a specified config from configs loaded from database at first
-   * and then from configs loaded from env vars.
+   * Search a specified config from configs loaded from the database at first
+   * and then from configs loaded from the environment variables.
    */
   defaultSearch(namespace, key) {
     if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
@@ -70,7 +122,20 @@ class ConfigManager {
   /**
    * private api
    *
-   * Search a specified config from configs loaded from env vars.
+   * Search a specified config from configs loaded from the database
+   */
+  searchOnlyFromDBConfigs(namespace, key) {
+    if (!this.configExistsInDB(namespace, key)) {
+      return undefined;
+    }
+
+    return this.configObject.fromEnvVars[namespace][key];
+  }
+
+  /**
+   * private api
+   *
+   * Search a specified config from configs loaded from the environment variables
    */
   searchOnlyFromEnvVarConfigs(namespace, key) {
     if (!this.configExistsInEnvVars(namespace, key)) {
@@ -81,6 +146,8 @@ class ConfigManager {
   }
 
   /**
+   * private api
+   *
    * check whether a specified config exists in configs loaded from the database
    * @returns {boolean}
    */
@@ -93,6 +160,8 @@ class ConfigManager {
   }
 
   /**
+   * private api
+   *
    * check whether a specified config exists in configs loaded from the environment variables
    * @returns {boolean}
    */
@@ -103,40 +172,6 @@ class ConfigManager {
 
     return this.configObject.fromEnvVars[namespace][key] !== undefined;
   }
-
-  /**
-   * update configs by a iterable object consisting of several objects with ns, key, value fields
-   *
-   * For example:
-   * ```
-   *  updateConfigs(
-   *   [{
-   *     ns:    'some namespace 1',
-   *     key:   'some key 1',
-   *     value: 'some value 1'
-   *   }, {
-   *     ns:    'some namespace 2',
-   *     key:   'some key 2',
-   *     value: 'some value 2'
-   *   }]
-   *  );
-   * ```
-   */
-  async updateConfigs(configs) {
-    const results = [];
-    for (const config of configs) {
-      results.push(
-        this.configModel.findOneAndUpdate(
-          { ns: config.ns, key: config.key },
-          { ns: config.ns, key: config.key, value: JSON.stringify(config.value) },
-          { upsert: true, }
-        ).exec()
-      );
-    }
-    await Promise.all(results);
-
-    await this.loadConfigs();
-  }
 }
 
 module.exports = ConfigManager;