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 |
No files matched your search
@@ -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
|
||||
.pnp.*
|
||||
|
||||
# FiveM
|
||||
.yarn.installed
|
||||
+1
-1
@@ -3,7 +3,7 @@ game 'gta5'
|
||||
|
||||
name 'oxmysql'
|
||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||
version '1.1.0'
|
||||
version '1.2.0'
|
||||
url 'https://github.com/overextended/oxmysql'
|
||||
author 'overextended'
|
||||
|
||||
|
||||
+49
-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,62 @@ 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);
|
||||
// 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) {
|
||||
if (debug) {
|
||||
console.error(query);
|
||||
console.error(parameters);
|
||||
}
|
||||
return console.error(error.message);
|
||||
console.error(error.message, query, parameters);
|
||||
}
|
||||
};
|
||||
|
||||
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)
|
||||
);
|
||||
});
|
||||
+16
-3
@@ -1,15 +1,28 @@
|
||||
{
|
||||
"name": "oxmysql",
|
||||
"private": "true",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.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,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