flame/db.js

33 lines
723 B
JavaScript
Raw Normal View History

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