Réorganisation des sources

This commit is contained in:
Rampeur
2025-08-15 17:30:39 +02:00
parent ea2d177c4c
commit e17fecc554
+68
View File
@@ -0,0 +1,68 @@
const chalk = require('chalk');
const mongoose = require('mongoose');
const DB = require('.');
const sequelize = require('./config/sequelize');
const associate = require('./relationships');
const utils = require('../utils');
// Connect to the MongoDB database and log a message to the console
const connectMongoDb = async () => {
try {
if (process.env.APP_ENV === 'production') {
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.set('strictQuery', true);
mongoose.connect(process.env.MONGODB_URI).then(() => {
console.log(`${utils.getDateTimeISOString()}`, chalk.green('MongoDB connection has been established successfully 🟢'));
}).catch(() => {
console.log(`${utils.getDateTimeISOString()}`, chalk.red('Unable to connect to the MongoDB database 🔴'));
});
mongoose.set('debug', process.env.DEBUG);
console.log(`${utils.getDateTimeISOString()}`, chalk.red('debug :'), chalk.yellow(process.env.DEBUG));
}
} catch (error) {
console.log(`${utils.getDateTimeISOString()}`, chalk.red('Unable to connect to the MongoDB database 🔴'));
console.error('MongoDB connection error:', error);
// Exit the process if the connection is not successful
process.exit(1);
}
};
// Connect to the MySQL database and log a message to the console
const connectMysqlDb = async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully 🔹');
console.log(`${utils.getDateTimeISOString()}`, chalk.green('MySQL connection has been established successfully 🟢'));
// Synchronize the database with the models without need of dropping the tables
await DB.sequelize.sync({
force: false,
});
// Call the associate function to create the relationships between the models
associate();
} catch (error) {
console.log(`${utils.getDateTimeISOString()}`, chalk.red('Unable to connect to the MySQL database 🔴'));
console.error('MySQL connection error:', error);
// Exit the process if the connection is not successful
process.exit(1);
}
};
// Connect to all database and log a message to the console
const connectAllDb = async () => {
connectMongoDb();
connectMysqlDb();
};
module.exports.connectAllDb = connectAllDb;
module.exports.connectMongoDb = connectMongoDb;
module.exports.connectMysqlDb = connectMysqlDb;
module.exports.default = connectAllDb;