forked from Bee-Balanced/Server-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
30 lines (24 loc) · 717 Bytes
/
db.js
File metadata and controls
30 lines (24 loc) · 717 Bytes
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
// db.js - creates and exports a MySQL connection pool
import mysql from 'mysql2/promise';
import dotenv from 'dotenv';
dotenv.config();
const db = mysql.createPool({
host: process.env.DB_HOST || 'localhost',
port: Number(process.env.DB_PORT || 3306),
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || 'my_database',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
(async () => {
try {
const connection = await db.getConnection();
console.log('MySQL connected!');
connection.release();
} catch (err) {
console.error('MySQL connection failed:', err.message);
}
})();
export default db;