refactor(database): use mysql2/promise

Better code-flow.
Other minor changes to config.
Remove profiling (temp).
This commit is contained in:
Linden committed 2023-07-11 16:33:01 +10:00
1 parent 32d61488f4
commit fa0068e798
5 files changed
+83 -65

No files matched your search

+19 -6
View File
@@ -1,5 +1,5 @@
import { createPool, Pool } from 'mysql2';
import { connectionOptions, mysql_transaction_isolation_level } from '../config';
import { createPool, Pool } from 'mysql2/promise';
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
import { typeCast } from '../utils/typeCast';
let pool: Pool;
@@ -18,20 +18,33 @@ export async function waitForConnection() {
}
}
setTimeout(() => {
setDebug();
setInterval(() => {
setDebug();
}, 1000);
setTimeout(async () => {
pool = createPool({
connectTimeout: 60000,
trace: false,
supportBigNumbers: true,
...connectionOptions,
typeCast,
namedPlaceholders: false, // we use our own named-placeholders patch, disable mysql2s
});
pool.query(mysql_transaction_isolation_level, (err) => {
if (err) return console.error(`^3Unable to establish a connection to the database!\n^3[${err}]^0`);
const connection = await pool.getConnection();
try {
connection.query(mysql_transaction_isolation_level);
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`);
}
connection.release()
});
export { pool, isServerConnected };