mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93d86f849a | ||
|
|
d6886bdd3f | ||
|
|
8c6c6119f1 | ||
|
|
2af99e35a7 | ||
|
|
03e6e534f5 | ||
|
|
45f9f72daa | ||
|
|
c4a9db0a79 | ||
|
|
8be251382d | ||
|
|
6bfda0c64d | ||
|
|
cf8eb3e590 | ||
|
|
44c4ea5fba |
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
ko_fi: dunak
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
<h2 align='center'>oxmysql (WIP) - not ready for use<p><a href='https://overextended.github.io/oxmysql/'>Documentation</a></p></h2>
|
<h2 align='center'>oxmysql<p><a href='https://overextended.github.io/oxmysql/'>Documentation</a></p></h2>
|
||||||
|
|
||||||
|
<p align='center'><a href='https://discord.io/overextended'>Discord</a></p>
|
||||||
|
|
||||||
oxmysql is a middleware for FXServer which utilises [node-mysql2](https://github.com/sidorares/node-mysql2) to communicate with a database and respond with the query results.
|
oxmysql is a middleware for FXServer which utilises [node-mysql2](https://github.com/sidorares/node-mysql2) to communicate with a database and respond with the query results.
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -3,8 +3,9 @@ game 'gta5'
|
|||||||
|
|
||||||
name 'oxmysql'
|
name 'oxmysql'
|
||||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||||
version '0.1.0'
|
version '1.1.0'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
|
|
||||||
server_scripts {'oxmysql.js', 'wrapper.lua'}
|
server_scripts {'oxmysql.js', 'wrapper.lua'}
|
||||||
|
server_only 'true'
|
||||||
+83
-52
@@ -1,78 +1,109 @@
|
|||||||
/// <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, format, escape } = require("mysql2/promise");
|
||||||
const { ConnectionStringParser } = require('connection-string-parser');
|
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 connectionStringParser = new ConnectionStringParser({
|
||||||
scheme: 'mysql',
|
scheme: "mysql",
|
||||||
hosts: []
|
hosts: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
|
||||||
|
|
||||||
if(connectionString === '') throw new Error(`Undefined convar mysql_connection_string`);
|
|
||||||
|
|
||||||
const config = connectionStringParser.parse(connectionString);
|
const config = connectionStringParser.parse(connectionString);
|
||||||
|
|
||||||
//TODO: semicolor format
|
if (Object.keys(config).length === 0)
|
||||||
|
throw new Error(
|
||||||
|
`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`
|
||||||
|
);
|
||||||
|
|
||||||
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 100)
|
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
||||||
|
|
||||||
|
const debug = GetConvar("mysql_debug", "false") === "true";
|
||||||
|
|
||||||
const pool = createPool({
|
const pool = createPool({
|
||||||
host: config.hosts[0].host,
|
host: config.hosts[0].host,
|
||||||
user: config.username,
|
user: config.username,
|
||||||
password: config.password,
|
password: config.password,
|
||||||
database: config.endpoint,
|
database: config.endpoint,
|
||||||
charset: 'utf8mb4_unicode_ci',
|
charset: "utf8mb4_unicode_ci",
|
||||||
multipleStatements: false,
|
namedPlaceholders: true,
|
||||||
namedPlaceholders: true,
|
...config.options,
|
||||||
waitForConnections: true,
|
|
||||||
connectionLimit: 10,
|
|
||||||
queueLimit: 0,
|
|
||||||
...config.options
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pool
|
||||||
|
.getConnection()
|
||||||
|
.then(() => {
|
||||||
|
console.info("Database server connection established.");
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
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) => {
|
const execute = async (query, parameters) => {
|
||||||
ScheduleResourceTick(GetCurrentResourceName());
|
ScheduleResourceTick(GetCurrentResourceName());
|
||||||
try {
|
try {
|
||||||
const startTime = process.hrtime.bigint();
|
const [formattedQuery, formattedParameters] = formatQuery(query, parameters);
|
||||||
const [result] = await pool.execute(query, parameters);
|
|
||||||
const executionTime = new Number((process.hrtime.bigint() - startTime)) / 1000000
|
|
||||||
|
|
||||||
if(executionTime >= slowQueryWarning)
|
const startTime = process.hrtime.bigint();
|
||||||
console.warn(`${query} took ${executionTime}ms!`)
|
const [result] = await pool.execute(formattedQuery, formattedParameters);
|
||||||
|
const executionTime = Number(process.hrtime.bigint() - startTime) / 1000000;
|
||||||
|
|
||||||
return result;
|
if (executionTime >= slowQueryWarning)
|
||||||
} catch (error) {
|
console.warn(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
||||||
return console.error(error.message);
|
else if (debug) console.log(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
if (debug) {
|
||||||
|
console.error(query);
|
||||||
|
console.error(parameters);
|
||||||
}
|
}
|
||||||
}
|
return console.error(error.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
global.exports("execute", (query, parameters, callback = () => { }) => {
|
global.exports("execute", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then(
|
||||||
callback(result && result.affectedRows)
|
(result) => typeof callback === "function" && callback(result && result.affectedRows)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("fetch", (query, parameters, callback = () => { }) => {
|
global.exports("fetch", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then((result) => typeof callback === "function" && callback(result));
|
||||||
callback(result)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("single", (query, parameters, callback = () => { }) => {
|
global.exports("single", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then(
|
||||||
callback(result && result[0])
|
(result) => typeof callback === "function" && callback(result && result[0])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("scalar", (query, parameters, callback = () => { }) => {
|
global.exports("scalar", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then(
|
||||||
callback(result && result[0] && Object.values(result[0])[0])
|
(result) =>
|
||||||
);
|
typeof callback === "function" && callback(result && result[0] && Object.values(result[0])[0])
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("insert", (query, parameters, callback = () => { }) => {
|
global.exports("insert", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then(
|
||||||
callback(result && result.insertId)
|
(result) => typeof callback === "function" && callback(result && result.insertId)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"private": "true",
|
"private": "true",
|
||||||
"version": "0.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",
|
||||||
|
|||||||
Reference in new issue
Block a user