mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 22:20:56 +00:00
fix: Type casting, removed old query format, dont prepare execute/insert
- fixed https://github.com/overextended/oxmysql/issues/6 - fixed https://github.com/overextended/oxmysql/issues/7 - fixed https://github.com/overextended/oxmysql/issues/8 - fixed https://github.com/overextended/oxmysql/issues/9 - closes https://github.com/overextended/oxmysql/issues/12 - support for modified named-placeholders package patch
This commit is contained in:
5 files changed
+1425
-49
No files matched your search
@@ -117,4 +117,5 @@ dist
|
|||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# FiveM
|
||||||
.yarn.installed
|
.yarn.installed
|
||||||
+42
-47
@@ -1,17 +1,15 @@
|
|||||||
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
|
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
|
||||||
const { createPool, format, escape } = require("mysql2/promise");
|
const { createPool } = require("mysql2/promise");
|
||||||
const { ConnectionStringParser } = require("connection-string-parser");
|
const { ConnectionStringParser } = require("connection-string-parser");
|
||||||
|
|
||||||
const connectionString = GetConvar("mysql_connection_string", "");
|
const connectionString = GetConvar("mysql_connection_string", "");
|
||||||
|
|
||||||
if (connectionString === "") throw new Error(`Set up mysql_connection_string in server.cfg`);
|
if (connectionString === "") throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||||
|
|
||||||
const connectionStringParser = new ConnectionStringParser({
|
const config = new ConnectionStringParser({
|
||||||
scheme: "mysql",
|
scheme: "mysql",
|
||||||
hosts: [],
|
hosts: [],
|
||||||
});
|
}).parse(connectionString);
|
||||||
|
|
||||||
const config = connectionStringParser.parse(connectionString);
|
|
||||||
|
|
||||||
if (Object.keys(config).length === 0)
|
if (Object.keys(config).length === 0)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -19,8 +17,8 @@ if (Object.keys(config).length === 0)
|
|||||||
);
|
);
|
||||||
|
|
||||||
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
||||||
|
|
||||||
const debug = GetConvar("mysql_debug", "false") === "true";
|
const debug = GetConvar("mysql_debug", "false") === "true";
|
||||||
|
const resourceName = GetCurrentResourceName() || "oxmysql";
|
||||||
|
|
||||||
const pool = createPool({
|
const pool = createPool({
|
||||||
host: config.hosts[0].host,
|
host: config.hosts[0].host,
|
||||||
@@ -28,8 +26,19 @@ const pool = createPool({
|
|||||||
password: config.password,
|
password: config.password,
|
||||||
database: config.endpoint,
|
database: config.endpoint,
|
||||||
charset: "utf8mb4_unicode_ci",
|
charset: "utf8mb4_unicode_ci",
|
||||||
namedPlaceholders: true,
|
|
||||||
...config.options,
|
...config.options,
|
||||||
|
namedPlaceholders: true,
|
||||||
|
typeCast: (field, next) => {
|
||||||
|
if (field.type === "TINY" && field.length === 1) {
|
||||||
|
return field.string() === "1";
|
||||||
|
} else if (field.type === "BIT") {
|
||||||
|
return field.buffer()[0] === 1;
|
||||||
|
} else if (field.type === "TIMESTAMP" || field.type === "DATETIME") {
|
||||||
|
return new Date(field.string()).getTime() / 1000;
|
||||||
|
} else {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
pool
|
pool
|
||||||
@@ -41,69 +50,55 @@ pool
|
|||||||
console.error(error.message);
|
console.error(error.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
const formatQuery = (query, params) => {
|
const execute = async (query, parameters, prepare = true) => {
|
||||||
if (!Array.isArray(params)) {
|
ScheduleResourceTick(resourceName);
|
||||||
query = query.replace(/@(\w+)/g, (raw, match) => `:${match}`);
|
|
||||||
|
|
||||||
let newParams = {};
|
|
||||||
Object.keys(params).forEach((param) => {
|
|
||||||
newParams[param.replace("@", "")] = params[param];
|
|
||||||
});
|
|
||||||
|
|
||||||
params = newParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [query, params];
|
|
||||||
};
|
|
||||||
|
|
||||||
const execute = async (query, parameters) => {
|
|
||||||
ScheduleResourceTick(GetCurrentResourceName());
|
|
||||||
try {
|
try {
|
||||||
const [formattedQuery, formattedParameters] = formatQuery(query, parameters);
|
|
||||||
|
|
||||||
const startTime = process.hrtime.bigint();
|
const startTime = process.hrtime.bigint();
|
||||||
const [result] = await pool.execute(formattedQuery, formattedParameters);
|
|
||||||
const executionTime = Number(process.hrtime.bigint() - startTime) / 1000000;
|
|
||||||
|
|
||||||
if (executionTime >= slowQueryWarning)
|
const [result] = prepare
|
||||||
console.warn(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
? await pool.execute(query, parameters)
|
||||||
else if (debug) console.log(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
: await pool.query(query, parameters);
|
||||||
|
|
||||||
|
const executionTime = Number(process.hrtime.bigint() - startTime) / 1e6;
|
||||||
|
|
||||||
|
if (executionTime >= slowQueryWarning || debug)
|
||||||
|
console.warn(`${query} took ${executionTime}ms!`, parameters);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (debug) {
|
console.error(error.message, debug && query, debug && parameters);
|
||||||
console.error(query);
|
return false;
|
||||||
console.error(parameters);
|
|
||||||
}
|
|
||||||
return console.error(error.message);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
global.exports("execute", (query, parameters, callback = () => {}) => {
|
global.exports("execute", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(
|
execute(query, parameters, false).then(
|
||||||
(result) => typeof callback === "function" && callback(result && result.affectedRows)
|
(result) => typeof callback === "function" && callback(result || false)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports("insert", (query, parameters, callback = () => {}) => {
|
||||||
|
execute(query, parameters, false).then(
|
||||||
|
(result) => typeof callback === "function" && callback((result && result.insertId) || false)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("fetch", (query, parameters, callback = () => {}) => {
|
global.exports("fetch", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then((result) => typeof callback === "function" && callback(result));
|
execute(query, parameters).then(
|
||||||
|
(result) => typeof callback === "function" && callback(result || false)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("single", (query, parameters, callback = () => {}) => {
|
global.exports("single", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(
|
execute(query, parameters).then(
|
||||||
(result) => typeof callback === "function" && callback(result && result[0])
|
(result) => typeof callback === "function" && callback((result && result[0]) || false)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("scalar", (query, parameters, callback = () => {}) => {
|
global.exports("scalar", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(
|
execute(query, parameters).then(
|
||||||
(result) =>
|
(result) =>
|
||||||
typeof callback === "function" && callback(result && result[0] && Object.values(result[0])[0])
|
typeof callback === "function" &&
|
||||||
);
|
callback((result && result[0] && Object.values(result[0])[0]) || false)
|
||||||
});
|
|
||||||
|
|
||||||
global.exports("insert", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then(
|
|
||||||
(result) => typeof callback === "function" && callback(result && result.insertId)
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
+15
-2
@@ -1,15 +1,28 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"private": "true",
|
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"main": "oxmysql.js",
|
"main": "oxmysql.js",
|
||||||
"description": "FiveM MySQL wrapper using faster mysql2",
|
"description": "FiveM MySQL wrapper using faster mysql2",
|
||||||
"repository": "[email protected]:overextended/oxmysql.git",
|
"repository": "[email protected]:overextended/oxmysql.git",
|
||||||
"author": "dunak-debug <[email protected]>",
|
"author": "dunak-debug <[email protected]>",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"watch": "webpack --watch",
|
||||||
|
"postinstall": "patch-package"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@citizenfx/server": "^2.0.4376-1",
|
"@citizenfx/server": "^2.0.4376-1",
|
||||||
"connection-string-parser": "^1.0.4",
|
"connection-string-parser": "^1.0.4",
|
||||||
"mysql2": "^2.3.0"
|
"mysql2": "^2.3.0",
|
||||||
|
"postinstall-postinstall": "^2.1.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"copy-webpack-plugin": "^9.0.1",
|
||||||
|
"patch-package": "^6.4.7",
|
||||||
|
"ts-loader": "^9.2.5",
|
||||||
|
"typescript": "^4.4.3",
|
||||||
|
"webpack": "^5.52.1",
|
||||||
|
"webpack-cli": "^4.8.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
const path = require("path");
|
||||||
|
const { DefinePlugin, IgnorePlugin } = require("webpack");
|
||||||
|
const CopyPlugin = require("copy-webpack-plugin");
|
||||||
|
|
||||||
|
const filesToCopy = ["fxmanifest.lua", "wrapper.lua", "README.md", "LICENSE.md"];
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
devtool: "source-map",
|
||||||
|
entry: "./oxmysql.js",
|
||||||
|
resolve: {
|
||||||
|
extensions: [".js"],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new DefinePlugin({ "global.GENTLY": false }),
|
||||||
|
new IgnorePlugin({
|
||||||
|
resourceRegExp: /^cardinal$/,
|
||||||
|
contextRegExp: /./,
|
||||||
|
}),
|
||||||
|
new CopyPlugin({
|
||||||
|
patterns: filesToCopy,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
output: {
|
||||||
|
filename: "oxmysql.js",
|
||||||
|
path: path.resolve(__dirname, "dist"),
|
||||||
|
},
|
||||||
|
target: "node",
|
||||||
|
};
|
||||||
Reference in new issue
Block a user