feat: Parse URI-like connection string, support for slow query warning

This commit is contained in:
dunak-debug committed 2021-09-08 01:07:27 +02:00
1 parent f252c94413
commit 6517951c83
4 files changed
+44 -8

No files matched your search

+3 -1
View File
@@ -115,4 +115,6 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*
.yarn.installed
+28 -7
View File
@@ -1,25 +1,46 @@
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
const { createPool } = require('mysql2/promise');
const { ConnectionStringParser } = require('connection-string-parser');
const connectionStringParser = new ConnectionStringParser({
scheme: 'mysql',
hosts: []
});
const connectionString = GetConvar('mysql_connection_string', '');
if(connectionString === '') throw new Error(`Undefined convar mysql_connection_string`);
const config = connectionStringParser.parse(connectionString);
//TODO: semicolor format
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 100)
const pool = createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'database',
host: config.hosts[0].host,
user: config.username,
password: config.password,
database: config.endpoint,
charset: 'utf8mb4_unicode_ci',
multipleStatements: false,
namedPlaceholders: true,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
queueLimit: 0,
...config.options
});
const execute = async (query, parameters) => {
ScheduleResourceTick(GetCurrentResourceName());
try {
console.time(query);
const startTime = process.hrtime.bigint();
const [result] = await pool.execute(query, parameters);
console.timeEnd(query);
const executionTime = new Number((process.hrtime.bigint() - startTime)) / 1000000
if(executionTime >= slowQueryWarning)
console.warn(`${query} took ${executionTime}ms!`)
return result;
} catch (error) {
return console.error(error.message);
+1
View File
@@ -9,6 +9,7 @@
"license": "GPL-3.0-or-later",
"dependencies": {
"@citizenfx/server": "^2.0.4376-1",
"connection-string-parser": "^1.0.4",
"mysql2": "^2.3.0"
}
}
+12
View File
@@ -7,6 +7,13 @@
resolved "https://registry.yarnpkg.com/@citizenfx/server/-/server-2.0.4376-1.tgz#6cc9e034bc463a031df40532f278b9f309b7f258"
integrity sha512-xIqpuSwbyga3ysSJ3NfiDeJ6+L3cEHI+vRF45IjMP3U0E4bMZLGrjoSXBbndDJjd97uPdak3HPZD4/AC0HBnIw==
connection-string-parser@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/connection-string-parser/-/connection-string-parser-1.0.4.tgz#27f1379694fccd7c1c0066db3de0960f9d0dd619"
integrity sha512-grmrIHzrZRjn53dwRqJTmtTRVc+gUiIk/tzjUrteN6aLN4l9DR6KE/IsGtjndyAlHhkmKjAHB9p7PJ0cFgO94w==
dependencies:
tslib "^2.0.0"
denque@^1.4.1:
version "1.5.0"
resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de"
@@ -92,6 +99,11 @@ sqlstring@^2.3.2:
resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.2.tgz#cdae7169389a1375b18e885f2e60b3e460809514"
integrity sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg==
tslib@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==
yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"