[Standalone] OxMySQL - Lightweight MySQL wrapper

Something like this?

local MySQL = {}
MySQL.Async = {}
MySQL.Sync = {}

setmetatable(MySQL, {
	__index = function(self, method)
		self[method] = setmetatable({}, {
			__call = oxmysql[method],
			__index = function(_, await)
				assert(await == 'await', ('unable to index MySQL.%s.%s, expected .await'):format(method, await))
				self[method].await = function(query, parameters)
					return Await(oxmysql[method], safeArgs(query, parameters))
				end
				return self[method].await
			end
		})
		return self[method]
	end
})

MySQL.Async.fetchScalar = MySQL.scalar
MySQL.Sync.fetchScalar = MySQL.scalar.await
CreateThread(function()
    local r = MySQL.scalar.await('SELECT identifier from users WHERE lastname = ?', {'Linden'})
    print('MySQL.scalar.await', r)

    MySQL.scalar('SELECT identifier from users WHERE lastname = ?', {'Linden'}, function(r)

        print('MySQL.scalar', r)
    end)

    Wait(0)
    print('\n\n')
    
    local r = MySQL.Sync.fetchScalar('SELECT identifier from users WHERE lastname = ?', {'Linden'})
    print('MySQL.Sync.fetchScalar', r)

    MySQL.Async.fetchScalar('SELECT identifier from users WHERE lastname = ?', {'Linden'}, function(r)

        print('MySQL.Async.fetchScalar', r)
    end)
end)

No excessive table or function creation, reuses what gets created.

1 Like

I would love to. :slight_smile:


I was more thinking about something like :

exports.oxmysql:fetch("SELECT column FROM table") -- I don't care about the result
local result = Citizen.Await(exports.oxmysql:fetch("SELECT column FROM table")) -- I need the result

But I have no idea how much a promise (not awaited) cost.


Ok so it is not that much, but it is still a win.
Thank you for all the detailed explanations !


This syntax is good !

If it happens to have weirdness, another good syntax could be :

MySQL.fetchSingle -- callback
MySQL.await.fetchSingle
1 Like

I like this one !

exports.oxmysql:query (previously execute)
MySQL.Async.fetchAll = MySQL.query
MySQL.Sync.fetchAll = MySQL.query.await

exports.oxmysql:scalar
MySQL.Async.fetchScalar = MySQL.scalar
MySQL.Sync.fetchScalar = MySQL.scalar.await

exports.oxmysql:single
MySQL.Async.fetchSingle = MySQL.single
MySQL.Sync.fetchSingle = MySQL.single.await

exports.oxmysql:insert
MySQL.Async.insert = MySQL.insert
MySQL.Sync.insert = MySQL.insert.await

exports.oxmysql:update
MySQL.Async.execute = MySQL.update
MySQL.Sync.execute = MySQL.update.await

exports.oxmysql:transaction
MySQL.Async.transaction = MySQL.transaction
MySQL.Sync.transaction = MySQL.transaction.await

exports.oxmysql:prepare
MySQL.Async.prepare = MySQL.prepare
MySQL.Sync.prepare = MySQL.prepare.await

Iā€™d like to be able to use execute for prepare but thatā€™s just another way to confuse the fuck out of people with the mysql-async and ghmatti divide (especially when MySQL.Async.execute exists).

I end up killing execute entirely and opt for query as the general export. Iā€™ll still have _async variants for returning promises too.

4 Likes

Nice Work

Latest release is tested with both the QBCore and ESX recipes included with txAdmin; functioning as intended without any changes.

Note that QBCore will require updates to utilise lib/MySQL and is currently using the deprecated exports.

1 Like

Hey, maybe you can tell me why Iā€™m getting these warnings?

Itā€™s called a slow query; it happens when your server hitches. Really canā€™t tell you more than that.

will the mysql store version be released at oxmysql?

Itā€™s in lib/MySQL for compatibility, but thereā€™s not much benefit if any to actually implementing it into the resource considering itā€™s just an array of strings to reference.

So, what can I do if scripts uses mysql.store?

Like I said, store exists in lib/MySQL for compatibility.
server_script '@oxmysql/lib/MySQL.lua' is the preferred usage as Iā€™ve posted several times here and on github, you get mysql-async syntax.

got a question about the removal of exports.oxmysql:fetch and exports.myoxmysql:fetchSync in your document you dont say what they change to. I have 21 files that use one or the other. should they all change to MySQL.query for fetch and MySQL.query.await for fetchSync?

i got them too

Hello you say that node-mysql2 is preferred on this system, how do you implent that, should i write a string or make a app.js on every resource?

i know how too install that in the folder nodejs but is this only thing to do?

1 Like

fetch/fetchSync were deprecated way back in 1.3, and synonymous with execute.
Since weā€™re pushing for the mysql-async backwards-compatible API then youā€™d be using MySQL.query (and yes, await for Sync) or MySQL.Async.fetchAll.

Weā€™re using node-mysql2 to create the connection, then handling the the arguments, typecasting, and API to interact with it; you donā€™t need to set it up in your own resources (and it would be a bit weird to build it into all of your resources when a single one is enough).

You need to webpack all this stuff.

Thanks <3 so its only install and thats it :slight_smile:

I will soon edit post to be more specific about latest release and mostly about upcoming v2.0.0 because right now it is a bit outdated.

V2 will also introduce advanced debugging features and mostly UI.

As always if any issue occurs post it on github.