flame/db.js

33 lines
723 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
2021-08-06 16:15:54 +03:00
});
const connectDB = async () => {
try {
await sequelize.authenticate();
2021-06-22 15:49:00 +03:00
logger.log('Connected to database');
2021-08-06 16:15:54 +03:00
2021-06-24 13:53:45 +03:00
const syncModels = true;
2021-08-06 16:15:54 +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);
}
2021-08-06 16:15:54 +03:00
};
module.exports = {
connectDB,
sequelize
2021-08-06 16:15:54 +03:00
};