initConfig.js 788 B

1234567891011121314151617181920212223242526272829303132333435
  1. const { Op } = require('sequelize');
  2. const Config = require('../models/Config');
  3. const { config } = require('./initialConfig.json');
  4. const Logger = require('./Logger');
  5. const logger = new Logger();
  6. const initConfig = async () => {
  7. // Get config values
  8. const configPairs = await Config.findAll({
  9. where: {
  10. key: {
  11. [Op.or]: config.map((pair) => pair.key),
  12. },
  13. },
  14. });
  15. // Get key from each pair
  16. const configKeys = configPairs.map((pair) => pair.key);
  17. // Create missing pairs
  18. config.forEach(async ({ key, value }) => {
  19. if (!configKeys.includes(key)) {
  20. await Config.create({
  21. key,
  22. value,
  23. valueType: typeof value,
  24. });
  25. }
  26. });
  27. logger.log('Initial config created');
  28. return;
  29. };
  30. module.exports = initConfig;