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:
Linden committed 2023-07-12 00:05:29 +10:00
1 parent 197b92891a
commit b2387fc33a
4 files changed
+40 -31

No files matched your search

+14 -2
View File
@@ -1,4 +1,5 @@
export const resourceName = GetCurrentResourceName();
import { typeCast } from './utils/typeCast';
export const mysql_connection_string = GetConvar('mysql_connection_string', '');
export let mysql_ui = GetConvar('mysql_ui', 'false') === 'true';
export let mysql_slow_query_warning = GetConvarInt('mysql_slow_query_warning', 200);
@@ -87,7 +88,18 @@ export const connectionOptions = (() => {
}
}
return options;
const flags: string[] = [];
flags.push(options.database ? 'CONNECT_WITH_DB' : '-CONNECT_WITH_DB');
return {
connectTimeout: 60000,
trace: false,
supportBigNumbers: true,
...options,
typeCast,
namedPlaceholders: false, // we use our own named-placeholders patch, disable mysql2s
flags: flags,
};
})();
RegisterCommand(
+18 -25
View File
@@ -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`
);
}
});
+7 -3
View File
@@ -1,5 +1,4 @@
import fetch from 'node-fetch';
import { resourceName } from '../config';
if (GetConvar('mysql_versioncheck', 'true') === 'true') {
setTimeout(async () => {
@@ -10,13 +9,18 @@ if (GetConvar('mysql_versioncheck', 'true') === 'true') {
const release = (await response.json()) as any;
if (release.prerelease) return;
const currentVersion = GetResourceMetadata(resourceName, 'version', 0).match(/(\d)\.(\d+\.\d+)/);
const currentVersion = GetResourceMetadata(GetCurrentResourceName(), 'version', 0).match(/(\d)\.(\d+\.\d+)/);
if (!currentVersion) return;
const latestVersion = release.tag_name.match(/(\d)\.(\d+\.\d+)/);
if (!latestVersion) return;
if (currentVersion[0] === latestVersion[0] || parseInt(currentVersion[1]) > parseInt(latestVersion[1]) || parseFloat(currentVersion[2]) > parseFloat(latestVersion[2])) return;
if (
currentVersion[0] === latestVersion[0] ||
parseInt(currentVersion[1]) > parseInt(latestVersion[1]) ||
parseFloat(currentVersion[2]) > parseFloat(latestVersion[2])
)
return;
console.log(
`^3An update is available for oxmysql (current version: ${currentVersion[0]})\r\n${release.html_url}^0`
+1 -1
View File
@@ -1,4 +1,4 @@
import { resourceName } from '../config';
const resourceName = GetCurrentResourceName();
export async function scheduleTick() {
ScheduleResourceTick(resourceName);