mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 22:20:56 +00:00
feat: Parse URI-like connection string, support for slow query warning
This commit is contained in:
4 files changed
+43
-7
No files matched your search
@@ -116,3 +116,5 @@ dist
|
|||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
.yarn.installed
|
||||||
+28
-7
@@ -1,25 +1,46 @@
|
|||||||
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
|
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
|
||||||
const { createPool } = require('mysql2/promise');
|
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({
|
const pool = createPool({
|
||||||
host: 'localhost',
|
host: config.hosts[0].host,
|
||||||
user: 'root',
|
user: config.username,
|
||||||
password: 'password',
|
password: config.password,
|
||||||
database: 'database',
|
database: config.endpoint,
|
||||||
charset: 'utf8mb4_unicode_ci',
|
charset: 'utf8mb4_unicode_ci',
|
||||||
multipleStatements: false,
|
multipleStatements: false,
|
||||||
namedPlaceholders: true,
|
namedPlaceholders: true,
|
||||||
waitForConnections: true,
|
waitForConnections: true,
|
||||||
connectionLimit: 10,
|
connectionLimit: 10,
|
||||||
queueLimit: 0
|
queueLimit: 0,
|
||||||
|
...config.options
|
||||||
});
|
});
|
||||||
|
|
||||||
const execute = async (query, parameters) => {
|
const execute = async (query, parameters) => {
|
||||||
ScheduleResourceTick(GetCurrentResourceName());
|
ScheduleResourceTick(GetCurrentResourceName());
|
||||||
try {
|
try {
|
||||||
console.time(query);
|
const startTime = process.hrtime.bigint();
|
||||||
const [result] = await pool.execute(query, parameters);
|
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;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return console.error(error.message);
|
return console.error(error.message);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@citizenfx/server": "^2.0.4376-1",
|
"@citizenfx/server": "^2.0.4376-1",
|
||||||
|
"connection-string-parser": "^1.0.4",
|
||||||
"mysql2": "^2.3.0"
|
"mysql2": "^2.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,13 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@citizenfx/server/-/server-2.0.4376-1.tgz#6cc9e034bc463a031df40532f278b9f309b7f258"
|
resolved "https://registry.yarnpkg.com/@citizenfx/server/-/server-2.0.4376-1.tgz#6cc9e034bc463a031df40532f278b9f309b7f258"
|
||||||
integrity sha512-xIqpuSwbyga3ysSJ3NfiDeJ6+L3cEHI+vRF45IjMP3U0E4bMZLGrjoSXBbndDJjd97uPdak3HPZD4/AC0HBnIw==
|
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:
|
denque@^1.4.1:
|
||||||
version "1.5.0"
|
version "1.5.0"
|
||||||
resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de"
|
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"
|
resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.2.tgz#cdae7169389a1375b18e885f2e60b3e460809514"
|
||||||
integrity sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg==
|
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:
|
yallist@^2.1.2:
|
||||||
version "2.1.2"
|
version "2.1.2"
|
||||||
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
|
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
|
||||||
|
|||||||
Reference in new issue
Block a user