Hey everyone!
It’s been a while since I released anything of value since forever. So today I release my side project that allows you to quickly execute simple SQL queries to your database without anything more to think about.
I’ll start with I plan on improving and further extending the functionality and optimization with this script, if you encounter any problems at any point I’ll always try to help as fast and best as I can! ![]()
Here are some before and afters from my friend after he refactored his code the first time.
---before
RegisterNetEvent("esx:playerLoaded", function(player, xPlayer, isNew)
local identifier = xPlayer.getIdentifier()
MySQL.query("SELECT `level`, `xp` FROM `postop_job` WHERE `identifier` = ?", {
identifier
}, function(response)
if response then
if response[1] then
Players[player] = { level = response[1].level, xp = response[1].xp }
else
MySQL.insert.await("INSERT INTO `postop_job` (identifier) VALUES (?)", {
identifier
})
Players[player] = { level = 1, xp = 0 }
end
end
end)
end)
---after
-- prepared objects are reusable
local PJDB = exports.DatabaseManager:GetDatabaseTableManager("postop_job")
local PJDB_DATA_SELECT = PJDB.Prepare.Select({
"identifier"
})
local PJDB_DATA_INSERT = PJDB.Prepare.Insert({
"identifier"
})
RegisterNetEvent("esx:playerLoaded", function(player, xPlayer, isNew)
local identifier = xPlayer.getIdentifier()
local data = PJDB_DATA_SELECT.execute(identifier)
if data then
Players[player] = { level = data.level, xp = data.xp }
else
PJDB_DATA_INSERT.execute(identifier)
Players[player] = { level = 1, xp = 0 }
end
end)
In DatabaseManager I also included multiple other functions and methods that allow you to further customize the way you type queries using prepared functions for a database table.
local DM = exports.DatabaseManager:GetDatabaseTableManager("table")
---DM.Create(args: {string: column_name, string: column_types}[])
-- Overloaded functions with the same name but different parameters
---DM.Prepare.Select(conditions: table|nil, cb: function|nil, individual: boolean|nil, query: string|nil)
---DM.Prepare.Select(fields: string[], cb: function|nil, individual: boolean|nil, query: string|nil)
---DM.Prepare.Select(cb: function)
---DM.Prepare.Update(updates: string[], condition: table, cb: function|nil)
---DM.Prepare.Update(updates_fields: string[], condition_fields: string[], cb: function|nil)
-- Every Prepare object returns a table with two functions, `.update(params)` and `.execute(...args)`
-- `.update` simply replaces arguments set at the creation of given prepared object
-- `.execute` executes the query with provided parameters according to desired statement
-- Examples with code could be such as:
---The only part that requires an SQL, of course if you don't know if a table is created already
DM.Create({
{"id", "INT(11) UNSIGNED PRIMARY KEY AUTO_INCREMENT"},
{"name", "VARCHAR(255) NOT NULL UNIQUE"},
{"price", "INT(11) UNSIGNED NULL DEFAULT 0"}
})
local select_row = DM.Prepare.Select({"name"})
print(select_row.execute("some_name"))
local select_row2 = DM.Prepare.Select({
name = "some_name"
})
print(select_row2.execute())
---update name field based on a column having its value as "some_name"
local update_row = DM.Prepare.Update({"name"}, {
name = "some_name"
})
update_row.execute("changed_name")
---update name(param 1) based on it's last name(param 2)
local update_row2 = DM.Prepare.Update({"name"}, {"name"})
update_row2.execute({"changed_name"}, {"some_name"})
In the code above I showcased overloaded methods with the Prepare statement. There are other prepared statements such as Delete, DeleteRows, Insert, InsertRows. There are examples on github in examples.lua file, also the entire server.lua and database_utils.lua are documented using LDoc so in any case you can just read which function accepts which parameter types.
As a last sentence I’ll leave a note that this is mainly intended for tables that are to be used in given script, but you also have an option to completely skip the single table access and use exports.DatabaseManager:GetDatabaseManager() and then simply provide table name as a first param to every function, but I leave the rest for you. ![]()
(github) Fivem-Script-Lab/DatabaseManager
Not required but I include this little list
| Code is accessible | Yes |
| Subscription-based | No |
| Lines (approximately) | 1100 |
| Requirements | oxmysql |
| Support | Always |