mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 22:21:51 +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
|
||||
.pnp.*
|
||||
|
||||
# FiveM
|
||||
.yarn.installed
|
||||
+42
-47
@@ -1,17 +1,15 @@
|
||||
/// <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 connectionString = GetConvar("mysql_connection_string", "");
|
||||
|
||||
if (connectionString === "") throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||
|
||||
const connectionStringParser = new ConnectionStringParser({
|
||||
const config = new ConnectionStringParser({
|
||||
scheme: "mysql",
|
||||
hosts: [],
|
||||
});
|
||||
|
||||
const config = connectionStringParser.parse(connectionString);
|
||||
}).parse(connectionString);
|
||||
|
||||
if (Object.keys(config).length === 0)
|
||||
throw new Error(
|
||||
@@ -19,8 +17,8 @@ if (Object.keys(config).length === 0)
|
||||
);
|
||||
|
||||
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
||||
|
||||
const debug = GetConvar("mysql_debug", "false") === "true";
|
||||
const resourceName = GetCurrentResourceName() || "oxmysql";
|
||||
|
||||
const pool = createPool({
|
||||
host: config.hosts[0].host,
|
||||
@@ -28,8 +26,19 @@ const pool = createPool({
|
||||
password: config.password,
|
||||
database: config.endpoint,
|
||||
charset: "utf8mb4_unicode_ci",
|
||||
namedPlaceholders: true,
|
||||
...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
|
||||
@@ -41,69 +50,55 @@ pool
|
||||
console.error(error.message);
|
||||
});
|
||||
|
||||
const formatQuery = (query, params) => {
|
||||
if (!Array.isArray(params)) {
|
||||
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());
|
||||
const execute = async (query, parameters, prepare = true) => {
|
||||
ScheduleResourceTick(resourceName);
|
||||
try {
|
||||
const [formattedQuery, formattedParameters] = formatQuery(query, parameters);
|
||||
|
||||
const startTime = process.hrtime.bigint();
|
||||
const [result] = await pool.execute(formattedQuery, formattedParameters);
|
||||
const executionTime = Number(process.hrtime.bigint() - startTime) / 1000000;
|
||||
|
||||
if (executionTime >= slowQueryWarning)
|
||||
console.warn(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
||||
else if (debug) console.log(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
||||
const [result] = prepare
|
||||
? await pool.execute(query, parameters)
|
||||
: 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;
|
||||
} catch (error) {
|
||||
if (debug) {
|
||||
console.error(query);
|
||||
console.error(parameters);
|
||||
}
|
||||
return console.error(error.message);
|
||||
console.error(error.message, debug && query, debug && parameters);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
global.exports("execute", (query, parameters, callback = () => {}) => {
|
||||
execute(query, parameters).then(
|
||||
(result) => typeof callback === "function" && callback(result && result.affectedRows)
|
||||
execute(query, parameters, false).then(
|
||||
(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 = () => {}) => {
|
||||
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 = () => {}) => {
|
||||
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 = () => {}) => {
|
||||
execute(query, parameters).then(
|
||||
(result) =>
|
||||
typeof callback === "function" && callback(result && result[0] && Object.values(result[0])[0])
|
||||
);
|
||||
});
|
||||
|
||||
global.exports("insert", (query, parameters, callback = () => {}) => {
|
||||
execute(query, parameters).then(
|
||||
(result) => typeof callback === "function" && callback(result && result.insertId)
|
||||
typeof callback === "function" &&
|
||||
callback((result && result[0] && Object.values(result[0])[0]) || false)
|
||||
);
|
||||
});
|
||||
+15
-2
@@ -1,15 +1,28 @@
|
||||
{
|
||||
"name": "oxmysql",
|
||||
"private": "true",
|
||||
"version": "1.1.0",
|
||||
"main": "oxmysql.js",
|
||||
"description": "FiveM MySQL wrapper using faster mysql2",
|
||||
"repository": "[email protected]:overextended/oxmysql.git",
|
||||
"author": "dunak-debug <[email protected]>",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"watch": "webpack --watch",
|
||||
"postinstall": "patch-package"
|
||||
},
|
||||
"dependencies": {
|
||||
"@citizenfx/server": "^2.0.4376-1",
|
||||
"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