Mise à jour et ajout de controllers, routes, services et utilities
This commit is contained in:
+58
-17
@@ -5,25 +5,25 @@ const mongoose = require('mongoose');
|
||||
const DB = require('./mysql');
|
||||
const sequelize = require('./config/sequelize');
|
||||
const associate = require('./relationships');
|
||||
const utils = require('../utils');
|
||||
const DateUtilities = require('../utils/dateUtilities');
|
||||
|
||||
|
||||
|
||||
// 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);
|
||||
await mongoose.connect(process.env.MONGODB_URI);
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green('MongoDB connection has been established successfully 🟢'));
|
||||
} 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 🔴'));
|
||||
});
|
||||
await mongoose.connect(process.env.MONGODB_URI);
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green('MongoDB connection has been established successfully 🟢'));
|
||||
mongoose.set('debug', process.env.DEBUG);
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.red('debug :'), chalk.yellow(process.env.DEBUG));
|
||||
console.log(`${DateUtilities.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.log(`${DateUtilities.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);
|
||||
@@ -34,18 +34,54 @@ const connectMongoDb = async () => {
|
||||
const connectMysqlDb = async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.green('MySQL connection has been established successfully 🟢'));
|
||||
console.log(`${DateUtilities.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,
|
||||
// Check that required tables exist instead of synchronizing (no auto-create)
|
||||
const queryInterface = sequelize.getQueryInterface();
|
||||
let existingTables = [];
|
||||
try {
|
||||
existingTables = await queryInterface.showAllTables();
|
||||
} catch (err) {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.red('Unable to list tables from MySQL 🔴'));
|
||||
console.error(err);
|
||||
// Exit immediately if unable to list tables
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Build expected table list from models exported in DB
|
||||
const expectedTables = [];
|
||||
Object.keys(DB).forEach((key) => {
|
||||
const model = DB[key];
|
||||
if (model && typeof model.getTableName === 'function') {
|
||||
try {
|
||||
const t = model.getTableName();
|
||||
const tableName = typeof t === 'string' ? t : t.tableName;
|
||||
if (tableName) expectedTables.push(tableName);
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
} catch (err) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Call the associate function to create the relationships between the models
|
||||
// Normalize table names to strings for comparison
|
||||
const existingNormalized = existingTables.map(t => (typeof t === 'string' ? t : t.tableName)).map(t => t.toString());
|
||||
|
||||
const missing = expectedTables.filter(t => !existingNormalized.includes(t) && !existingNormalized.includes(t.toLowerCase()));
|
||||
|
||||
if (missing.length > 0) {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.red('Database schema validation failed - missing tables:'), chalk.yellow(missing.join(', ')));
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.red('Run migrations to create the schema: npx sequelize-cli db:migrate'));
|
||||
// Exit immediately if schema is incomplete
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// If all expected tables are present, create associations
|
||||
associate();
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green('Database schema validated successfully 🟢'));
|
||||
|
||||
} catch (error) {
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.red('Unable to connect to the MySQL database 🔴'));
|
||||
console.log(`${DateUtilities.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);
|
||||
@@ -55,8 +91,13 @@ const connectMysqlDb = async () => {
|
||||
|
||||
// Connect to all database and log a message to the console
|
||||
const connectAllDb = async () => {
|
||||
connectMongoDb();
|
||||
connectMysqlDb();
|
||||
try {
|
||||
await connectMongoDb();
|
||||
await connectMysqlDb();
|
||||
} catch (error) {
|
||||
console.error('Failed to connect to databases:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user