diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 9d0f582..cf7f33f 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -31,8 +31,9 @@ jobs: - name: Bundle files run: | mkdir -p ./temp/oxmysql - cp ./{LICENSE.md,README.md,fxmanifest.lua,lib} ./temp/oxmysql - zip -r oxmysql-${{ github.event.inputs.tag }}.zip ./temp/oxmysql + cp ./{LICENSE.md,README.md,fxmanifest.lua} ./temp/oxmysql + cp -r ./{lib,dist} ./temp/oxmysql + cd ./temp && zip -r ../oxmysql-${{ github.event.inputs.tag }}.zip ./oxmysql - name: Create Release uses: "marvinpinto/action-automatic-releases@v1.2.1" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 99cebcb..7fa03f9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,8 +32,9 @@ jobs: - name: Bundle files run: | mkdir -p ./temp/oxmysql - cp ./{LICENSE.md,README.md,fxmanifest.lua,lib} ./temp/oxmysql - zip -r oxmysql-${{ github.event.inputs.tag }}.zip ./temp/oxmysql + cp ./{LICENSE.md,README.md,fxmanifest.lua} ./temp/oxmysql + cp -r ./{lib,dist} ./temp/oxmysql + cd ./temp && zip -r ../oxmysql-${{ github.event.inputs.tag }}.zip ./oxmysql - name: Create Release uses: "marvinpinto/action-automatic-releases@v1.2.1" diff --git a/lua/fxmanifest.lua b/fxmanifest.lua similarity index 57% rename from lua/fxmanifest.lua rename to fxmanifest.lua index 26c9f83..a3417ea 100644 --- a/lua/fxmanifest.lua +++ b/fxmanifest.lua @@ -3,19 +3,16 @@ game 'common' name 'oxmysql' description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.' -version '1.8.7' +version '1.9.0' url 'https://github.com/overextended/oxmysql' author 'overextended' -use_fxv2_oal 'yes' -lua54 'yes' ---dependencies { --- '/server:4837', Disabled until the heat death of the universe (or recommended artifact updates) ---} +dependencies { + '/server:5104', +} server_scripts { - 'oxmysql.js', - 'wrapper.lua' + 'dist/server/build.js', } provide 'mysql-async' diff --git a/lib/MySQL.lua b/lib/MySQL.lua new file mode 100644 index 0000000..079b8e0 --- /dev/null +++ b/lib/MySQL.lua @@ -0,0 +1,212 @@ +-- lib/MySQL.lua provides complete compatibility for resources using mysql-async functions +-- As of v2.0.0 this is the preferred method of interacting with oxmysql +-- * Though some function names are not 100% accurate, mysql-async provides a "standard" (for better or worse) +-- * Using this lib provides minor improvements to performance and helps debug poor queries +-- * Resources are not bound to using oxmysql where the user may prefer another option (compatibility libraries are common) + +local Store = {} + +local function safeArgs(query, parameters, cb, transaction) + if type(query) == 'number' then query = Store[query] end + if transaction then + assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query)) + else + assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query)) + end + if cb then + assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb)) + end + local type = parameters and type(parameters) + if type and type ~= 'table' and type ~= 'function' then + assert(nil, ('A %s was expected, but instead received %s'):format(cb and 'table' or 'function', parameters)) + end + return query, parameters, cb +end + +local promise = promise +local oxmysql = exports.oxmysql +local GetCurrentResourceName = GetCurrentResourceName() +local Citizen_Await = Citizen.Await + +local function Await(fn, query, parameters) + local p = promise.new() + fn(nil, query, parameters, function(result) + p:resolve(result) + end, GetCurrentResourceName) + return Citizen_Await(p) +end + +local MySQL = {} +--- Results will be returned to a callback function without halting the execution of the current thread. +MySQL.Async = {} +--- The current thread will yield until the query has resolved, returning results to a variable. +MySQL.Sync = {} + +---@param query string +---@param cb? function +---@return number result +--- returns the id used to reference a stored query string +function MySQL.Async.store(query, cb) + assert(type(query) == 'string', 'The SQL Query must be a string') + local store = #Store+1 + Store[store] = query + cb(store) +end + +---@param query string +---@return number result +--- returns the id used to reference a stored query string +function MySQL.Sync.store(query) + assert(type(query) == 'string', 'The SQL Query must be a string') + local store = #Store+1 + Store[store] = query + return store +end + +---@param query string +---@param parameters? table|function +---@param cb? function +---@return number result +--- returns number of affected rows +function MySQL.Async.execute(query, parameters, cb) + query, parameters, cb = safeArgs(query, parameters, cb) + oxmysql:update_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters? table +---@return number result +--- returns number of affected rows +function MySQL.Sync.execute(query, parameters) + return Await(oxmysql.update_callback, safeArgs(query, parameters)) +end + +---@param query string +---@param parameters? table|function +---@param cb? function +---@return table result +--- returns array of matching rows or result data +function MySQL.Async.fetchAll(query, parameters, cb) + query, parameters, cb = safeArgs(query, parameters, cb) + oxmysql:query_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters? table +---@return table result +--- returns array of matching rows or result data +function MySQL.Sync.fetchAll(query, parameters) + return Await(oxmysql.query_callback, safeArgs(query, parameters)) +end + +---@param query string +---@param parameters? table|function +---@param cb? function +---@return any result +--- returns value of the first column of a single row +function MySQL.Async.fetchScalar(query, parameters, cb) + query, parameters, cb = safeArgs(query, parameters, cb) + oxmysql:scalar_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters? table +---@return any result +--- returns value of the first column of a single row +function MySQL.Sync.fetchScalar(query, parameters) + return Await(oxmysql.scalar_callback, safeArgs(query, parameters)) +end + +---@param query string +---@param parameters? table|function +---@param cb? function +---@return table result +--- returns table containing key value pairs +function MySQL.Async.fetchSingle(query, parameters, cb) + query, parameters, cb = safeArgs(query, parameters, cb) + oxmysql:single_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters? table +---@return table result +--- returns table containing key value pairs +function MySQL.Sync.fetchSingle(query, parameters) + return Await(oxmysql.single_callback, safeArgs(query, parameters)) +end + +---@param query string +---@param parameters? table|function +---@param cb? function +---@return number result +--- returns the insert id of the executed query +function MySQL.Async.insert(query, parameters, cb) + query, parameters, cb = safeArgs(query, parameters, cb) + oxmysql:insert_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters? table +---@return number result +--- returns the insert id of the executed query +function MySQL.Sync.insert(query, parameters) + return Await(oxmysql.insert_callback, safeArgs(query, parameters)) +end + +---@param queries table +---@param parameters? table|function +---@param cb? function +---@return boolean result +--- returns true when the transaction has succeeded +function MySQL.Async.transaction(queries, parameters, cb) + queries, parameters, cb = safeArgs(queries, parameters, cb, true) + oxmysql:transaction_callback(queries, parameters, cb, GetCurrentResourceName) +end + +---@param queries table +---@param parameters? table +---@return boolean result +--- returns true when the transaction has succeeded +function MySQL.Sync.transaction(queries, parameters) + return Await(oxmysql.transaction_callback, safeArgs(queries, parameters, false, true)) +end + +---@param query string +---@param parameters table +---@param cb? function +---@return any result +--- Utilises a separate function to execute queries more efficiently. The return type will differ based on the query submitted. +--- Parameters can be a single table containing placeholders (perform one query) or contain multiple tables with a set of placeholders, i.e +--- ```lua +--- MySQL.Async.prepare('SELECT * FROM users WHERE firstname = ?', {{'Dunak'}, {'Linden'}, {'Luke'}}) +--- MySQL.Async.prepare('SELECT * FROM users WHERE firstname = ?', {'Linden'}) +--- ```` +--- When selecting a single row the result will match fetchSingle, or a single column will match fetchScalar. +function MySQL.Async.prepare(query, parameters, cb) + oxmysql:execute_callback(query, parameters, cb, GetCurrentResourceName) +end + +---@param query string +---@param parameters table +---@return any result +--- Utilises a separate function to execute queries more efficiently. The return type will differ based on the query submitted. +--- Parameters can be a single table containing placeholders (perform one query) or contain multiple tables with a set of placeholders, i.e +--- ```lua +--- MySQL.Sync.prepare('SELECT * FROM users WHERE firstname = ?', {{'Dunak'}, {'Linden'}, {'Luke'}}) +--- MySQL.Sync.prepare('SELECT * FROM users WHERE firstname = ?', {'Linden'}) +--- ```` +--- When selecting a single row the result will match fetchSingle, or a single column will match fetchScalar. +function MySQL.Sync.prepare(query, parameters) + return Await(oxmysql.execute_callback, safeArgs(query, parameters)) +end + +function MySQL.ready(cb) + CreateThread(function() + repeat + Wait(50) + until GetResourceState('oxmysql') == 'started' + cb() + end) +end + +_ENV.MySQL = MySQL diff --git a/lua/lib/MySQL.lua b/lua/lib/MySQL.lua deleted file mode 100644 index 1196d51..0000000 --- a/lua/lib/MySQL.lua +++ /dev/null @@ -1,187 +0,0 @@ --- This file allows simple conversion from MySQL-Async to Oxmysql --- Replace any references to '@mysql-async' in your resource manifests to use '@oxmysql' instead --- Store is used for compatibility only - -local Ox = exports.oxmysql -local Store = {} - -local function safeArgs(query, parameters, cb, transaction) - if type(query) == 'number' then query = Store[query] end - if transaction then - assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query)) - else - assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query)) - end - if cb then - assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb)) - end - local type = parameters and type(parameters) - if type and type ~= 'table' and type ~= 'function' then - assert(nil, ('A %s was expected, but instead received %s'):format(cb and 'table' or 'function', parameters)) - end - return query, parameters, cb -end - -MySQL = { Async = {}, Sync = {} } - ----@param query string ----@param parameters? table|function ----@param cb? function ----@return integer result ----returns number of rows updated by the executed query -MySQL.Async.execute = function(query, parameters, cb) - Ox:update(safeArgs(query, parameters, cb)) -end - ----@param query string ----@param parameters? table|function ----@param cb? function ----@return table result ----returns array of matching rows or result data -MySQL.Async.fetchAll = function(query, parameters, cb) - Ox:execute(safeArgs(query, parameters, cb)) -end - ----@param query string ----@param parameters? table|function ----@param cb? function ----@return integer|string ----returns value of the first column of a single row -MySQL.Async.fetchScalar = function(query, parameters, cb) - Ox:scalar(safeArgs(query, parameters, cb)) -end - ----@param query string ----@param parameters? table|function ----@param cb? function ----@return table result ----returns table containing key value pairs -MySQL.Async.fetchSingle = function(query, parameters, cb) - Ox:single(safeArgs(query, parameters, cb)) -end - ----@param query string ----@param parameters? table|function ----@param cb? function ----@return table result ----returns the last inserted id -MySQL.Async.insert = function(query, parameters, cb) - Ox:insert(safeArgs(query, parameters, cb)) -end - ----@param queries table ----@param parameters? table|function ----@param cb? function ----@return boolean result ----returns true when the transaction has succeeded -MySQL.Async.transaction = function(queries, parameters, cb) - Ox:transaction(safeArgs(queries, parameters, cb, true)) -end - ----@param query string ----@param cb? function ----@return integer result ----returns the id used to reference a stored query string -MySQL.Async.store = function(query, cb) - assert(type(query) == 'string', 'The SQL Query must be a string') - local store = #Store+1 - Store[store] = query - cb(store) -end - ----@param query string ----@param parameters? table|function ----@return integer result ----returns number of rows updated by the executed query -MySQL.Sync.execute = function(query, parameters) - query, parameters = safeArgs(query, parameters) - local promise = promise.new() - Ox:update(query, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param query string ----@param parameters? table|function ----@return table result ----returns array of matching rows or result data -MySQL.Sync.fetchAll = function(query, parameters) - query, parameters = safeArgs(query, parameters) - local promise = promise.new() - Ox:execute(query, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param query string ----@param parameters? table|function ----@return integer|string ----returns value of the first column of a single row -MySQL.Sync.fetchScalar = function(query, parameters) - query, parameters = safeArgs(query, parameters) - local promise = promise.new() - Ox:scalar(query, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param query string ----@param parameters? table|function ----@return table result ----returns table containing key value pairs -MySQL.Sync.fetchSingle = function(query, parameters) - query, parameters = safeArgs(query, parameters) - local promise = promise.new() - Ox:single(query, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param query string ----@param parameters? table|function ----@return table result ----returns the last inserted id -MySQL.Sync.insert = function(query, parameters) - query, parameters = safeArgs(query, parameters) - local promise = promise.new() - Ox:insert(query, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param queries table ----@param parameters? table|function ----@return boolean result ----returns true when the transaction has succeeded -MySQL.Sync.transaction = function(queries, parameters) - queries, parameters = safeArgs(queries, parameters, false, true) - local promise = promise.new() - Ox:transaction(queries, parameters, function(result) - promise:resolve(result) - end) - return Citizen.Await(promise) -end - ----@param query string ----@return integer result ----returns the id used to reference a stored query string -MySQL.Sync.store = function(query) - assert(type(query) == 'string', 'The SQL Query must be a string') - local store = #Store+1 - Store[store] = query - return store -end - -MySQL.ready = function(cb) - CreateThread(function() - repeat - Wait(50) - until GetResourceState('oxmysql') == 'started' - cb() - end) -end diff --git a/lua/wrapper.lua b/lua/wrapper.lua deleted file mode 100644 index 5388689..0000000 --- a/lua/wrapper.lua +++ /dev/null @@ -1,127 +0,0 @@ -local oxmysql = exports.oxmysql - -CreateThread(function() - Wait(1000) - local resource = GetCurrentResourceName() - local url = GetResourceMetadata(resource, 'url', 0) - local version = GetResourceMetadata(resource, 'version', 0) - - PerformHttpRequest(('%s/main/lua/fxmanifest.lua'):format(url:gsub('github.com', 'raw.githubusercontent.com')), function(error, response) - if error == 200 then - local latest = response:match('%d%.%d+%.%d+') - if version ~= latest then - local curMajor, curMinor = string.strsplit('.', version) - local newMajor, newMinor = string.strsplit('.', response:match('%d%.%d+%.%d+')) - local link = ('%s/releases/download/v%s/oxmysql-v%s.zip'):format(url, latest, latest) - - if tonumber(curMajor) < tonumber(newMajor) then - latest = 'A major update' - elseif tonumber(curMinor) < tonumber(newMinor) then - latest = 'An update' - else - latest = 'A patch' - end - - print(('^3%s is available for oxmysql - please update to the latest release (current version: %s)\n ^3- %s^0'):format(latest, version, link)) - end - end - 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) - ----@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 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 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 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 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) \ No newline at end of file diff --git a/src/errors.js b/src/errors.js deleted file mode 100644 index fadb81c..0000000 --- a/src/errors.js +++ /dev/null @@ -1,9 +0,0 @@ -class FormatError extends Error { - constructor(message, query, parameters) { - super(message); - if (parameters) this.sql = `${query} ${JSON.stringify(parameters)}`; - else this.sql = query; - } -} - -export { FormatError }; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 84265ec..0000000 --- a/src/index.js +++ /dev/null @@ -1,85 +0,0 @@ -import { execute, preparedStatement } from './execute'; -import { transaction } from './transaction'; -import { debug } from './config'; - -const safeCallback = (callback, result) => { - if (typeof callback === 'function') callback(result); -}; - -global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result)); -}); - -global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.insertId)); -}); - -global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.affectedRows)); -}); - -global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result)); -}); - -global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result[0])); -}); - -global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => { - execute(query, parameters, resource).then((result) => - safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0]) - ); -}); - -global.exports('transaction', (queries, parameters, cb, resource = GetInvokingResource()) => { - transaction(queries, parameters, resource).then((result) => { - safeCallback(cb || parameters, result || false, resource, debug && JSON.stringify(queries)); - }); -}); - -global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource()) => { - preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result)); -}); - -if (!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; - }); -} \ No newline at end of file diff --git a/src/server/deprecated.js b/src/server/deprecated.js new file mode 100644 index 0000000..5faef3c --- /dev/null +++ b/src/server/deprecated.js @@ -0,0 +1,83 @@ +import sql_execute from './db/execute.js'; +import sql_query from './db/query.js'; +import sql_transaction from './db/transaction.js'; + +let deprecated = (invokingResource) => { + setTimeout(() => { + console.log( + `^3[WARNING] ${invokingResource} triggered a deprecated function! Exports from prior to v1.9.0 will be removed in a future update. + Refer to the readme for information on updating.^0` + ); + }, 500); + deprecated = undefined; +}; + +global.exports('execute', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_query('', invokingResource, query, parameters, cb); +}); + +global.exports('executeSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_query('', invokingResource, query, parameters); +}); + +global.exports('single', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_query('single', invokingResource, query, parameters, cb); +}); + +global.exports('singleSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_query('single', invokingResource, query, parameters); +}); + +global.exports('scalar', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_query('scalar', invokingResource, query, parameters, cb); +}); + +global.exports('scalarSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_query('scalar', invokingResource, query, parameters); +}); + +global.exports('update', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_query('update', invokingResource, query, parameters, cb); +}); + +global.exports('updateSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_query('update', invokingResource, query, parameters); +}); + +global.exports('insert', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_query('insert', invokingResource, query, parameters, cb); +}); + +global.exports('insertSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_query('insert', invokingResource, query, parameters); +}); + +global.exports('transaction', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_transaction(invokingResource, query, parameters, cb); +}); + +global.exports('transactionSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_transaction(invokingResource, query, parameters); +}); + +global.exports('prepare', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + sql_execute(invokingResource, query, parameters, cb); +}); + +global.exports('prepareSync', (query, parameters, invokingResource = GetInvokingResource()) => { + if (deprecated !== undefined) deprecated(invokingResource); + return sql_execute(invokingResource, query, parameters); +}); diff --git a/src/server/index.js b/src/server/index.js new file mode 100644 index 0000000..b474324 --- /dev/null +++ b/src/server/index.js @@ -0,0 +1,61 @@ +import sql_execute from './db/execute.js'; +import sql_query from './db/query.js'; +import sql_transaction from './db/transaction.js'; +require('./versioncheck.js'); +require('./deprecated.js'); + +global.exports('query_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_query('', invokingResource, query, parameters, cb); +}); + +global.exports('query_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_query('', invokingResource, query, parameters); +}); + +global.exports('single_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_query('single', invokingResource, query, parameters, cb); +}); + +global.exports('single_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_query('single', invokingResource, query, parameters); +}); + +global.exports('scalar_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_query('scalar', invokingResource, query, parameters, cb); +}); + +global.exports('scalar_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_query('scalar', invokingResource, query, parameters); +}); + +global.exports('update_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_query('update', invokingResource, query, parameters, cb); +}); + +global.exports('update_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_query('update', invokingResource, query, parameters); +}); + +global.exports('insert_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_query('insert', invokingResource, query, parameters, cb); +}); + +global.exports('insert_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_query('insert', invokingResource, query, parameters); +}); + +global.exports('transaction_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_transaction(invokingResource, query, parameters, cb); +}); + +global.exports('transaction_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_transaction(invokingResource, query, parameters); +}); + +global.exports('execute_callback', (query, parameters, cb, invokingResource = GetInvokingResource()) => { + sql_execute(invokingResource, query, parameters, cb); +}); + +global.exports('execute_async', (query, parameters, invokingResource = GetInvokingResource()) => { + return sql_execute(invokingResource, query, parameters); +}); diff --git a/src/server/versioncheck.js b/src/server/versioncheck.js new file mode 100644 index 0000000..8cd9750 --- /dev/null +++ b/src/server/versioncheck.js @@ -0,0 +1,34 @@ +import { resourceName } from './config'; +const https = require('https'); + +setTimeout(() => { + https + .request( + { + hostname: 'raw.githubusercontent.com', + port: 443, + path: '/overextended/oxmysql/main/lua/fxmanifest.lua', + method: 'GET', + }, + (res) => { + if (res.statusCode === 200) { + res.on('data', (data) => { + let currentVersion = GetResourceMetadata(resourceName, 'version', 0).match(/(\d)\.(\d+)\.(\d+)/); + let latestVersion = data.toString().match(/(\d)\.(\d+)\.(\d+)/); + if (currentVersion[0] !== latestVersion[0]) { + let update; + + if (currentVersion[1] < latestVersion[1]) update = 'a major update'; + else if (currentVersion[2] < latestVersion[2]) update = 'an update'; + else if (currentVersion[3] < latestVersion[3]) update = 'a patch'; + + console.log( + `^3There is ${update} available for oxmysql - please update to the latest release (current version: ${currentVersion[0]})\r\nhttps://github.com/overextended/oxmysql/releases/download/v${latestVersion[0]}/oxmysql-v${latestVersion[0]}.zip^0` + ); + } + }); + } + } + ) + .end(); +}, 1000);