Docs

Exports & events

Hook your own scripts into waw-sql: read its numbers, take a backup before a big change, or react the moment something goes wrong.

Server sideLuaJavaScript
Good to know

Every export below works from any server script, with or without the waw-sql library. The ones that wait for something have a callback form and an _async form: in Lua, call the _async form inside a thread and it returns the result; in JavaScript, await it.

Exports

exportwhat it gives you
resourceStats(name?)numbers for one script: queries, totalMs, avgMs, slow, errors and its slowest query. Leave the name out for every script. nil for a script that has not run a query yet
startupStats()the same numbers, counted only while the server was starting
insights()the open smart alerts, newest first
history(range)the saved history the panel shows: '24h', '7d' or '30d'. The same answer is given for 10 seconds
migrations(resource?, cb)the schema version and applied steps of one script, or of every script. Also migrations_async(resource?)
backupCreate(options?, cb)takes a backup and gives back its details. Also backupCreate_async(options?). Off until a server owner switches it on
backupList()every backup, newest first
stats()pool, cache and queue status, the same as MySQL.stats()

Calling an export

An export that answers at once is called like any other export. One that waits for something is called through its _async form: in Lua inside a thread, in JavaScript with await.

-- answers at once
local stats = exports['waw-sql']:resourceStats(GetCurrentResourceName())
if stats then print(('my queries: %d, %.1f ms on average'):format(stats.queries, stats.avgMs)) end

-- waits: the _async form, inside a thread
CreateThread(function()
    local info = exports['waw-sql']:migrations_async(GetCurrentResourceName())
    print(json.encode(info))
end)

Take a backup from your script

A good habit before your script changes many rows at once. Give it the tables you are about to touch, or nothing for the whole database.

-- a backup before your script changes many rows
exports['waw-sql']:backupCreate({ tables = { 'my_garage' }, note = 'before the garage update' }, function(backup, err)
    if err then return print('no backup: ' .. err) end
    print(('backup %s: %d rows'):format(backup.name, backup.rows))
end)
Off until the server owner allows it

Backups from scripts are switched off by default. A server owner turns them on in the panel under Backups → Backups from other scripts. While it is off, every request gets an error and the console names the script once.

  • Without tables the whole database is backed up, the same as the panel's backup button. The backup shows up in the panel's backup list with your script's name.
  • Only one backup or restore runs at a time, and scripts together can take at most one backup every 10 minutes. Otherwise the callback gets an error.
  • Of the backups taken by scripts, only the last 10 are kept. Backups from the panel or the schedule are never removed by this.

Events

waw-sql raises these server events. Players cannot send or receive them.

eventargumentswhen
waw-sql:readythe first connection to the database is open. A script that starts later should use MySQL.ready instead
waw-sql:connection-lost{ error, at }the database stopped answering
waw-sql:connection-restored{ seconds, at }it answers again, after seconds
waw-sql:alertkind, infoevery alert the Discord log would send, also when the Discord log is off. kind is one of the names in shared/webhook.lua (very-slow, error-spike, pool-full, db-down, backup-done, audit-flag, hot-loop...). The same waiting time applies (cooldownSeconds)
waw-sql:insightinsighta new smart alert opens: { id, kind, resource, sql, detail, occurrences, firstAt, lastAt }
waw-sql:audit-flagflagthe audit log flags a suspicious increase: { table, column, ident, pk, from, to, delta, at }
waw-sql:backup-donebackupa backup was taken, from the panel, the schedule or a script
waw-sql:backup-failed{ reason, by, error, at }a backup could not be taken
waw-sql:error{ query, parameters, message, err, resource }a query failed. Also sent as oxmysql:error
waw-sql:transaction-errorthe samea transaction was rolled back. Also sent as oxmysql:transaction-error

Who did something (actor in waw-sql:alert, by in the backup events and in backupList) is the admin's name and server ID, for example Arda (3), without their license or other identifiers. Identifiers inside an alert show only their type, such as license:hidden. The Discord log still shows them in full.

Old mysql-async scripts that wait for onMySQLReady or call exports['mysql-async']:is_ready() work too.

Listen safely

Any server script can raise an event with these names. Before you punish a player or change data because of one, check that waw-sql raised it. If waw-sql runs under another folder name, compare with that name.

AddEventHandler('waw-sql:audit-flag', function(flag)
    -- act only on the ones waw-sql raised
    if GetInvokingResource() ~= 'waw-sql' then return end
    print(('%s: %s.%s went from %d to %d'):format(flag.ident, flag.table, flag.column, flag.from, flag.to))
end)
Error events carry the query's values

waw-sql:error and waw-sql:transaction-error include the query's parameters, which can hold player identifiers, names or amounts. If you send them to Discord or another log, leave the parameters out or mask them.

What stays in the panel

Some things can't be undone, so only a person with the right permission can do them, from the panel: restoring a backup, deleting player data, rolling back the audit log and applying optimizer suggestions.

The panel's own exports (panelAction, the permission, Discord and advisor exports) are only for waw-sql itself. Another script that calls one gets nothing back and nothing happens; the console names the script once.