[FREE] SQL QueryBuilder 🔨 (WIP)

Hey everyone!

I’m working on a Laravel-inspired QueryBuilder for FiveM, built using ox_lib and oxmysql. It’s meant to make working with databases in your scripts way easier, with stuff like models (haven’t started that yet), tables, and simple query methods—kind of like how Laravel does it.

Right now, it’s still super early and more of a showcase than anything. Things will probably break and change a lot as I keep working on it, but I wanted to share what I’ve got so far.

Feel free to check it out and drop any feedback or ideas!

GitHub Repository

Usage Examples

Here are some basic examples of how to use the query builder:

Complex Select Query

-- Retrieve a list of users filtered by group, grouped by identifier, ordered by lastname, and limited to 10 results
local users = DB:table("users")
    :select("identifier", "firstname", "lastname")
    :where("group", "LIKE", "%admin")
    :groupBy("identifier")
    :orderBy("lastname", "ASC")
    :limit(10)
    :get()

Basic Paginated Select Query

-- Get paginated results by specifying perPage and currentPage
local entriesPerPage = 10
local currentPage = 1

local obj = DB:table("users")
    :select("firstname", "lastname")
    :paginate(entriesPerPage, currentPage)

print(("Returned %d out of %d"):format(entriesPerPage, obj.totalCount))

Basic Count Query

-- Count the number of users active in the last hour
local count = DB:table("users")
    :where("last_seen", ">", os.time() - 3600)
    :count()

print(("Count: %d"):format(count))

Basic Insert Query

-- Insert a new user record
local insertId = DB:table("users"):insert({
    identifier = "char1:12345",
    firstname = "John",
    lastname = "Doe",
    accounts = { bank = 100, cash = 0, black_money = 0 }, -- Automatically parsed as JSON string
    group = "admin"
})

Basic Update Query

-- Update user group
local affectedRows = DB:table("users")
    :where("identifier", "char1:12345")
    :update({
        group = "moderator"
    })

Basic Delete Query

-- Delete user by identifier
local affectedRows = DB:table("users")
    :where("identifier", "char1:12345")
    :delete()
21 Likes

This thing is amazing man! I will contribute on github asap.

2 Likes

This is really nice, I love to see stuff like this

3 Likes

Very nice I love Laravel’s query builder.

3 Likes

Good fkn job!

3 Likes

Suddenly it popped into my mind, what if we add laravel’s model logic to fivem?

1 Like

Check the thread description, I plan on implementing something like that :smile:

I got too excited when the idea popped and want to share immediately lol sorry.

This is pretty cool. Months ago I tried working on something similar, although mine was inspired by Django. For different reasons I had to stop working on it, but I released the source code nonetheless if you would like to re-use anything from it. It was a heavy work in progress, and I feel like releasing it would help.

Not intending to hijack your thread btw, keep up the good work! :blush:

2 Likes

No biggie! :smile:

I’ve got a bunch of ideas I want to explore once I get some time off from work and university.

Honestly, this could turn into more of a “core”-style resource with adapters and helpers like:

  • Models & Collections
  • Batch processing support
  • Joins
  • Transactions
  • Maybe even migrations with Lua (still on the fence about whether they’re really needed)
  • A testing library
  • SQLite support (though I’d personally stick with oxmysql as the primary DB driver—SQLite is great performance-wise, but I’m not sure if it’s worth the extra effort here).

Still just brainstorming, but there’s definitely a lot of potential!

I’ve worked with both, but I definitely prefer Laravel over Django… It just feels a lot smoother to work with :slight_smile:

Really appreciate you sharing your work here! I’ll definitely check it out. Also, I love the relations idea—can’t believe I forgot about that :joy: . But yeah, it’s definitely going to be a part of this codebase too.

Thank You, and feel free to contribute if you’d like! Maybe this ends up being useful to a lot of ppl.

1 Like

Damn, this is awesome, can’t wait to see where this goes.

1 Like

Very nice, I like it

1 Like

Thank you, I appreciate it! :smiley:

Thanks man! :slight_smile: