Skip to content

Commit

Permalink
refactor: enable JS exports for more recent artifacts
Browse files Browse the repository at this point in the history
We still don't have a bump in recommended artifact, so for now check for the existence of new(ish) native.
  • Loading branch information
thelindat committed Nov 30, 2021
1 parent 3e266ec commit dbe107e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 92 deletions.
184 changes: 94 additions & 90 deletions lua/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,100 +28,104 @@ CreateThread(function()
end, 'GET')
end)

---@param query string
---@param parameters? table
---@return integer result
---returns array of matching rows or result data
exports('executeSync', function(query, parameters)
local p = promise.new()
oxmysql:execute(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
-- Check for a recent native since the recommended artifact doesn't support JS async_retval or dependency constraints
if not GetEntityAttachedTo then

---@param query string
---@param parameters? table
---@return table result
---returns array of matching rows or result data
exports('fetchSync', function(query, parameters)
local p = promise.new()
oxmysql:fetch(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer result
---returns array of matching rows or result data
exports('executeSync', function(query, parameters)
local p = promise.new()
oxmysql:execute(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return table result
---returns table containing key value pairs
exports('singleSync', function(query, parameters)
local p = promise.new()
oxmysql:single(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns array of matching rows or result data
exports('fetchSync', function(query, parameters)
local p = promise.new()
oxmysql:fetch(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return integer|string
---returns value of the first column of a single row
exports('scalarSync', function(query, parameters)
local p = promise.new()
oxmysql:scalar(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns table containing key value pairs
exports('singleSync', function(query, parameters)
local p = promise.new()
oxmysql:single(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return table result
---returns the last inserted id
exports('insertSync', function(query, parameters)
local p = promise.new()
oxmysql:insert(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer|string
---returns value of the first column of a single row
exports('scalarSync', function(query, parameters)
local p = promise.new()
oxmysql:scalar(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return integer result
---returns number of rows updated by the executed query
exports('updateSync', function(query, parameters)
local p = promise.new()
oxmysql:update(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns the last inserted id
exports('insertSync', function(query, parameters)
local p = promise.new()
oxmysql:insert(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param queries table
---@param parameters? table
---@return boolean result
---returns true when the transaction has succeeded
exports('transactionSync', function(queries, parameters)
local p = promise.new()
oxmysql:transaction(queries, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer result
---returns number of rows updated by the executed query
exports('updateSync', function(query, parameters)
local p = promise.new()
oxmysql:update(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param queries table
---@param parameters? table
---@return boolean result
---returns true when the transaction has succeeded
exports('transactionSync', function(queries, parameters)
local p = promise.new()
oxmysql:transaction(queries, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters table
---@return any result
---supports SELECT, INSERT, UPDATE, and DELETE
---
---return value is based on the number of results and the type of query used
exports('prepareSync', function(query, parameters)
local p = promise.new()
oxmysql:prepare(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters table
---@return any result
---supports SELECT, INSERT, UPDATE, and DELETE
---
---return value is based on the number of results and the type of query used
exports('prepareSync', function(query, parameters)
local p = promise.new()
oxmysql:prepare(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
end
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ const isolationLevel = (() => {
}
})();

const resourceName = GetCurrentResourceName() || 'oxmysql';
const resourceName = GetCurrentResourceName();

export { slowQueryWarning, debug, isolationLevel, resourceName };
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource
safeCallback(cb || parameters, result, resource, query))
});

if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
if (GetEntityAttachedTo || !GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
global.exports('prepareSync', async (query, parameters) => {
const result = await preparedStatement(query, parameters, GetInvokingResource());
return result;
Expand Down

0 comments on commit dbe107e

Please sign in to comment.