mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e5d4cce06 | ||
|
|
681b39692a | ||
|
|
39c485644d | ||
|
|
634bbffd46 | ||
|
|
273a4a8a89 | ||
|
|
b8249d5457 | ||
|
|
372a95632c | ||
|
|
faae020dd4 |
No files matched your search
+1
-1
@@ -3,7 +3,7 @@ game 'common'
|
|||||||
|
|
||||||
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 '1.3.5'
|
version '1.4.1'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
use_fxv2_oal 'yes'
|
use_fxv2_oal 'yes'
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"version": "1.3.5",
|
"version": "1.4.1",
|
||||||
"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",
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
"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",
|
||||||
|
"github-version-checker": "^2.2.0",
|
||||||
"mysql2": "^2.3.0",
|
"mysql2": "^2.3.0",
|
||||||
"postinstall-postinstall": "^2.1.0"
|
"postinstall-postinstall": "^2.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
diff --git a/node_modules/github-version-checker/lib/check.js b/node_modules/github-version-checker/lib/check.js
|
||||||
|
index 7f00060..cf0e231 100644
|
||||||
|
--- a/node_modules/github-version-checker/lib/check.js
|
||||||
|
+++ b/node_modules/github-version-checker/lib/check.js
|
||||||
|
@@ -88,7 +88,6 @@ const rest = (options, callback) => {
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
const mapped = schemeMapper[options.fetchTags ? 'tag' : 'release'](version);
|
||||||
|
- console.log(mapped);
|
||||||
|
callback(null, mapped);
|
||||||
|
} else {
|
||||||
|
callback(null, null);
|
||||||
@@ -7,7 +7,7 @@ index 527d723..f47b591 100644
|
|||||||
// License: https://github.com/mscdex/node-mariasql/blob/master/LICENSE
|
// 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,
|
||||||
+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,
|
DQUOTE = 34,
|
||||||
SQUOTE = 39,
|
SQUOTE = 39,
|
||||||
BSLASH = 92;
|
BSLASH = 92;
|
||||||
|
|||||||
+38
-1
@@ -1,4 +1,6 @@
|
|||||||
import { ConnectionStringParser } from 'connection-string-parser';
|
import { ConnectionStringParser } from 'connection-string-parser';
|
||||||
|
import versionCheck from 'github-version-checker';
|
||||||
|
import currentVersion from '../package.json';
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
const connectionString = GetConvar('mysql_connection_string', '');
|
||||||
|
|
||||||
@@ -15,4 +17,39 @@ 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';
|
||||||
|
|
||||||
export { config, slowQueryWarning, debug };
|
const isolationLevel = (() => {
|
||||||
|
switch (GetConvarInt('mysql_transaction_isolation_level', 2)) {
|
||||||
|
case 1:
|
||||||
|
return 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ';
|
||||||
|
case 2:
|
||||||
|
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
|
||||||
|
case 3:
|
||||||
|
return 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED';
|
||||||
|
case 4:
|
||||||
|
return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE';
|
||||||
|
default:
|
||||||
|
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
||||||
|
|
||||||
|
export { config, slowQueryWarning, debug, isolationLevel, resourceName };
|
||||||
|
|
||||||
|
setImmediate(async () => {
|
||||||
|
const versionCheckConfig = {
|
||||||
|
repo: 'oxmysql',
|
||||||
|
owner: 'overextended',
|
||||||
|
currentVersion: currentVersion.version,
|
||||||
|
latest: true
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const update = await versionCheck(versionCheckConfig);
|
||||||
|
if (update) {
|
||||||
|
console.log(`^3Your version of oxmysql is outdated (v${versionCheckConfig.currentVersion})! Please update to the latest version (${update.name}) here: \nhttps://github.com/overextended/oxmysql/releases/latest`);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
+1
-3
@@ -1,8 +1,6 @@
|
|||||||
import { pool } from './pool';
|
import { pool } from './pool';
|
||||||
import { parseParameters } from './parser';
|
import { parseParameters } from './parser';
|
||||||
import { slowQueryWarning, debug } from './config';
|
import { slowQueryWarning, debug, resourceName } from './config';
|
||||||
|
|
||||||
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
|
||||||
|
|
||||||
const execute = async (query, parameters, resource) => {
|
const execute = async (query, parameters, resource) => {
|
||||||
ScheduleResourceTick(resourceName);
|
ScheduleResourceTick(resourceName);
|
||||||
|
|||||||
+14
-2
@@ -1,10 +1,11 @@
|
|||||||
import { pool } from './pool';
|
import { pool } from './pool';
|
||||||
import { execute } from './execute';
|
import { execute } from './execute';
|
||||||
import { debug } from './config';
|
import { transaction } from './transaction';
|
||||||
|
import { debug, isolationLevel } from './config';
|
||||||
|
|
||||||
setImmediate(async () => {
|
setImmediate(async () => {
|
||||||
try {
|
try {
|
||||||
await (await pool.getConnection()).ping();
|
await pool.query(isolationLevel);
|
||||||
console.log(`^2Database server connection established!^0`);
|
console.log(`^2Database server connection established!^0`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
||||||
@@ -48,6 +49,12 @@ global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource(
|
|||||||
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0], resource, query));
|
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0], resource, query));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
global.exports('transaction', (queries, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
transaction(queries, parameters, resource).then((result) => {
|
||||||
|
safeCallback(cb || parameters, result || false, resource, debug && JSON.stringify(queries));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
||||||
global.exports('executeSync', async (query, parameters) => {
|
global.exports('executeSync', async (query, parameters) => {
|
||||||
const result = await execute(query, parameters, GetInvokingResource());
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
@@ -78,4 +85,9 @@ if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
|||||||
const result = await execute(query, parameters, GetInvokingResource());
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
return result && result[0] && Object.values(result[0])[0];
|
return result && result[0] && Object.values(result[0])[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
global.exports('transactionSync', async (queries, parameters) => {
|
||||||
|
const result = await transaction(queries, parameters, GetInvokingResource());
|
||||||
|
return result;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
+21
-4
@@ -28,7 +28,7 @@ const parseTypes = (field, next) => {
|
|||||||
const parseParameters = (query, parameters) => {
|
const parseParameters = (query, parameters) => {
|
||||||
if (query === undefined) throw new FormatError(`Undefined query passed`);
|
if (query === undefined) throw new FormatError(`Undefined query passed`);
|
||||||
|
|
||||||
if(typeof parameters === 'function') return [query, []];
|
if (typeof parameters === 'function') return [query, []];
|
||||||
|
|
||||||
if (query.includes('@') || query.includes(':')) return [query, parameters];
|
if (query.includes('@') || query.includes(':')) return [query, parameters];
|
||||||
|
|
||||||
@@ -41,14 +41,31 @@ const parseParameters = (query, parameters) => {
|
|||||||
|
|
||||||
if (Array.isArray(parameters)) {
|
if (Array.isArray(parameters)) {
|
||||||
if (parameters.length !== queryParams.length)
|
if (parameters.length !== queryParams.length)
|
||||||
throw new Error(`Undefined array parameter #${parameters.length}`, query, parameters);
|
throw new Error(`Undefined array parameter #${parameters.length + 1}`, query, parameters);
|
||||||
} else {
|
} else {
|
||||||
queryParams.forEach((_, i) => {
|
queryParams.forEach((_, i) => {
|
||||||
if (parameters[`${i + 1}`] === undefined) throw new FormatError(`Undefined object parameter #${i + 1}`, query, parameters);
|
if (parameters[`${i + 1}`] === undefined)
|
||||||
|
throw new FormatError(`Undefined object parameter #${i + 1}`, query, parameters);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return [query, parameters];
|
return [query, parameters];
|
||||||
};
|
};
|
||||||
|
|
||||||
export { parseParameters, parseTypes };
|
const parseTransaction = (queries, parameters) => {
|
||||||
|
//https://github.com/GHMatti/ghmattimysql/blob/37f1d2ae5c53f91782d168fe81fba80512d3c46d/packages/ghmattimysql/src/server/utility/sanitizeTransactionInput.ts#L5
|
||||||
|
|
||||||
|
if (!Array.isArray(queries)) throw new Error(`Transaction queries must be array type`);
|
||||||
|
|
||||||
|
const parsedTransaction = queries.map((query) => {
|
||||||
|
const [parsedQuery, parsedParameters] = parseParameters(
|
||||||
|
typeof query === 'object' ? query.query : query,
|
||||||
|
(typeof query === 'object' && (query.parameters || query.values)) || parameters || []
|
||||||
|
);
|
||||||
|
return { query: parsedQuery, params: parsedParameters };
|
||||||
|
});
|
||||||
|
|
||||||
|
return parsedTransaction;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { parseTypes, parseParameters, parseTransaction };
|
||||||
@@ -4,10 +4,12 @@ import { parseTypes } from './parser';
|
|||||||
|
|
||||||
export const pool = createPool({
|
export const pool = createPool({
|
||||||
host: config.hosts[0].host || 'localhost',
|
host: config.hosts[0].host || 'localhost',
|
||||||
|
port: config.hosts[0].port || 3306,
|
||||||
user: config.username || 'root',
|
user: config.username || 'root',
|
||||||
password: config.password || '',
|
password: config.password || '',
|
||||||
database: config.endpoint || 'es_extended',
|
database: config.endpoint || 'es_extended',
|
||||||
charset: 'utf8mb4_unicode_ci',
|
charset: 'utf8mb4_unicode_ci',
|
||||||
|
connectTimeout: 30000,
|
||||||
...config.options,
|
...config.options,
|
||||||
namedPlaceholders: true,
|
namedPlaceholders: true,
|
||||||
typeCast: parseTypes,
|
typeCast: parseTypes,
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { pool } from './pool';
|
||||||
|
import { parseTransaction } from './parser';
|
||||||
|
import { slowQueryWarning, debug, resourceName } from './config';
|
||||||
|
|
||||||
|
const transactionError = (queries, parameters) =>
|
||||||
|
`${queries
|
||||||
|
.map((query) =>
|
||||||
|
typeof query === 'string' ? query : `${query.query} ${JSON.stringify(query.values || query.parameters || [])}`
|
||||||
|
)
|
||||||
|
.join('\n')}\n${JSON.stringify(parameters)}`;
|
||||||
|
|
||||||
|
const transaction = async (queries, parameters, resource) => {
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
try {
|
||||||
|
const time = debug ? process.hrtime.bigint() : Date.now();
|
||||||
|
|
||||||
|
const fullQuery = parseTransaction(queries, parameters);
|
||||||
|
const transactionAmount = fullQuery.length;
|
||||||
|
|
||||||
|
await connection.beginTransaction();
|
||||||
|
|
||||||
|
for (let i = 0; i < transactionAmount; i++) {
|
||||||
|
await connection.query(fullQuery[i].query, fullQuery[i].params);
|
||||||
|
}
|
||||||
|
|
||||||
|
await connection.commit();
|
||||||
|
|
||||||
|
const executionTime = debug ? Number(process.hrtime.bigint() - time) / 1e6 : Date.now() - time;
|
||||||
|
|
||||||
|
if (executionTime >= slowQueryWarning * transactionAmount || debug)
|
||||||
|
console.log(
|
||||||
|
`^3[${
|
||||||
|
debug ? 'DEBUG' : 'WARNING'
|
||||||
|
}] ${resource} took ${executionTime}ms to execute a transaction!\n${transactionError(queries, parameters)}^0`
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
await connection.rollback();
|
||||||
|
console.log(
|
||||||
|
`^1[ERROR] ${resource} was unable to execute a transaction!
|
||||||
|
${error.message}
|
||||||
|
${error.sql || `${transactionError(queries, parameters)}`}^0`
|
||||||
|
);
|
||||||
|
debug && console.trace(error);
|
||||||
|
} finally {
|
||||||
|
connection.release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { transaction };
|
||||||
+1
-2
@@ -3,8 +3,7 @@ const { DefinePlugin, IgnorePlugin } = require("webpack");
|
|||||||
const CopyPlugin = require("copy-webpack-plugin");
|
const CopyPlugin = require("copy-webpack-plugin");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
mode: "development",
|
mode: "production",
|
||||||
devtool: "eval-source-map",
|
|
||||||
entry: "./src/index.js",
|
entry: "./src/index.js",
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: [".js"],
|
extensions: [".js"],
|
||||||
|
|||||||
+16
@@ -93,3 +93,19 @@ exports('updateSync', function(query, parameters)
|
|||||||
end, GetInvokingResource())
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---
|
||||||
|
-- Execute a transaction and return boolean depending on success
|
||||||
|
--
|
||||||
|
-- @param queries table
|
||||||
|
-- @param parameters table
|
||||||
|
--
|
||||||
|
-- @return boolean
|
||||||
|
--
|
||||||
|
exports('transactionSync', function(queries, parameters)
|
||||||
|
local p = promise.new()
|
||||||
|
exports['oxmysql']:transaction(queries, parameters, function(result)
|
||||||
|
p:resolve(result)
|
||||||
|
end, GetInvokingResource())
|
||||||
|
return Citizen.Await(p)
|
||||||
|
end)
|
||||||
@@ -571,6 +571,19 @@ get-stream@^6.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-6.0.1.tgz#a262d8eef67aced57c2852ad6167526a43cbf7b7"
|
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-6.0.1.tgz#a262d8eef67aced57c2852ad6167526a43cbf7b7"
|
||||||
integrity sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==
|
integrity sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==
|
||||||
|
|
||||||
|
github-graphql-client@^1.0.0:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/github-graphql-client/-/github-graphql-client-1.0.0.tgz#d5acacc31bb9f6b77e5c502812e90c844f727bca"
|
||||||
|
integrity sha1-1ayswxu59rd+XFAoEukMhE9ye8o=
|
||||||
|
|
||||||
|
github-version-checker@^2.2.0:
|
||||||
|
version "2.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/github-version-checker/-/github-version-checker-2.2.0.tgz#08a2b52f9ef9cd3dd05b898537750a91b73facec"
|
||||||
|
integrity sha512-Ttj/Uzg++TGfycfn5ga8BfMJ8QYB1+DhG24rtasrN8tHYWgqqrEMAQ52y2qGnPNWzSF8AUdjm6dBcoPdUhKhDQ==
|
||||||
|
dependencies:
|
||||||
|
github-graphql-client "^1.0.0"
|
||||||
|
semver "^5.5.1"
|
||||||
|
|
||||||
glob-parent@^5.1.2:
|
glob-parent@^5.1.2:
|
||||||
version "5.1.2"
|
version "5.1.2"
|
||||||
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
|
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
|
||||||
@@ -1120,7 +1133,7 @@ schema-utils@^3.0.0, schema-utils@^3.1.0, schema-utils@^3.1.1:
|
|||||||
ajv "^6.12.5"
|
ajv "^6.12.5"
|
||||||
ajv-keywords "^3.5.2"
|
ajv-keywords "^3.5.2"
|
||||||
|
|
||||||
semver@^5.5.0, semver@^5.6.0:
|
semver@^5.5.0, semver@^5.5.1, semver@^5.6.0:
|
||||||
version "5.7.1"
|
version "5.7.1"
|
||||||
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
|
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
|
||||||
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
|
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
|
||||||
|
|||||||
Reference in new issue
Block a user