diff --git a/src/config.ts b/src/config.ts index f05a0eb..9e4a90c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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( diff --git a/src/database/index.ts b/src/database/index.ts index 6455ba2..eee8f31 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -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` + ); } }); diff --git a/src/update/index.ts b/src/update/index.ts index 56da483..c40d696 100644 --- a/src/update/index.ts +++ b/src/update/index.ts @@ -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` diff --git a/src/utils/scheduleTick.ts b/src/utils/scheduleTick.ts index 5d4c9bb..4532828 100644 --- a/src/utils/scheduleTick.ts +++ b/src/utils/scheduleTick.ts @@ -1,4 +1,4 @@ -import { resourceName } from '../config'; +const resourceName = GetCurrentResourceName(); export async function scheduleTick() { ScheduleResourceTick(resourceName);