diff --git a/src/database/index.ts b/src/database/index.ts index 83af9f3..9f688d9 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -3,14 +3,14 @@ import { connectionOptions, mysql_transaction_isolation_level } from '../config' import { typeCast } from '../utils/typeCast'; let pool: Pool; -let serverReady = false; +let isServerConnected = false; export async function waitForConnection() { - if (!serverReady) { - await new Promise((resolve) => { + if (!isServerConnected) { + return await new Promise((resolve) => { (function wait() { - if (serverReady) { - return resolve(); + if (isServerConnected) { + return resolve(true); } setTimeout(wait); })(); @@ -30,8 +30,8 @@ setTimeout(() => { pool.query(mysql_transaction_isolation_level, (err) => { if (err) return console.error(`^3Unable to establish a connection to the database!\n^3[${err}]^0`); console.log(`^2Database server connection established!^0`); - serverReady = true; + isServerConnected = true; }); }); -export { pool, serverReady }; +export { pool, isServerConnected }; diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index 3f25977..0127a74 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -5,7 +5,7 @@ import { CFXCallback, CFXParameters } from '../types'; import { parseResponse } from '../utils/parseResponse'; import { executeType, parseExecute } from '../utils/parseExecute'; import { scheduleTick } from '../utils/scheduleTick'; -import { serverReady, waitForConnection } from '../database'; +import { isServerConnected, waitForConnection } from '../database'; export const rawExecute = ( invokingResource: string, @@ -28,7 +28,7 @@ export const rawExecute = ( scheduleTick(); return new Promise(async (resolve, reject) => { - if (!serverReady) await waitForConnection(); + if (!isServerConnected) await waitForConnection(); pool.getConnection((err, connection) => { if (err) return reject(err.message); diff --git a/src/database/rawQuery.ts b/src/database/rawQuery.ts index 5e3cc97..71f399e 100644 --- a/src/database/rawQuery.ts +++ b/src/database/rawQuery.ts @@ -1,4 +1,4 @@ -import { pool, serverReady, waitForConnection } from '.'; +import { pool, isServerConnected, waitForConnection } from '.'; import { parseArguments } from '../utils/parseArguments'; import { parseResponse } from '../utils/parseResponse'; import { logQuery } from '../logger'; @@ -23,7 +23,7 @@ export const rawQuery = ( scheduleTick(); return new Promise(async (resolve, reject) => { - if (!serverReady) await waitForConnection(); + if (!isServerConnected) await waitForConnection(); pool.query(query, parameters, (err, result, _, executionTime) => { if (err) return reject(err); diff --git a/src/database/rawTransaction.ts b/src/database/rawTransaction.ts index 55c7c3c..be7f012 100644 --- a/src/database/rawTransaction.ts +++ b/src/database/rawTransaction.ts @@ -1,4 +1,4 @@ -import { pool, serverReady, waitForConnection } from '.'; +import { pool, isServerConnected, waitForConnection } from '.'; import { logQuery } from '../logger'; import { CFXParameters, TransactionQuery } from '../types'; import { parseTransaction } from '../utils/parseTransaction'; @@ -15,7 +15,7 @@ export const rawTransaction = async ( parameters: CFXParameters, callback?: (result: boolean) => void ) => { - if (!serverReady) await waitForConnection() + if (!isServerConnected) await waitForConnection() scheduleTick();