config-manager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const ConfigLoader = require('../service/config-loader')
  2. , debug = require('debug')('growi:service:ConfigManager');
  3. class ConfigManager {
  4. constructor(configModel) {
  5. this.configModel = configModel;
  6. this.configLoader = new ConfigLoader(this.configModel);
  7. this.configObject = null;
  8. }
  9. /**
  10. * load configs from the database and the environment variables
  11. */
  12. async loadConfigs() {
  13. this.configObject = await this.configLoader.load();
  14. debug('ConfigManager#loadConfigs', this.configObject);
  15. }
  16. /**
  17. * get a config specified by namespace & key
  18. *
  19. * Basically, search a specified config from configs loaded from database at first
  20. * and then from configs loaded from env vars.
  21. *
  22. * In some case, this search method changes.(not yet implemented)
  23. */
  24. getConfig(namespace, key) {
  25. return this.defaultSearch(namespace, key);
  26. }
  27. /**
  28. * private api
  29. *
  30. * Search a specified config from configs loaded from database at first
  31. * and then from configs loaded from env vars.
  32. *
  33. * the followings are the meanings of each special return value.
  34. * - null: a specified config is not set.
  35. * - undefined: a specified config does not exist.
  36. */
  37. defaultSearch(namespace, key) {
  38. if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  39. return undefined;
  40. }
  41. if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key) ) {
  42. return this.configObject.fromDB[namespace][key];
  43. }
  44. if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key) ) {
  45. return this.configObject.fromEnvVars[namespace][key];
  46. }
  47. if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key) ) {
  48. if (this.configObject.fromDB[namespace][key] !== null) {
  49. return this.configObject.fromDB[namespace][key];
  50. }
  51. else {
  52. return this.configObject.fromEnvVars[namespace][key];
  53. }
  54. }
  55. }
  56. /**
  57. * check whether a specified config exists in configs loaded from the database
  58. * @returns {boolean}
  59. */
  60. configExistsInDB(namespace, key) {
  61. if (this.configObject.fromDB[namespace] === undefined) {
  62. return false;
  63. }
  64. return this.configObject.fromDB[namespace][key] !== undefined;
  65. }
  66. /**
  67. * check whether a specified config exists in configs loaded from the environment variables
  68. * @returns {boolean}
  69. */
  70. configExistsInEnvVars(namespace, key) {
  71. if (this.configObject.fromEnvVars[namespace] === undefined) {
  72. return false;
  73. }
  74. return this.configObject.fromEnvVars[namespace][key] !== undefined;
  75. }
  76. /**
  77. * update configs by a iterable object consisting of several objects with ns, key, value fields
  78. *
  79. * For example:
  80. * ```
  81. * updateConfigs(
  82. * [{
  83. * ns: 'some namespace 1',
  84. * key: 'some key 1',
  85. * value: 'some value 1'
  86. * }, {
  87. * ns: 'some namespace 2',
  88. * key: 'some key 2',
  89. * value: 'some value 2'
  90. * }]
  91. * );
  92. * ```
  93. */
  94. async updateConfigs(configs) {
  95. for (const config of configs) {
  96. this.configModel.findOneAndUpdate(
  97. { ns: config.ns, key: config.key },
  98. { ns: config.ns, key: config.key, value: JSON.stringify(config.value) },
  99. { upsert: true, },
  100. function(err, config) {
  101. debug('ConfigManager#updateConfigs', err, config);
  102. });
  103. }
  104. await this.loadConfigs();
  105. }
  106. }
  107. module.exports = ConfigManager;