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

35 lines
889 B
TypeScript
Raw Normal View History

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-02-03 18:03:23 +00:00
import { registerModels } from "./register-models";
2025-01-29 16:01:17 +00:00
dotenv.config();
export const sequelize = new Sequelize(
process.env.DB_NAME as string,
process.env.DB_USER as string,
process.env.DB_PASSWORD as string,
{
host: process.env.DB_HOST as string,
dialect: "mariadb",
port: parseInt(process.env.DB_PORT || "3306", 10),
2025-02-01 21:48:13 +00:00
logging: process.env.DB_LOGGING === "true" ? console.log : false,
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000,
},
}
2025-01-29 16:01:17 +00:00
);
export async function connectToDatabase(): Promise<void> {
try {
2025-02-03 18:03:23 +00:00
await registerModels();
2025-01-29 16:01:17 +00:00
await sequelize.authenticate();
2025-02-01 21:48:13 +00:00
console.log("✅ Database connection established successfully.");
2025-01-29 16:01:17 +00:00
} catch (error) {
2025-02-01 21:48:13 +00:00
console.error("❌ Unable to connect to the database:", error);
2025-01-29 16:01:17 +00:00
process.exit(1);
}
}