flame/db/migrations/01_new-config.js

38 lines
915 B
JavaScript
Raw Normal View History

2021-10-21 18:14:25 +03:00
const { readFile, writeFile, copyFile } = require('fs/promises');
const Config = require('../../models/Config');
const up = async (query) => {
await copyFile('utils/init/initialConfig.json', 'data/config.json');
const initConfigFile = await readFile('data/config.json', 'utf-8');
const parsedNewConfig = JSON.parse(initConfigFile);
const existingConfig = await Config.findAll({ raw: true });
for (let pair of existingConfig) {
const { key, value, valueType } = pair;
let newValue = value;
if (valueType == 'number') {
newValue = parseFloat(value);
} else if (valueType == 'boolean') {
newValue = value == 1;
}
parsedNewConfig[key] = newValue;
}
const newConfig = JSON.stringify(parsedNewConfig);
await writeFile('data/config.json', newConfig);
2021-10-22 17:10:38 +03:00
await query.dropTable('config');
2021-10-21 18:14:25 +03:00
};
2021-10-22 17:10:38 +03:00
const down = async (query) => {};
2021-10-21 18:14:25 +03:00
module.exports = {
up,
down,
};