# API Reference

URL: https://rivo.tools/docs/cfx-db-api-reference

`DB.insert(table, data, callback?)`

Insert a new record into the specified table

Examplelua

```lua
DB.insert('players', {
    identifier = license,
    name = name,
    money = 5000
}, function(id, err)
    print("Inserted:", id)
end)
```

`DB.select(table, options)`

Query records with WHERE, ORDER BY, LIMIT, and field selection

Examplelua

```lua
local players = DB.select('players', {
    where = {money = {operator = ">", value = 10000}},
    orderBy = {money = 'desc'},
    limit = 10,
    select = {'name', 'money'}
})
```

`DB.update(table, options, callback?)`

Update records matching the WHERE clause

Examplelua

```lua
DB.update('players', {
    where = {identifier = license},
    set = {money = 7500}
})
```

`DB.subscribe(table, options, callback)`

Subscribe to real-time data updates (automatic push on changes)

Examplelua

```lua
local unsub = DB.subscribe('players', {
    where = {online = true}
}, function(players)
    print("Online players:", #players)
end)
```

`DB.relations(table, options, callback)`

Query with relationships (JOIN-like functionality)

Examplelua

```lua
DB.relations('players', {
    where = {identifier = license},
    relations = {
        {name = 'vehicles', table = 'vehicles',
         foreignKey = 'owner', localKey = 'identifier', type = 'many'}
    }
})
```

`exports["cfx-db"]:executeTransaction(operations, callback)`

Execute multiple operations atomically (all succeed or all fail)

Examplelua

```lua
local ops = {
    {type = "update", table = "players", where = {id = 1}, data = {money = 400}},
    {type = "update", table = "players", where = {id = 2}, data = {money = 600}}
}
exports['cfx-db']:executeTransaction(ops, function(success, result)
    print("Transaction:", success)
end)
```
