initConfig.js 712 B

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