flame/utils/initConfig.js

36 lines
788 B
JavaScript
Raw Normal View History

const { Op } = require('sequelize');
const Config = require('../models/Config');
const { config } = require('./initialConfig.json');
2021-06-22 15:49:00 +03:00
const Logger = require('./Logger');
const logger = new Logger();
const initConfig = async () => {
// Get config values
const configPairs = await Config.findAll({
where: {
key: {
2021-09-06 13:24:01 +03:00
[Op.or]: config.map((pair) => pair.key),
},
},
});
// Get key from each pair
const configKeys = configPairs.map((pair) => pair.key);
// Create missing pairs
2021-09-06 13:24:01 +03:00
config.forEach(async ({ key, value }) => {
if (!configKeys.includes(key)) {
await Config.create({
key,
value,
2021-09-06 13:24:01 +03:00
valueType: typeof value,
});
}
2021-09-06 13:24:01 +03:00
});
2021-06-22 15:49:00 +03:00
logger.log('Initial config created');
return;
2021-09-06 13:24:01 +03:00
};
2021-09-06 13:24:01 +03:00
module.exports = initConfig;