flame/db.js

32 lines
727 B
JavaScript
Raw Normal View History

const { Sequelize } = require('sequelize');
2021-06-22 15:49:00 +03:00
const Logger = require('./utils/Logger');
const logger = new Logger();
const sequelize = new Sequelize({
dialect: 'sqlite',
2021-06-05 16:48:43 +03:00
storage: './data/db.sqlite',
logging: false
})
const connectDB = async () => {
try {
await sequelize.authenticate();
2021-06-22 15:49:00 +03:00
logger.log('Connected to database');
2021-06-24 13:53:45 +03:00
const syncModels = true;
2021-06-05 16:48:43 +03:00
if (syncModels) {
2021-06-22 15:49:00 +03:00
logger.log('Starting model synchronization');
await sequelize.sync({ alter: true });
2021-06-22 15:49:00 +03:00
logger.log('All models were synchronized');
}
} catch (error) {
2021-06-22 15:49:00 +03:00
logger.log(`Unable to connect to the database: ${error.message}`, 'ERROR');
process.exit(1);
}
}
module.exports = {
connectDB,
sequelize
}