diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index ed93d695d..0e9a18220 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -1,5 +1,5 @@ import config from '../config'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; const sleep = (ms: number) => new Promise(res => setTimeout(res, ms)); diff --git a/backend/src/api/liquid/elements-parser.ts b/backend/src/api/liquid/elements-parser.ts index e2da5adef..12439e037 100644 --- a/backend/src/api/liquid/elements-parser.ts +++ b/backend/src/api/liquid/elements-parser.ts @@ -2,7 +2,7 @@ import { IBitcoinApi } from '../bitcoin/bitcoin-api.interface'; import bitcoinClient from '../bitcoin/bitcoin-client'; import bitcoinSecondClient from '../bitcoin/bitcoin-second-client'; import { Common } from '../common'; -import { DB } from '../../database'; +import DB from '../../database'; import logger from '../../logger'; class ElementsParser { diff --git a/backend/src/api/pools-parser.ts b/backend/src/api/pools-parser.ts index d53335c9f..202fca1bd 100644 --- a/backend/src/api/pools-parser.ts +++ b/backend/src/api/pools-parser.ts @@ -1,5 +1,4 @@ -import { readFileSync } from 'fs'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; import config from '../config'; diff --git a/backend/src/api/statistics.ts b/backend/src/api/statistics.ts index d5690c52e..bd93b4c6e 100644 --- a/backend/src/api/statistics.ts +++ b/backend/src/api/statistics.ts @@ -1,5 +1,5 @@ import memPool from './mempool'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; import { Statistic, TransactionExtended, OptimizedStatistic } from '../mempool.interfaces'; diff --git a/backend/src/database.ts b/backend/src/database.ts index 3ccad9aa3..3816154cd 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -3,52 +3,49 @@ import { createPool, Pool, PoolConnection } from 'mysql2/promise'; import logger from './logger'; import { PoolOptions } from 'mysql2/typings/mysql'; -export class DB { - static connectionsReady: number[] = []; - static pool: Pool | null = null; - - static poolConfig = (): PoolOptions => { - const poolConfig: PoolOptions = { - port: config.DATABASE.PORT, - database: config.DATABASE.DATABASE, - user: config.DATABASE.USERNAME, - password: config.DATABASE.PASSWORD, - connectionLimit: 10, - supportBigNumbers: true, - timezone: '+00:00', - }; - + class DB { + constructor() { if (config.DATABASE.SOCKET !== '') { - poolConfig.socketPath = config.DATABASE.SOCKET; + this.poolConfig.socketPath = config.DATABASE.SOCKET; } else { - poolConfig.host = config.DATABASE.HOST; + this.poolConfig.host = config.DATABASE.HOST; } + } + private pool: Pool | null = null; + private poolConfig: PoolOptions = { + port: config.DATABASE.PORT, + database: config.DATABASE.DATABASE, + user: config.DATABASE.USERNAME, + password: config.DATABASE.PASSWORD, + connectionLimit: 10, + supportBigNumbers: true, + timezone: '+00:00', + }; - return poolConfig; + public async query(query, params?) { + const pool = await this.getPool(); + return pool.query(query, params); } - static async getPool(): Promise { - if (DB.pool === null) { - DB.pool = createPool(DB.poolConfig()); - DB.pool.on('connection', function (newConnection: PoolConnection) { + public async checkDbConnection() { + try { + await this.query('SELECT ?', [1]); + logger.info('Database connection established.'); + } catch (e) { + logger.err('Could not connect to database: ' + (e instanceof Error ? e.message : e)); + process.exit(1); + } + } + + private async getPool(): Promise { + if (this.pool === null) { + this.pool = createPool(this.poolConfig); + this.pool.on('connection', function (newConnection: PoolConnection) { newConnection.query(`SET time_zone='+00:00'`); }); } - return DB.pool; - } - - static async query(query, params?) { - const pool = await DB.getPool(); - return pool.query(query, params); + return this.pool; } } -export async function checkDbConnection() { - try { - await DB.query('SELECT ?', [1]); - logger.info('Database connection established.'); - } catch (e) { - logger.err('Could not connect to database: ' + (e instanceof Error ? e.message : e)); - process.exit(1); - } -} +export default new DB(); diff --git a/backend/src/index.ts b/backend/src/index.ts index 1081c5468..c2f111c65 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,7 +5,7 @@ import * as WebSocket from 'ws'; import * as cluster from 'cluster'; import axios from 'axios'; -import { checkDbConnection, DB } from './database'; +import DB from './database'; import config from './config'; import routes from './routes'; import blocks from './api/blocks'; @@ -89,7 +89,7 @@ class Server { diskCache.loadMempoolCache(); if (config.DATABASE.ENABLED) { - await checkDbConnection(); + await DB.checkDbConnection(); try { if (process.env.npm_config_reindex != undefined) { // Re-index requests const tables = process.env.npm_config_reindex.split(','); diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index e35647fd2..8e96a0c38 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -1,5 +1,5 @@ -import { BlockExtended, PoolTag } from '../mempool.interfaces'; -import { DB } from '../database'; +import { BlockExtended } from '../mempool.interfaces'; +import DB from '../database'; import logger from '../logger'; import { Common } from '../api/common'; import { prepareBlock } from '../utils/blocks-utils'; diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index 1701d2236..8388b9122 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -1,5 +1,5 @@ import { Common } from '../api/common'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; import PoolsRepository from './PoolsRepository'; diff --git a/backend/src/repositories/PoolsRepository.ts b/backend/src/repositories/PoolsRepository.ts index c1f8cc80f..037a6250a 100644 --- a/backend/src/repositories/PoolsRepository.ts +++ b/backend/src/repositories/PoolsRepository.ts @@ -1,6 +1,6 @@ import { Common } from '../api/common'; import config from '../config'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; import { PoolInfo, PoolTag } from '../mempool.interfaces'; diff --git a/backend/src/tasks/pools-updater.ts b/backend/src/tasks/pools-updater.ts index 90937a16f..aee786ff9 100644 --- a/backend/src/tasks/pools-updater.ts +++ b/backend/src/tasks/pools-updater.ts @@ -1,7 +1,7 @@ const https = require('https'); import poolsParser from '../api/pools-parser'; import config from '../config'; -import { DB } from '../database'; +import DB from '../database'; import logger from '../logger'; /**