mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 14:38:50 +00:00
refactor: minor tweaks and cleanup
Primarily moving connectionOptions fully to config. Removed exported resource name. Include error code and number when database connection fails.
This commit is contained in:
1 parent
197b92891a
commit
b2387fc33a
4 files changed
+40
-31
No files matched your search
+18
-25
@@ -1,6 +1,5 @@
|
||||
import { Connection, createPool, Pool } from 'mysql2/promise';
|
||||
import { createPool, Pool } from 'mysql2/promise';
|
||||
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
|
||||
import { typeCast } from '../utils/typeCast';
|
||||
|
||||
let pool: Pool;
|
||||
let isServerConnected = false;
|
||||
@@ -18,25 +17,8 @@ export async function waitForConnection() {
|
||||
}
|
||||
}
|
||||
|
||||
setDebug();
|
||||
|
||||
setInterval(() => {
|
||||
setDebug();
|
||||
}, 1000);
|
||||
|
||||
setTimeout(async () => {
|
||||
const flags: string[] = [];
|
||||
flags.push(connectionOptions.database ? 'CONNECT_WITH_DB' : '-CONNECT_WITH_DB');
|
||||
|
||||
pool = createPool({
|
||||
connectTimeout: 60000,
|
||||
trace: false,
|
||||
supportBigNumbers: true,
|
||||
...connectionOptions,
|
||||
typeCast,
|
||||
namedPlaceholders: false, // we use our own named-placeholders patch, disable mysql2s
|
||||
flags: flags,
|
||||
});
|
||||
function setConnectionPool() {
|
||||
pool = createPool(connectionOptions);
|
||||
|
||||
pool.on('connection', (connection) => {
|
||||
connection.query(mysql_transaction_isolation_level);
|
||||
@@ -45,16 +27,27 @@ setTimeout(async () => {
|
||||
pool.on('acquire', (connection) => {
|
||||
connection.query('SET profiling_history_size = 0');
|
||||
connection.query('SET profiling = 0');
|
||||
connection.query('SET profiling = 1');
|
||||
connection.query('SET profiling_history_size = 1000');
|
||||
connection.query('SET profiling = 1');
|
||||
});
|
||||
}
|
||||
|
||||
setInterval(() => {
|
||||
setDebug();
|
||||
}, 1000);
|
||||
|
||||
setTimeout(async () => {
|
||||
setDebug();
|
||||
setConnectionPool();
|
||||
|
||||
try {
|
||||
await pool.query('SELECT DATABASE()');
|
||||
(await pool.getConnection()).release();
|
||||
console.log(`^2Database server connection established!^0`);
|
||||
isServerConnected = true;
|
||||
} catch (err) {
|
||||
console.error(`^3Unable to establish a connection to the database!\n^3[${err}]^0`);
|
||||
} catch (err: any) {
|
||||
console.log(
|
||||
`^3Unable to establish a connection to the database (${err.code})!\n^1Error ${err.errno}: ${err.message}^0`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user