feat(mysql): setup execute export

- also setup test.lua for testing
This commit is contained in:
dunak-debug committed 2021-08-16 00:12:28 +02:00
1 parent 7afd440931
commit b2ade77589
8 files changed
+139 -10

No files matched your search

View File
Whitespace-only changes.
+12
View File
@@ -0,0 +1,12 @@
fx_version 'cerulean'
game 'gta5'
name 'oxmysql'
description 'FiveM MySQL wrapper using faster mysql2'
version '1.0.0'
url 'https://github.com/overextended/oxmysql'
author 'Dunak'
server_script 'dist/oxmysql.js'
server_script 'test.lua'
+6 -3
View File
@@ -2,6 +2,7 @@
"name": "oxmysql",
"private": "true",
"version": "1.0.0",
"main": "src/index.ts",
"description": "FiveM MySQL wrapper using faster mysql2",
"repository": "[email protected]:overextended/oxmysql.git",
"author": "dunak-debug <[email protected]>",
@@ -10,15 +11,17 @@
"start": "webpack --watch --progress",
"build": "webpack"
},
"dependencies": {
"@citizenfx/server": "^2.0.4376-1",
"mysql2": "^2.3.0"
},
"devDependencies": {
"@types/node": "^16.6.1",
"cardinal": "^2.1.1",
"mysql2": "^2.3.0",
"ts-loader": "^9.2.5",
"ts-node": "^10.2.0",
"typescript": "^4.3.5",
"webpack": "^5.50.0",
"webpack-cli": "^4.7.2"
},
"dependencies": {}
}
}
+32 -6
View File
@@ -1,7 +1,33 @@
const mysql2 = require("mysql2");
import { QueryError } from "mysql2/promise";
import { pool } from "./pool";
const pool = mysql2.createPool({
host: "localhost",
user: "root",
database: "es_extended",
});
const execute = (
query: string,
callback: (result: any) => void,
parameters?: { [key: string]: any }
) => {
const startTime = process.hrtime.bigint();
pool
.execute(query, parameters)
.then((result) => {
const end = process.hrtime.bigint();
console.log(`Query took ${Number(end - startTime) / 1000000} ms!`);
callback(result[0]);
})
.catch((error) => {
callback(false);
console.log(error);
});
};
global.exports(
"execute",
(
query: string,
parameters: { [key: string]: any },
callback: (result: any) => void
) => {
execute(query, callback, parameters);
}
);
+9
View File
@@ -0,0 +1,9 @@
import { createPool } from "mysql2/promise";
const pool = createPool({
host: "localhost",
user: "root",
database: "es_extended",
});
export { pool };
+75
View File
@@ -0,0 +1,75 @@
function tprint(tbl, indent)
if not indent then
indent = 0
end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent + 1)
elseif type(v) == 'boolean' then
print(formatting .. tostring(v))
else
print(formatting .. v)
end
end
end
exports('executeSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:execute(query, parameters, function(result)
p:resolve(result)
end)
return Citizen.Await(p)
end)
exports('executeSync2', function(query, parameters)
local p = promise.new()
exports['mysql-async']:mysql_fetch_all(query, parameters, function(result)
p:resolve(result)
end)
return Citizen.Await(p)
end)
exports('executeSync3', function(query, parameters)
local p = promise.new()
exports['ghmattimysql']:execute(query, parameters, function(result)
p:resolve(result)
end)
return Citizen.Await(p)
end)
RegisterCommand("mysql2", function()
local time = os.clock()
exports.oxmysql:executeSync('SELECT * FROM user_inventory');
print('[Sync] mysql2: ' .. ((os.clock() - time) * 1000) .. 'ms')
end)
RegisterCommand('mysql-async', function()
local time = os.clock()
exports.oxmysql:executeSync2('SELECT * FROM user_inventory');
print('[Sync] mysql-async: ' .. ((os.clock() - time) * 1000) .. 'ms')
end)
RegisterCommand("ghmat", function()
local time = os.clock()
exports.oxmysql:executeSync3('SELECT * FROM user_inventory');
print('[Sync] ghmattimysql: ' .. ((os.clock() - time) * 1000) .. 'ms')
end)
local measure = function(query, params)
print('------- ' .. query .. ' --------')
local time = os.clock()
exports.oxmysql:executeSync(query, params);
print('[Sync] mysql2: ' .. ((os.clock() - time) * 1000) .. 'ms')
time = os.clock()
exports.oxmysql:executeSync2(query, params);
print('[Sync] mysql-async: ' .. ((os.clock() - time) * 1000) .. 'ms')
time = os.clock()
exports.oxmysql:executeSync3(query, params);
print('[Sync] ghmattimysql: ' .. ((os.clock() - time) * 1000) .. 'ms')
end
RegisterCommand("test", function()
measure('SELECT 1')
end)
+2 -1
View File
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs"
"module": "commonjs",
"types": ["@citizenfx/server", "@types/node"]
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
+3
View File
@@ -2,6 +2,9 @@ const path = require('path');
module.exports = {
mode: "production",
optimization: {
minimize: false
},
target: 'node',
entry: './src/index.ts',
module: {