-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
30 lines (26 loc) · 1.22 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require('dotenv').config();
const mysql = require('mysql2');
// Check environment variables
const requiredEnvVars = ['DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DEV_PORT', 'PROD_PORT'];
requiredEnvVars.forEach((envVar) => {
if (!process.env[envVar]) {
console.error(`Error: The environment variable ${envVar} is missing.`);
process.exit(1); // Exit if any required environment variable is not set
}
});
const pool = mysql.createPool({
host: process.env.DB_HOST, // Use environment variable for MySQL server address
user: process.env.DB_USER, // Use environment variable for MySQL user
password: process.env.DB_PASSWORD, // Use environment variable for MySQL password
database: process.env.DB_NAME // Use environment variable for MySQL database name
});
// Test the connection
pool.getConnection((err, connection) => {
if (err) {
console.error("Error connecting to MySQL database:", err);
process.exit(1); // Exit if connection to the database fails
}
console.log("Connected to the MySQL database!");
connection.release(); // Release the connection back to the pool
});
module.exports = pool; // Export the pool for use elsewhere in the application