feat: isReady and awaitConnection exports

This commit is contained in:
Linden committed 2023-06-13 02:42:54 +10:00
1 parent 11b21f8ed2
commit c97a07a377
2 files changed
+36 -14

No files matched your search

+26 -14
View File
@@ -64,16 +64,13 @@ local mysql_method_mt = {
end end
} }
local MySQL = { local MySQL = setmetatable(MySQL or {}, {
ready = function(cb) __index = function(_, index)
CreateThread(function() return function(...)
repeat return oxmysql[index](nil, ...)
Wait(50) end
until GetResourceState('oxmysql') == 'started'
cb()
end)
end end
} })
for _, method in pairs({ for _, method in pairs({
'scalar', 'single', 'query', 'insert', 'update', 'prepare', 'transaction', 'scalar', 'single', 'query', 'insert', 'update', 'prepare', 'transaction',
@@ -108,9 +105,6 @@ local alias_mt = {
end end
} }
setmetatable(MySQL.Async, alias_mt)
setmetatable(MySQL.Sync, alias_mt)
local function addStore(query, cb) local function addStore(query, cb)
assert(type(query) == 'string', 'The SQL Query must be a string') assert(type(query) == 'string', 'The SQL Query must be a string')
@@ -120,7 +114,25 @@ local function addStore(query, cb)
return cb and cb(storeN) or storeN return cb and cb(storeN) or storeN
end end
MySQL.Sync = { store = addStore } MySQL.Sync = setmetatable({ store = addStore }, alias_mt)
MySQL.Async = { store = addStore } MySQL.Async = setmetatable({ store = addStore }, alias_mt)
local function onReady(cb)
while GetResourceState('oxmysql') ~= 'started' do
Wait(50)
end
oxmysql.awaitConnection()
return cb and cb() or true
end
MySQL.ready = setmetatable({
await = onReady
}, {
__call = function(_, cb)
Citizen.CreateThreadNow(function() onReady(cb) end)
end,
})
_ENV.MySQL = MySQL _ENV.MySQL = MySQL
+10
View File
@@ -2,10 +2,20 @@ import { CFXCallback, CFXParameters, TransactionQuery } from './types';
import { rawQuery } from './database/rawQuery'; import { rawQuery } from './database/rawQuery';
import { rawTransaction } from './database/rawTransaction'; import { rawTransaction } from './database/rawTransaction';
import { rawExecute } from './database/rawExecute'; import { rawExecute } from './database/rawExecute';
import { isServerConnected, waitForConnection } from './database';
import('./update'); import('./update');
const MySQL = {} as Record<string, Function>; const MySQL = {} as Record<string, Function>;
MySQL.isReady = () => {
return isServerConnected;
};
MySQL.awaitConnection = async () => {
await waitForConnection();
return true;
};
MySQL.query = ( MySQL.query = (
query: string, query: string,
parameters: CFXParameters, parameters: CFXParameters,