initConfig.js 789 B

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