Skip to content

Commit

Permalink
improvement: Only display warnings if callback is defined
Browse files Browse the repository at this point in the history
- Non-function callbacks will display a warning.
- This makes the third argument optional for async queries.
  • Loading branch information
thelindat committed Sep 18, 2021
1 parent 4b2b564 commit c5f150e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ game 'gta5'

name 'oxmysql'
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
version '1.3.2'
version '1.3.3'
url 'https://github.com/overextended/oxmysql'
author 'overextended'
use_fxv2_oal 'yes'
Expand Down
22 changes: 12 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,41 @@ setImmediate(async () => {
}
});

const safeCallback = (callback, result, error) => {
if (typeof callback === 'function') return callback(result || false);
else return console.log(`^3[WARNING] ${error[0]} executed a query, but no callback function was defined!\n ^3 ${error[1]}^0`);
const safeCallback = (callback, result, resource, query) => {
if (typeof callback === 'function')
return callback(result || false);
else if (callback)
return console.log(`^3[WARNING] ${resource} executed a query, but no callback function was defined!\n ^3 ${query}^0`);
};

global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result, !cb && [resource, query]));
safeCallback(cb, result, resource, query));
});

global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result && result.insertId, !cb && [resource, query]));
safeCallback(cb, result && result.insertId, resource, query));
});

global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result && result.affectedRows, !cb && [resource, query]));
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result && result.affectedRows, resource, query));
});

global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result, !cb && [resource, query]));
safeCallback(cb, result, resource, query));
});

global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result && result[0], !cb && [resource, query]));
safeCallback(cb, result && result[0], resource, query));
});

global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb, result && result[0] && Object.values(result[0])[0], !cb && [resource, query]));
safeCallback(cb, result && result[0] && Object.values(result[0])[0], resource, query));
});

if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
Expand Down

0 comments on commit c5f150e

Please sign in to comment.