-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
34 lines (28 loc) · 1000 Bytes
/
db.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
31
32
33
34
const mysql = require('mysql');
const util = require('util');
require('dotenv').config();
const pool = mysql.createPool({
connectionLimit: 1000,
host: process.env.DB_CONN_HOST,
user: process.env.DB_CONN_USER,
password: process.env.DB_CONN_PW,
database: process.env.DB_CONN_DBNAME,
multipleStatements: true
});
// Check for connection errors:
pool.getConnection((err, connection) => {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.error('Database Error: Connection was closed.');
} else if (err.code === 'ER_CON_COUNT_ERROR') {
console.error('Database Error: Database has too many connections.');
} else if (err.code === 'ECONNREFUSED') {
console.error('Database Error: Connection was refused.');
}
}
if (connection) connection.release();
return;
});
// so we can use async/await when running db queries
pool.query = util.promisify(pool.query);
module.exports = pool;