-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
69 lines (64 loc) · 2.24 KB
/
db.js
File metadata and controls
69 lines (64 loc) · 2.24 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const mysql = require('mysql2/promise');
require('dotenv').config();
// Erstelle den Verbindungspool für MySQL
const pool = mysql.createPool({
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASS || '',
database: process.env.DB_NAME || 'bot_database',
port: process.env.DB_PORT || 3306,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
connectTimeout: 10000, // Timeout von 10 Sekunden
});
// Teste die Verbindung beim Start
(async () => {
try {
const connection = await pool.getConnection();
console.log('✅ [INFO] Verbindung zur Datenbank erfolgreich.');
connection.release(); // Verbindung nach Test freigeben
} catch (error) {
console.error('❌ [ERROR] Fehler bei der Verbindung zur Datenbank:', error.message || error);
process.exit(1); // Bot stoppen, wenn keine Verbindung möglich ist
}
})();
// Exportiere Methoden zur Abfrage und zum Pool
module.exports = {
/**
* Führt eine SQL-Abfrage aus.
* @param {string} sql - Die SQL-Abfrage.
* @param {Array} params - Parameter für die SQL-Abfrage.
* @returns {Promise<[]>} - Ergebnisse der Abfrage.
*/
query: async (sql, params) => {
try {
console.log(`➡️ [DEBUG] Führe Abfrage aus: ${sql} mit Parametern: ${JSON.stringify(params)}`);
const [rows] = await pool.execute(sql, params);
return rows;
} catch (error) {
console.error('❌ [ERROR] Fehler bei der SQL-Abfrage:', {
sql,
params,
error: error.message || error,
});
throw error; // Fehler weiter werfen
}
},
/**
* Liefert den Connection Pool für erweiterte Abfragen.
* @returns {mysql.Pool} - Der MariaDB Connection Pool.
*/
pool: pool,
/**
* Schließt alle offenen Verbindungen zum Pool.
*/
close: async () => {
try {
await pool.end();
console.log('✅ [INFO] Alle Verbindungen zum Pool wurden geschlossen.');
} catch (error) {
console.error('❌ [ERROR] Fehler beim Schließen des Pools:', error.message || error);
}
}
};