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,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<Pool> {
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<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 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();

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