Refactored the DB class into a regular singleton class.

This commit is contained in:
softsimon 2022-04-13 17:38:42 +04:00
parent 6fb0571b06
commit 6919393e6c
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
10 changed files with 45 additions and 49 deletions

View File

@ -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));

View File

@ -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 {

View File

@ -1,5 +1,4 @@
import { readFileSync } from 'fs';
import { DB } from '../database';
import DB from '../database';
import logger from '../logger';
import config from '../config';

View File

@ -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';

View File

@ -3,12 +3,16 @@ 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 = {
class DB {
constructor() {
if (config.DATABASE.SOCKET !== '') {
this.poolConfig.socketPath = config.DATABASE.SOCKET;
} else {
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,
@ -18,37 +22,30 @@ export class DB {
timezone: '+00:00',
};
if (config.DATABASE.SOCKET !== '') {
poolConfig.socketPath = config.DATABASE.SOCKET;
} else {
poolConfig.host = config.DATABASE.HOST;
}
return poolConfig;
}
static async getPool(): Promise<Pool> {
if (DB.pool === null) {
DB.pool = createPool(DB.poolConfig());
DB.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();
public async query(query, params?) {
const pool = await this.getPool();
return pool.query(query, params);
}
}
export async function checkDbConnection() {
public async checkDbConnection() {
try {
await DB.query('SELECT ?', [1]);
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<Pool> {
if (this.pool === null) {
this.pool = createPool(this.poolConfig);
this.pool.on('connection', function (newConnection: PoolConnection) {
newConnection.query(`SET time_zone='+00:00'`);
});
}
return this.pool;
}
}
export default new DB();

View File

@ -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(',');

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';
/**