This repository has been archived on 2026-01-22. You can view files and clone it, but cannot push or open issues or pull requests.
factuges_2025/apps/server/src/config/database.ts
2025-04-27 22:47:47 +02:00

49 lines
1.2 KiB
TypeScript

import { logger } from "@rdx/logger";
import dotenv from "dotenv";
import { Sequelize } from "sequelize";
dotenv.config();
export const sequelize = new Sequelize(
process.env.DB_NAME as string, // database
process.env.DB_USER as string, // username
process.env.DB_PASSWORD as string, // password
{
host: process.env.DB_HOST as string,
dialect: "mysql",
port: parseInt(process.env.DB_PORT || "3306", 10),
dialectOptions: {
multipleStatements: true,
dateStrings: true,
typeCast: true,
//timezone: "Z",
},
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000,
},
logQueryParameters: true,
logging: process.env.DB_LOGGING === "true" ? logger.debug : false,
define: {
charset: "utf8mb4",
collate: "utf8mb4_unicode_ci",
//freezeTableName: true,
underscored: true,
timestamps: true,
},
}
);
export async function connectToDatabase(): Promise<void> {
try {
await sequelize.authenticate();
//await registerModels();
logger.info(`✔️${" "}Database connection established successfully.`);
} catch (error) {
logger.error("❌ Unable to connect to the database:", error);
process.exit(1);
}
}