config-manager.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const ConfigLoader = require('../service/config-loader')
  2. , debug = require('debug')('growi:models:config');
  3. class ConfigManager {
  4. constructor(configModel) {
  5. this.configModel = configModel;
  6. this.configLoader = new ConfigLoader(this.configModel);
  7. this.configObject = null;
  8. }
  9. async loadConfigs() {
  10. this.configObject = await this.configLoader.load();
  11. }
  12. getConfig(namespace, key) {
  13. return this.defaultSearch(namespace, key);
  14. }
  15. defaultSearch(namespace, key) {
  16. if (this.configObject.fromDB[namespace][key] !== undefined) {
  17. return this.configObject.fromDB[namespace][key];
  18. }
  19. else {
  20. return this.configObject.fromEnvVars[namespace][key];
  21. }
  22. }
  23. async updateConfigs(configs) {
  24. for (const config of configs) {
  25. this.configModel.findOneAndUpdate(
  26. { ns: config.ns, key: config.key },
  27. { ns: config.ns, key: config.key, value: JSON.stringify(config.value) },
  28. { upsert: true, },
  29. function(err, config) {
  30. debug('Config.findAndUpdate', err, config);
  31. });
  32. }
  33. await this.loadConfigs();
  34. }
  35. }
  36. module.exports = ConfigManager;