2021-05-10 18:47:26 +03:00
|
|
|
const { Sequelize } = require('sequelize');
|
2021-06-22 15:49:00 +03:00
|
|
|
const Logger = require('./utils/Logger');
|
|
|
|
const logger = new Logger();
|
2021-05-10 18:47:26 +03:00
|
|
|
|
|
|
|
const sequelize = new Sequelize({
|
|
|
|
dialect: 'sqlite',
|
2021-06-05 16:48:43 +03:00
|
|
|
storage: './data/db.sqlite',
|
|
|
|
logging: false
|
2021-06-22 14:07:32 +03:00
|
|
|
})
|
2021-05-10 18:47:26 +03:00
|
|
|
|
|
|
|
const connectDB = async () => {
|
|
|
|
try {
|
2021-06-09 13:45:55 +03:00
|
|
|
await sequelize.authenticate();
|
2021-06-22 15:49:00 +03:00
|
|
|
logger.log('Connected to database');
|
|
|
|
|
|
|
|
const syncModels = false;
|
2021-06-05 16:48:43 +03:00
|
|
|
|
2021-06-22 14:07:32 +03:00
|
|
|
if (syncModels) {
|
2021-06-22 15:49:00 +03:00
|
|
|
logger.log('Starting model synchronization');
|
2021-06-22 14:07:32 +03:00
|
|
|
await sequelize.sync({ alter: true });
|
2021-06-22 15:49:00 +03:00
|
|
|
logger.log('All models were synchronized');
|
2021-06-22 14:07:32 +03:00
|
|
|
}
|
2021-05-10 18:47:26 +03:00
|
|
|
} 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-05-10 18:47:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
connectDB,
|
|
|
|
sequelize
|
2021-06-22 14:07:32 +03:00
|
|
|
}
|