Skip to content

Commit

Permalink
fix: JS sync exports check
Browse files Browse the repository at this point in the history
Since JS is such a bad language it throws an error when doing `variable !== undefined`, so wrap the whole thing in a try block and catch the error.
  • Loading branch information
thelindat committed Dec 3, 2021
1 parent 94fe763 commit 87a5123
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
3 changes: 2 additions & 1 deletion lua/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ game 'common'

name 'oxmysql'
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
version '1.8.3'
version '1.8.4'
url 'https://github.com/overextended/oxmysql'
author 'overextended'
use_fxv2_oal 'yes'
lua54 'yes'

server_scripts {
-- '/server:4837', Disabled until the heat death of the universe (or recommended artifact updates)
'oxmysql.js',
'wrapper.lua'
}
Expand Down
5 changes: 3 additions & 2 deletions lua/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ CreateThread(function()
end, 'GET')
end)

-- Check for a recent native since the recommended artifact doesn't support JS async_retval or dependency constraints
if not GetEntityAttachedTo then
-- Check for the existance of a native from FXServer 4837 to disable Lua exports
-- Shame dependency constraints were added immediately after the recommended build
if not MumbleSetPlayerMuted then

---@param query string
---@param parameters? table
Expand Down
90 changes: 48 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,51 @@ global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource
preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
});

// Check for a recent native (~server artifact 4700) to enable JS exports
if (GetEntityAttachedTo !== undefined || !GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
global.exports('prepareSync', async (query, parameters) => {
const result = await preparedStatement(query, parameters, GetInvokingResource());
return result;
});

global.exports('executeSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('insertSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.insertId;
});

global.exports('updateSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.affectedRows;
});

global.exports('fetchSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('singleSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0];
});

global.exports('scalarSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0] && Object.values(result[0])[0];
});

global.exports('transactionSync', async (queries, parameters) => {
const result = await transaction(queries, parameters, GetInvokingResource());
return result;
});
}
try {
// Check for the existance of a native from FXServer 4837 to enable JS exports
if (MumbleSetPlayerMuted || !GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
global.exports('prepareSync', async (query, parameters) => {
const result = await preparedStatement(query, parameters, GetInvokingResource());
return result;
});

global.exports('executeSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('insertSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.insertId;
});

global.exports('updateSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.affectedRows;
});

global.exports('fetchSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('singleSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0];
});

global.exports('scalarSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0] && Object.values(result[0])[0];
});

global.exports('transactionSync', async (queries, parameters) => {
const result = await transaction(queries, parameters, GetInvokingResource());
return result;
});
}
} catch(e) {
setTimeout(() => {
console.log(`^3Unable to load enhanced sync exports (download FXServer 4837+)^0`);
}, 1000)
}

0 comments on commit 87a5123

Please sign in to comment.