mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6acd0ac352 | ||
|
|
55d8d46334 | ||
|
|
4b179191d4 | ||
|
|
1ec52903ca |
No files matched your search
@@ -32,6 +32,8 @@ jobs:
|
|||||||
- name: Bundle files
|
- name: Bundle files
|
||||||
run: |
|
run: |
|
||||||
mv dist oxmysql
|
mv dist oxmysql
|
||||||
|
mkdir oxmysql/lib
|
||||||
|
mv lib/MySQL.lua oxmysql/lib/MySQL.lua
|
||||||
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
||||||
|
|
||||||
- name: Create Release
|
- name: Create Release
|
||||||
|
|||||||
+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.4.1'
|
version '1.5.0'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
use_fxv2_oal 'yes'
|
use_fxv2_oal 'yes'
|
||||||
|
|||||||
+146
@@ -0,0 +1,146 @@
|
|||||||
|
-- This file allows simple conversion from MySQL-Async to Oxmysql
|
||||||
|
-- Replace any references to '@mysql-async' in your resource manifests to use '@oxmysql' instead
|
||||||
|
-- Store is used for compatibility only
|
||||||
|
|
||||||
|
local Ox = exports.oxmysql
|
||||||
|
local Resource = GetCurrentResourceName()
|
||||||
|
|
||||||
|
local function safeParameters(parameters)
|
||||||
|
if not parameters or next(parameters) == nil then
|
||||||
|
return {[''] = ''}
|
||||||
|
end
|
||||||
|
assert(type(parameters) == 'table', 'Parameters must be a table')
|
||||||
|
return parameters
|
||||||
|
end
|
||||||
|
local Store = {}
|
||||||
|
|
||||||
|
MySQL = {
|
||||||
|
Async = {
|
||||||
|
execute = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:update(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchAll = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:execute(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchScalar = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:scalar(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchSingle = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:single(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
insert = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:insert(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
transaction = function(query, parameters, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
Ox:transaction(query, safeParameters(parameters), cb)
|
||||||
|
end,
|
||||||
|
|
||||||
|
store = function(query, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local store = #Store+1
|
||||||
|
Store[store] = query
|
||||||
|
cb(store)
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
Sync = {
|
||||||
|
execute = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:update(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchAll = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:execute(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchScalar = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:scalar(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
fetchSingle = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:scalar(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
insert = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:scalar(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
transaction = function(query, parameters)
|
||||||
|
if type(query) == 'number' then
|
||||||
|
query = Store[query]
|
||||||
|
end
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:scalar(query, safeParameters(parameters), function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end, Resource)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end,
|
||||||
|
|
||||||
|
store = function(query, parameters)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local store = #Store+1
|
||||||
|
Store[store] = query
|
||||||
|
return store
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
ready = function(cb)
|
||||||
|
CreateThread(function()
|
||||||
|
repeat
|
||||||
|
Wait(50)
|
||||||
|
until GetResourceState('oxmysql') == 'started'
|
||||||
|
cb()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"version": "1.4.1",
|
"version": "1.5.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",
|
||||||
|
|||||||
+7
-18
@@ -1,20 +1,7 @@
|
|||||||
import { ConnectionStringParser } from 'connection-string-parser';
|
|
||||||
import versionCheck from 'github-version-checker';
|
import versionCheck from 'github-version-checker';
|
||||||
import currentVersion from '../package.json';
|
import currentVersion from '../package.json';
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 150);
|
||||||
|
|
||||||
if (connectionString === '') throw new Error(`Set up mysql_connection_string in server.cfg`);
|
|
||||||
|
|
||||||
const config = new ConnectionStringParser({
|
|
||||||
scheme: 'mysql',
|
|
||||||
hosts: [],
|
|
||||||
}).parse(connectionString);
|
|
||||||
|
|
||||||
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 debug = GetConvar('mysql_debug', 'false') === 'true';
|
const debug = GetConvar('mysql_debug', 'false') === 'true';
|
||||||
|
|
||||||
const isolationLevel = (() => {
|
const isolationLevel = (() => {
|
||||||
@@ -29,25 +16,27 @@ const isolationLevel = (() => {
|
|||||||
return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE';
|
return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE';
|
||||||
default:
|
default:
|
||||||
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
|
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
|
||||||
};
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
||||||
|
|
||||||
export { config, slowQueryWarning, debug, isolationLevel, resourceName };
|
export { slowQueryWarning, debug, isolationLevel, resourceName };
|
||||||
|
|
||||||
setImmediate(async () => {
|
setImmediate(async () => {
|
||||||
const versionCheckConfig = {
|
const versionCheckConfig = {
|
||||||
repo: 'oxmysql',
|
repo: 'oxmysql',
|
||||||
owner: 'overextended',
|
owner: 'overextended',
|
||||||
currentVersion: currentVersion.version,
|
currentVersion: currentVersion.version,
|
||||||
latest: true
|
latest: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const update = await versionCheck(versionCheckConfig);
|
const update = await versionCheck(versionCheckConfig);
|
||||||
if (update) {
|
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`);
|
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) {
|
} catch (e) {
|
||||||
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
|
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
|
||||||
|
|||||||
+68
-13
@@ -1,16 +1,71 @@
|
|||||||
import { createPool } from 'mysql2/promise';
|
import { createPool } from 'mysql2/promise';
|
||||||
import { config } from './config';
|
import { debug } from './config';
|
||||||
import { parseTypes } from './parser';
|
import { parseTypes } from './parser';
|
||||||
|
import { ConnectionStringParser } from 'connection-string-parser';
|
||||||
|
|
||||||
export const pool = createPool({
|
const connectionString = GetConvar('mysql_connection_string', '');
|
||||||
host: config.hosts[0].host || 'localhost',
|
|
||||||
port: config.hosts[0].port || 3306,
|
if (connectionString === '') {
|
||||||
user: config.username || 'root',
|
throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||||
password: config.password || '',
|
}
|
||||||
database: config.endpoint || 'es_extended',
|
|
||||||
charset: 'utf8mb4_unicode_ci',
|
const parseSemiColons = () => {
|
||||||
connectTimeout: 30000,
|
const parts = connectionString.split(';');
|
||||||
...config.options,
|
if (parseTypes.length === 1) {
|
||||||
namedPlaceholders: true,
|
throw new Error(
|
||||||
typeCast: parseTypes,
|
`Set up mysql_connection_string in correct format - 'username=root;password=password;database=db;host=127.0.0.1'`
|
||||||
});
|
);
|
||||||
|
}
|
||||||
|
return parts.reduce((connectionInfo, parameter) => {
|
||||||
|
const [key, value] = parameter.split('=');
|
||||||
|
connectionInfo[key] = value;
|
||||||
|
return connectionInfo;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
const createConnection = () => {
|
||||||
|
if (connectionString.includes('database=')) {
|
||||||
|
const options = parseSemiColons();
|
||||||
|
|
||||||
|
return createPool({
|
||||||
|
host: options.host || 'localhost',
|
||||||
|
port: options.port || 3306,
|
||||||
|
user: options.username || options.user || options.userid || 'root',
|
||||||
|
password: options.password || options.pass || '',
|
||||||
|
database: options.endpoint || options.database || 'es_extended',
|
||||||
|
charset: 'utf8mb4_unicode_ci',
|
||||||
|
connectTimeout: 30000,
|
||||||
|
trace: debug || false,
|
||||||
|
debug: debug || false,
|
||||||
|
...options.options,
|
||||||
|
namedPlaceholders: true,
|
||||||
|
typeCast: parseTypes,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const options = new ConnectionStringParser({
|
||||||
|
scheme: 'mysql',
|
||||||
|
hosts: [],
|
||||||
|
}).parse(connectionString);
|
||||||
|
|
||||||
|
if (Object.keys(options).length === 0) {
|
||||||
|
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createPool({
|
||||||
|
host: options.hosts[0].host || 'localhost',
|
||||||
|
port: options.hosts[0].port || 3306,
|
||||||
|
user: options.username || 'root',
|
||||||
|
password: options.password || '',
|
||||||
|
database: options.endpoint || 'es_extended',
|
||||||
|
charset: 'utf8mb4_unicode_ci',
|
||||||
|
connectTimeout: 30000,
|
||||||
|
trace: debug || false,
|
||||||
|
debug: debug || false,
|
||||||
|
...options.options,
|
||||||
|
namedPlaceholders: true,
|
||||||
|
typeCast: parseTypes,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const pool = createConnection();
|
||||||
Reference in new issue
Block a user