-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
114 lines (103 loc) · 3.02 KB
/
Copy pathdatabase.js
File metadata and controls
114 lines (103 loc) · 3.02 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const mysql = require("mysql2/promise");
const dotenv = require("dotenv");
const path = require("path");
const fs = require("fs");
dotenv.config({ path: "./.env" });
// SSL configuration based on environment
const getSSLConfig = () => {
if (process.env.NODE_ENV === 'production') {
// For Vercel deployment - disable SSL verification
return { rejectUnauthorized: false };
} else {
// For local development - use certificate if available
try {
const caPath = path.join(__dirname, 'certs', 'ca.pem');
if (fs.existsSync(caPath)) {
return { ca: fs.readFileSync(caPath) };
}
} catch (error) {
console.log("SSL certificate not found, using default SSL config");
}
return { rejectUnauthorized: false };
}
};
// Create connection pool for better serverless performance
const pool = mysql.createPool({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
port: process.env.DB_PORT || 3306,
ssl: getSSLConfig(),
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
multipleStatements: false
});
// Enhanced connection wrapper with retry logic
const executeQuery = async (query, params = []) => {
let retries = 3;
while (retries > 0) {
try {
const [results] = await pool.execute(query, params);
return results;
} catch (error) {
console.error(`Database query failed, retries left: ${retries - 1}`, error.message);
retries--;
if (retries === 0) throw error;
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
};
// For backward compatibility with existing code
const legacyConnection = {
query: (sql, params, callback) => {
if (typeof params === 'function') {
callback = params;
params = [];
}
executeQuery(sql, params)
.then(results => callback(null, results))
.catch(error => callback(error));
},
beginTransaction: (callback) => {
pool.getConnection()
.then(connection => {
connection.beginTransaction()
.then(() => callback(null, connection))
.catch(callback);
})
.catch(callback);
},
rollback: (connection, callback) => {
if (typeof connection === 'function') {
callback = connection;
callback();
return;
}
connection.rollback(() => {
connection.release();
if (callback) callback();
});
},
commit: (connection, callback) => {
connection.commit()
.then(() => {
connection.release();
if (callback) callback(null);
})
.catch(error => {
connection.rollback(() => {
connection.release();
if (callback) callback(error);
});
});
}
};
// Test connection on startup (optional)
if (process.env.NODE_ENV !== 'production') {
executeQuery('SELECT 1')
.then(() => console.log("✅ Connected to Aiven MySQL database"))
.catch(err => console.error("❌ Failed to connect to database:", err.message));
}
module.exports = legacyConnection;