List all storage objects in a collection

Listing operations are paginated to make sure you always get back only the required set of data, rather than get everything back and have to filter out a lot you may not need. Data is returned in “pages” and you can use cursors to get the next page or listing results.

Any non-nil cursor indicates there are more results beyond the current page. A nil cursor indicates your pagination has reached the end.

I’ll assume you want to paginate storage listings in an RPC function, try this:

local nk = require("nakama")

local function list_all(context, payload)
  local all_results = {}
  local next_cursor = nil
  repeat
    local results, cursor = nk.storage_list(nil, "LevelPlays", 100, next_cursor)
    for _, result in ipairs(results) do
      table.insert(all_results, result)
    end
    next_cursor = cursor
  until (not next_cursor)
  -- all_results now contains all records owned by the root user
  -- and that belong to collection "LevelPlays"

  return nk.json_encode(all_results)
end
nk.register_rpc(list_all, "list_all")

This is an example RPC function that will list all available records and return them, but you can of course just use the records internally in the RPC function rather than simply return them to the client/caller.

3 Likes