const { Sequelize } = require('sequelize'); const logger = require('../utils/logger'); const { getConfig } = require('./databaseConfig'); const sequelize = new Sequelize(getConfig()); // Test database connection const testConnection = async () => { try { await sequelize.authenticate(); logger.info('Database connection established successfully'); return true; } catch (error) { logger.error('Unable to connect to the database:', error); return false; } }; // Sync database (create tables) const syncDatabase = async (force = false) => { try { await sequelize.sync({ force, alter: !force }); logger.info(`Database synchronized successfully (force: ${force})`); return true; } catch (error) { logger.error('Database synchronization failed:', error); return false; } }; // Drop all tables const dropDatabase = async () => { try { await sequelize.drop(); logger.info('Database dropped successfully'); return true; } catch (error) { logger.error('Database drop failed:', error); return false; } }; // Close database connection const closeConnection = async () => { try { await sequelize.close(); logger.info('Database connection closed'); return true; } catch (error) { logger.error('Error closing database connection:', error); return false; } }; module.exports = { sequelize, testConnection, syncDatabase, dropDatabase, closeConnection };