Uecko_ERP/apps/server/src/config/database.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-04-22 15:09:57 +00:00
import { logger } from "core/common/infrastructure/logger";
2025-01-29 16:01:17 +00:00
import dotenv from "dotenv";
2025-02-01 21:48:13 +00:00
import { Sequelize } from "sequelize";
2025-01-29 16:01:17 +00:00
dotenv.config();
export const sequelize = new Sequelize(
2025-02-03 19:50:16 +00:00
process.env.DB_NAME as string, // database
process.env.DB_USER as string, // username
process.env.DB_PASSWORD as string, // password
2025-01-29 16:01:17 +00:00
{
host: process.env.DB_HOST as string,
2025-03-04 17:08:33 +00:00
dialect: "mysql",
2025-01-29 16:01:17 +00:00
port: parseInt(process.env.DB_PORT || "3306", 10),
2025-02-03 19:50:16 +00:00
dialectOptions: {
multipleStatements: true,
dateStrings: true,
typeCast: true,
//timezone: "Z",
},
2025-02-01 21:48:13 +00:00
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000,
},
2025-02-03 19:50:16 +00:00
logQueryParameters: true,
logging: process.env.DB_LOGGING === "true" ? logger.debug : false,
define: {
charset: "utf8mb4",
collate: "utf8mb4_unicode_ci",
//freezeTableName: true,
underscored: true,
timestamps: true,
},
2025-02-01 21:48:13 +00:00
}
2025-01-29 16:01:17 +00:00
);
export async function connectToDatabase(): Promise<void> {
try {
await sequelize.authenticate();
2025-04-22 17:14:34 +00:00
//await registerModels();
2025-02-03 19:50:16 +00:00
logger.info(`✔️${" "}Database connection established successfully.`);
2025-01-29 16:01:17 +00:00
} catch (error) {
2025-02-03 19:50:16 +00:00
logger.error("❌ Unable to connect to the database:", error);
2025-01-29 16:01:17 +00:00
process.exit(1);
}
}