mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 06:28:53 +00:00
refactor(database): move pool and connection variables
Mostly reorganising module. Added getPoolConnection helper.
This commit is contained in:
1 parent
b3f4a7aa6b
commit
0398abc501
6 files changed
+78
-69
No files matched your search
+10
-46
@@ -1,52 +1,16 @@
|
||||
import { createPool, Pool, RowDataPacket } from 'mysql2/promise';
|
||||
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
|
||||
import { setDebug } from '../config';
|
||||
import { getPoolConnection } from './connection';
|
||||
|
||||
let pool: Pool;
|
||||
let isServerConnected = false;
|
||||
let dbVersion = '';
|
||||
|
||||
export async function waitForConnection() {
|
||||
if (!isServerConnected) {
|
||||
return await new Promise<boolean>((resolve) => {
|
||||
(function wait() {
|
||||
if (isServerConnected) {
|
||||
return resolve(true);
|
||||
}
|
||||
setTimeout(wait);
|
||||
})();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setConnectionPool() {
|
||||
pool = createPool(connectionOptions);
|
||||
|
||||
pool.on('connection', (connection) => {
|
||||
connection.query(mysql_transaction_isolation_level);
|
||||
});
|
||||
}
|
||||
setTimeout(() => {
|
||||
setDebug();
|
||||
getPoolConnection();
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
setDebug();
|
||||
}, 1000);
|
||||
|
||||
setTimeout(async () => {
|
||||
setDebug();
|
||||
setConnectionPool();
|
||||
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
const [result] = await <Promise<RowDataPacket[]>> connection.query('SELECT VERSION() as version')
|
||||
dbVersion = `^5[${result[0].version}]`
|
||||
connection.release();
|
||||
|
||||
console.log(`${dbVersion} ^2Database server connection established!^0`);
|
||||
isServerConnected = true;
|
||||
} catch (err: any) {
|
||||
console.log(
|
||||
`^3Unable to establish a connection to the database (${err.code})!\n^1Error ${err.errno}: ${err.message}^0`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export { pool, isServerConnected, dbVersion };
|
||||
export * from './connection';
|
||||
export * from './rawQuery';
|
||||
export * from './rawExecute';
|
||||
export * from './rawTransaction';
|
||||
Reference in new issue
Block a user