Skip to main content

DB

The db module is available on the server. It builds small model objects around oxmysql async exports and adds console commands for pushing Lua schemas to SQL tables or pulling SQL tables back into Lua schema files.
The module calls exports.oxmysql:query_async, insert_async, update_async, and execute_async. Start oxmysql before resources that use the db module.

Create a model

string
required
Logical model name. If tableName is not supplied, this is also the SQL table name.
string
Optional SQL table override.
table
Optional schema field map or ordered field array used by the console push command.

Query helpers

update and delete require a non-empty where table. sure_lib returns nil and logs an error instead of running broad updates or deletes without a filter.

Schema files

Create schema files under db/<schemaName>.lua in the resource that owns the database models.
db/users.lua

Field types

Field options include length, nullable = false, default, autoIncrement = true, unique = true, and primaryKey = true.

Console commands

The command name is prefixed with the current resource name. For sure_lib, the command is sure_lib:db.
Loads db/users.lua and runs CREATE TABLE IF NOT EXISTS.
DB console commands are console-only. If a player source tries to run them, sure_lib logs [sure_lib][db] db commands are console-only.