mempool/backend/src/database.ts

27 lines
716 B
TypeScript
Raw Normal View History

import config from './config';
2019-07-21 16:59:47 +02:00
import { createPool } from 'mysql2/promise';
import logger from './logger';
2019-07-21 16:59:47 +02:00
export class DB {
static pool = createPool({
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,
});
}
export async function checkDbConnection() {
try {
const connection = await DB.pool.getConnection();
logger.info('Database connection established.');
2019-07-21 16:59:47 +02:00
connection.release();
} catch (e) {
logger.err('Could not connect to database: ' + (e instanceof Error ? e.message : e));
2019-07-21 16:59:47 +02:00
process.exit(1);
}
}