2020-10-19 06:57:02 +02:00
|
|
|
import config from './config';
|
2019-07-21 16:59:47 +02:00
|
|
|
import { createPool } from 'mysql2/promise';
|
2020-10-13 10:27:52 +02:00
|
|
|
import logger from './logger';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
export class DB {
|
|
|
|
static pool = createPool({
|
2020-10-19 06:57:02 +02:00
|
|
|
host: config.DATABASE.HOST,
|
|
|
|
port: config.DATABASE.PORT,
|
|
|
|
database: config.DATABASE.DATABASE,
|
|
|
|
user: config.DATABASE.USERNAME,
|
|
|
|
password: config.DATABASE.PASSWORD,
|
2019-07-21 16:59:47 +02:00
|
|
|
connectionLimit: 10,
|
|
|
|
supportBigNumbers: true,
|
2022-03-06 16:44:09 +01:00
|
|
|
timezone: '+00:00',
|
2019-07-21 16:59:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkDbConnection() {
|
|
|
|
try {
|
|
|
|
const connection = await DB.pool.getConnection();
|
2020-10-13 10:27:52 +02:00
|
|
|
logger.info('Database connection established.');
|
2019-07-21 16:59:47 +02:00
|
|
|
connection.release();
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
logger.err('Could not connect to database: ' + (e instanceof Error ? e.message : e));
|
2019-07-21 16:59:47 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|