flame/db/index.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2021-10-05 13:29:17 +03:00
const { Sequelize } = require('sequelize');
const { join } = require('path');
const fs = require('fs');
const Umzug = require('umzug');
const backupDB = require('./utils/backupDb');
2021-10-05 13:29:17 +03:00
const Logger = require('../utils/Logger');
const logger = new Logger();
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './data/db.sqlite',
logging: false,
});
const umzug = new Umzug({
migrations: {
path: join(__dirname, './migrations'),
params: [sequelize.getQueryInterface()],
},
storage: 'sequelize',
storageOptions: {
sequelize,
},
});
const connectDB = async () => {
try {
backupDB();
2021-10-05 13:29:17 +03:00
await sequelize.authenticate();
logger.log('Connected to database');
// migrations
const pendingMigrations = await umzug.pending();
if (pendingMigrations.length > 0) {
logger.log('Executing pending migrations');
await umzug.up();
}
} catch (error) {
logger.log(`Unable to connect to the database: ${error.message}`, 'ERROR');
process.exit(1);
}
};
module.exports = {
connectDB,
sequelize,
};