Refactor database pool use

This commit is contained in:
nymkappa
2022-04-12 15:15:57 +09:00
parent ec4405b07a
commit 6fb0571b06
10 changed files with 164 additions and 358 deletions

View File

@@ -9,9 +9,7 @@ class PoolsRepository {
* Get all pools tagging info
*/
public async $getPools(): Promise<PoolTag[]> {
const connection = await DB.getConnection();
const [rows] = await connection.query('SELECT id, name, addresses, regexes, slug FROM pools;');
connection.release();
const [rows] = await DB.query('SELECT id, name, addresses, regexes, slug FROM pools;');
return <PoolTag[]>rows;
}
@@ -19,9 +17,7 @@ class PoolsRepository {
* Get unknown pool tagging info
*/
public async $getUnknownPool(): Promise<PoolTag> {
const connection = await DB.getConnection();
const [rows] = await connection.query('SELECT id, name, slug FROM pools where name = "Unknown"');
connection.release();
const [rows] = await DB.query('SELECT id, name, slug FROM pools where name = "Unknown"');
return <PoolTag>rows[0];
}
@@ -42,14 +38,10 @@ class PoolsRepository {
query += ` GROUP BY pool_id
ORDER BY COUNT(height) DESC`;
const connection = await DB.getConnection();
try {
const [rows] = await connection.query(query);
connection.release();
const [rows] = await DB.query(query);
return <PoolInfo[]>rows;
} catch (e) {
connection.release();
logger.err(`Cannot generate pools stats. Reason: ` + (e instanceof Error ? e.message : e));
throw e;
}
@@ -64,14 +56,10 @@ class PoolsRepository {
LEFT JOIN blocks on pools.id = blocks.pool_id AND blocks.blockTimestamp BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?)
GROUP BY pools.id`;
const connection = await DB.getConnection();
try {
const [rows] = await connection.query(query, [from, to]);
connection.release();
const [rows] = await DB.query(query, [from, to]);
return <PoolInfo[]>rows;
} catch (e) {
connection.release();
logger.err('Cannot generate pools blocks count. Reason: ' + (e instanceof Error ? e.message : e));
throw e;
}
@@ -86,12 +74,8 @@ class PoolsRepository {
FROM pools
WHERE pools.slug = ?`;
let connection;
try {
connection = await DB.getConnection();
const [rows] = await connection.query(query, [slug]);
connection.release();
const [rows]: any[] = await DB.query(query, [slug]);
if (rows.length < 1) {
logger.debug(`This slug does not match any known pool`);
@@ -107,7 +91,6 @@ class PoolsRepository {
return rows[0];
} catch (e) {
connection.release();
logger.err('Cannot get pool from db. Reason: ' + (e instanceof Error ? e.message : e));
throw e;
}