MySQL ORM

Hi, i made a simple orm for mysql-async/oxmysql.
MySQL tables are created via annotations, pretty cool ngl. If anyone wants to use it, there is it. (nothing for configolopers / pseudo devs).

Creating repository

Each repository is created using a function and annotation. For annotations, you choose a table name for @entity type and then columns with data type, name, and additional sql lang data for the @column types.
If we chosed export way before, we use exports.maku_sqlorm:createRepository, if adding to the manifest, we skip the export call for orm script.
The first argument for createRepository must be the path to the repository file so that the parser can retrieve the data from annotations.

--- @entity players
--- @column id number NOT NULL AUTO_INCREMENT
--- @column nick string NOT NULL
--- @column money number NOT NULL DEFAULT '0'
--- @primaryKey id
local function createPlayersRepository()
    return exports.maku_sqlorm:createRepository('example/players.lua') -- Using export case
    return createRepository('example/players.lua', GetCurrentResourceName()) -- Including file in manifest case
end

Then we call function and define a variable.

PlayersRepository = createPlayersRepository()

Now we have created a repository object and can use its functions. A list of all functions can be found below

Repository functions

Main

local object = Repository:find(column, value)
Repository:findAsync(column, value, callback)

object.save()
local objects = Repository:findAll()
Repository:findAllAsync(callback)
local object = Repository:createEntity(data)
object.save()

When the save or saveAsync function is called on object, the object in the database is updated or, if it does not have a defined primary key, it is created.

Other

Repository:insert(data)
Repository:insertAsync(data, callback)

Repository:update(data)
Repository:updateAsync(data, callback)

Repository:delete(id)
Repository:deleteAsync(id, callback)

Great example can be found there.

Dependencies

2 Likes

Great work!

Just so I can understand more about Object Relational Mapping and it’s implementations in FiveM, what would you say is the most effective feature of this script?

Would this optimize queries when developing resources that use MySQL?

Thanks again for making this resource, always looking forward to developer libraries like this!

2 Likes

It’s an automatic data conversion when querying the database. You don’t have to write individual queries and then process the data, it simply makes the call for you and works with the deserialized object all the time, which is then serialized into columns for MySQL.
In the future I want to add a caching system to improve performance, as with other ORMs.

That’s awesome, thank you for explaining that to me!

Also, a caching system like you were thinking about would be amazing!

2 Likes