-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.js
More file actions
36 lines (29 loc) · 987 Bytes
/
mysql.js
File metadata and controls
36 lines (29 loc) · 987 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
31
32
33
34
35
36
import dotenv from 'dotenv';
dotenv.config();
import util from 'util';
import {
createPool
} from 'mysql';
const pool = createPool({
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
database: process.env.MYSQL_DATABASE,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
connectionLimit: 100,
supportBigNumbers: true,
bigNumberStrings: true
});
// Ping database to check for common exception errors.
pool.getConnection((err, connection) => {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') console.error('Database connection was closed.');
if (err.code === 'ER_CON_COUNT_ERROR') console.error('Database has too many connections.');
if (err.code === 'ECONNREFUSED') console.error('Database connection was refused.');
}
if (connection) connection.release();
return
});
// Promisify for Node.js async/await.
pool.query = util.promisify(pool.query);
module.exports = pool;