mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e31cc0ace | ||
|
|
e09b3482ba | ||
|
|
a31df20979 | ||
|
|
f6fb7cc017 | ||
|
|
04e03e4fef | ||
|
|
94bd814186 | ||
|
|
93d86f849a | ||
|
|
d6886bdd3f | ||
|
|
8c6c6119f1 | ||
|
|
2af99e35a7 | ||
|
|
03e6e534f5 | ||
|
|
45f9f72daa | ||
|
|
c4a9db0a79 | ||
|
|
8be251382d | ||
|
|
6bfda0c64d | ||
|
|
cf8eb3e590 | ||
|
|
44c4ea5fba |
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
ko_fi: dunak
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
create-release:
|
||||||
|
name: Build and Create Tagged Release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Install archive tools
|
||||||
|
run: sudo apt install zip
|
||||||
|
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
ref: ${{ github.ref }}
|
||||||
|
|
||||||
|
- name: Set env
|
||||||
|
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: yarn
|
||||||
|
|
||||||
|
- name: Run build
|
||||||
|
run: yarn build
|
||||||
|
|
||||||
|
- name: Bundle files
|
||||||
|
run: |
|
||||||
|
mv dist oxmysql
|
||||||
|
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: "marvinpinto/[email protected]"
|
||||||
|
id: auto_release
|
||||||
|
with:
|
||||||
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
title: ${{ env.RELEASE_VERSION }}
|
||||||
|
prerelease: false
|
||||||
|
files: oxmysql-${{ env.RELEASE_VERSION }}.zip
|
||||||
|
|
||||||
|
env:
|
||||||
|
CI: false
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -117,4 +117,5 @@ dist
|
|||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# FiveM
|
||||||
.yarn.installed
|
.yarn.installed
|
||||||
@@ -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.2.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'
|
||||||
+93
-60
@@ -1,78 +1,111 @@
|
|||||||
/// <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 } = require("connection-string-parser");
|
||||||
|
|
||||||
const connectionStringParser = new ConnectionStringParser({
|
const connectionString = GetConvar("mysql_connection_string", "");
|
||||||
scheme: 'mysql',
|
|
||||||
hosts: []
|
|
||||||
});
|
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
if (connectionString === "") throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||||
|
|
||||||
if(connectionString === '') throw new Error(`Undefined convar mysql_connection_string`);
|
const config = new ConnectionStringParser({
|
||||||
|
scheme: "mysql",
|
||||||
|
hosts: [],
|
||||||
|
}).parse(connectionString);
|
||||||
|
|
||||||
const config = connectionStringParser.parse(connectionString);
|
if (Object.keys(config).length === 0)
|
||||||
|
throw new Error(
|
||||||
|
`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`
|
||||||
|
);
|
||||||
|
|
||||||
//TODO: semicolor format
|
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
||||||
|
const debug = GetConvar("mysql_debug", "false") === "true";
|
||||||
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 100)
|
const resourceName = GetCurrentResourceName() || "oxmysql";
|
||||||
|
|
||||||
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,
|
...config.options,
|
||||||
namedPlaceholders: true,
|
namedPlaceholders: true,
|
||||||
waitForConnections: true,
|
typeCast: (field, next) => {
|
||||||
connectionLimit: 10,
|
if (field.type === "TINY" && field.length === 1) {
|
||||||
queueLimit: 0,
|
return field.string() === "1";
|
||||||
...config.options
|
} else if (field.type === "BIT") {
|
||||||
});
|
return field.buffer()[0] === 1;
|
||||||
|
} else if (field.type === "TIMESTAMP" || field.type === "DATETIME") {
|
||||||
const execute = async (query, parameters) => {
|
return new Date(field.string()).getTime() / 1000;
|
||||||
ScheduleResourceTick(GetCurrentResourceName());
|
} else {
|
||||||
try {
|
return next();
|
||||||
const startTime = process.hrtime.bigint();
|
|
||||||
const [result] = await pool.execute(query, parameters);
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
global.exports("execute", (query, parameters, callback = () => { }) => {
|
|
||||||
execute(query, parameters).then(result =>
|
|
||||||
callback(result && result.affectedRows)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("fetch", (query, parameters, callback = () => { }) => {
|
pool
|
||||||
execute(query, parameters).then(result =>
|
.getConnection()
|
||||||
callback(result)
|
.then(() => {
|
||||||
);
|
console.info("Database server connection established.");
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error.message);
|
||||||
|
});
|
||||||
|
|
||||||
|
const execute = async (query, parameters, prepare = true) => {
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
try {
|
||||||
|
const startTime = process.hrtime.bigint();
|
||||||
|
|
||||||
|
// FIX WHEN SENDING LUA {nil, something, nil} => [null, something, null] based on count of ? in query
|
||||||
|
// Named placeholders handles this in patch {var1 = nil, var2 = something} => { var1: null, var2: something }
|
||||||
|
if (Array.isArray(parameters) && query.includes("?")) {
|
||||||
|
const requiredParameters = query.match(/\?/g);
|
||||||
|
if (requiredParameters.length !== parameters.length)
|
||||||
|
requiredParameters.forEach((_, index) => (parameters[index] = parameters[index] || null));
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
console.error(error.message, query, parameters);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
global.exports("execute", (query, parameters, callback = () => {}) => {
|
||||||
|
execute(query, parameters, false).then(
|
||||||
|
(result) => typeof callback === "function" && callback(result || false)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("single", (query, parameters, callback = () => { }) => {
|
global.exports("insert", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters, false).then(
|
||||||
callback(result && result[0])
|
(result) => typeof callback === "function" && callback((result && result.insertId) || false)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("scalar", (query, parameters, callback = () => { }) => {
|
global.exports("fetch", (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 || false)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports("insert", (query, parameters, callback = () => { }) => {
|
global.exports("single", (query, parameters, callback = () => {}) => {
|
||||||
execute(query, parameters).then(result =>
|
execute(query, parameters).then(
|
||||||
callback(result && result.insertId)
|
(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]) || false)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
+16
-3
@@ -1,15 +1,28 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"private": "true",
|
"version": "1.2.0",
|
||||||
"version": "0.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,61 @@
|
|||||||
|
diff --git a/node_modules/named-placeholders/index.js b/node_modules/named-placeholders/index.js
|
||||||
|
index 527d723..1a955b4 100644
|
||||||
|
--- a/node_modules/named-placeholders/index.js
|
||||||
|
+++ b/node_modules/named-placeholders/index.js
|
||||||
|
@@ -3,7 +3,7 @@
|
||||||
|
// based on code from Brian White @mscdex mariasql library - https://github.com/mscdex/node-mariasql/blob/master/lib/Client.js#L272-L332
|
||||||
|
// License: https://github.com/mscdex/node-mariasql/blob/master/LICENSE
|
||||||
|
|
||||||
|
-const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g,
|
||||||
|
+const RE_PARAM = /(?:\?)|(?:[:,@](\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g,
|
||||||
|
DQUOTE = 34,
|
||||||
|
SQUOTE = 39,
|
||||||
|
BSLASH = 92;
|
||||||
|
@@ -94,15 +94,27 @@ function createCompiler(config) {
|
||||||
|
if (typeof params == 'undefined')
|
||||||
|
throw new Error('Named query contains placeholders, but parameters object is undefined');
|
||||||
|
|
||||||
|
+ let parsedParams = {}
|
||||||
|
+
|
||||||
|
+ for(const key in params) {
|
||||||
|
+ parsedParams[key.charAt(0) == '@' ? key.substring(1) : key] = params[key]
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
const tokens = tree[1];
|
||||||
|
for (let i=0; i < tokens.length; ++i) {
|
||||||
|
- arr.push(params[tokens[i]]);
|
||||||
|
+ if(typeof tokens[i] === 'number') {
|
||||||
|
+ const param = parsedParams[(tokens[i] + 1).toString()];
|
||||||
|
+ arr.push(param !== undefined ? param : null);
|
||||||
|
+ } else {
|
||||||
|
+ arr.push(parsedParams[tokens[i]] !== undefined ? parsedParams[tokens[i]] : null);
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
+
|
||||||
|
return [tree[0], arr];
|
||||||
|
}
|
||||||
|
|
||||||
|
function noTailingSemicolon(s) {
|
||||||
|
- if (s.slice(-1) == ':') {
|
||||||
|
+ if (s.slice(-1) == ':' || s.slice(-1) == '@') {
|
||||||
|
return s.slice(0, -1);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
@@ -115,7 +127,7 @@ function createCompiler(config) {
|
||||||
|
|
||||||
|
let unnamed = noTailingSemicolon(tree[0][0]);
|
||||||
|
for (let i=1; i < tree[0].length; ++i) {
|
||||||
|
- if (tree[0][i-1].slice(-1) == ':') {
|
||||||
|
+ if (tree[0][i-1].slice(-1) == ':' || tree[0][i-1].slice(-1) == '@') {
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
}
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
@@ -124,7 +136,7 @@ function createCompiler(config) {
|
||||||
|
|
||||||
|
const last = tree[0][tree[0].length -1];
|
||||||
|
if (tree[0].length == tree[1].length) {
|
||||||
|
- if (last.slice(-1) == ':') {
|
||||||
|
+ if (last.slice(-1) == ':' || last.slice(-1) == '@') {
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
}
|
||||||
|
unnamed += config.placeholder;
|
||||||
@@ -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