Database Access

Interact with data, execute queries, and run transactions.

CRUD Operations

Direct access via the db object.

  • db:get(coll, key) -> table|nil
    Retrieve a document by key.
  • db:insert(coll, doc) -> string (key)
    Insert a new document. Generates UUID if _key is missing.
  • db:update(coll, key, updates) -> bool
    Merge updates into existing document.
  • db:replace(coll, key, doc) -> bool
    Completely replace a document.
  • db:delete(coll, key) -> bool
    Delete a document.

SDBQL Queries

Execute complex queries using SDBQL (SoliDB Query Language).

local results = db:query("FOR u IN users FILTER u.age > @age RETURN u", {
  age = 18
})

for _, user in ipairs(results) do
  solidb.log("User: " .. user.name)
end

ACID Transactions

Perform atomic operations across multiple collections using db:transaction.

local success, err = db:transaction(function(tx)
  -- All helper methods (get, insert, etc) are available on 'tx'
  local sender = tx:get("accounts", "A")
  local receiver = tx:get("accounts", "B")

  if sender.balance < 100 then
    error("Insufficient funds") -- Will rollback automatically
  end

  tx:update("accounts", "A", { balance = sender.balance - 100 })
  tx:update("accounts", "B", { balance = receiver.balance + 100 })

  return "Transfer complete"
end)

if not success then
  solidb.log("Tx failed: " .. err)
end