Collections, filters and totals
Create collections, read and write records, filter, count and total, show records at set times, and rank them by a number.
Collections
You create collections, each with its safety rule; the SDK never does. Locally, use
npx gemmein collection add <name> or the terminal running npx gemmein dev. On Gemmein, use the
dashboard's Data (one click), or carry them there with npx gemmein sync.
Names are lowercase letters, numbers, and underscores only (saved_games, never
savedGames), and a bad name throws synchronously.
const tasks = g.collection("tasks")
const task = await tasks.create({ title: "Buy milk", done: false })
console.log(task.id, task.data.title) // fields live under .data
const { records } = await tasks.list() // { records, cursor, hasMore }
const recent = await tasks.list({ limit: 10, sort: "newest" })
const filtered = await tasks.list({ where: { done: false } }) // a filter — see below
const one = await tasks.get("rec_abc123")
await tasks.update("rec_abc123", { done: true })
await tasks.delete("rec_abc123")
Your fields live under data. Everything else on a record is set by the server
and read-only:
| Field | What it is |
|---|---|
id | The record's id |
createdAt / updatedAt | When it was created and last changed |
ownerUserId | The person who wrote it; null for the app's own records (receipts, records added on the dashboard without a person) |
collectionId | The id of the collection it is in (the collection's own id, not its name) |
appId | The id of your app |
environmentId | The id of the environment it lives in, Development or Live |
version | Goes up by 1 on every update; pass it as ifVersion to refuse a stale save |
published | false on a draft (public rules only) |
key | The create-if-absent key it was created with, when one was used |
audienceUserId / recipients | Who an addressed or direct record is for; recipients lists everyone a group message names |
writtenBy | "server" when your server's secret key wrote it |
mine | Your own marks on its open fields (below); only you see it |
mineRetired | For a "one per person of" field whose choice you hold but that was taken off its list: true. mine still says the choice; show it as retired, and let the person choose again or withdraw it |
flaggedBy | Who set each "per person flag" field (the first 100). Only on one record read by your dashboard or a secret key |
expand | The linked records you asked for with expand |
existing | true when a keyed create returned the record you already made |
showFrom / showUntil | When the record shows to other readers, when it was set with show and hide times |
rank | Its place on a ranked list: 1 + how many records the same list holds with a better value; ties share |
Filters, counts and totals
A where names up to 5 of your data fields (link fields too), and every field
must match. A field's value is a literal, an exact match, or an object of operators, 1 to 3
per field, every one of which must hold:
| Operator | Matches when | Value |
|---|---|---|
eq / ne | The stored value equals / does not equal it, compared as text: { eq: "10" } matches the number 10, as a literal always has. Null-safe: { eq: null } matches a field that is absent or null, { ne: null } one that holds a value; a bare null literal is refused | string, number, boolean or null |
gt gte lt lte | The stored value is greater / at least / less / at most. A number compares as a number, and a text value in that field never matches; a string compares as text in code-unit order (uppercase before lowercase), so an ISO date orders correctly | number, or a string of at most 200 characters |
in / nin | The stored value is one of / none of the set | 1 to 50 strings, numbers or booleans |
contains / startsWith | The stored text holds / begins with it, case-insensitive (ASCII letters fold; other scripts compare as stored) | text of 1 to 200 characters |
exists | true: the field is present and not null. false: absent or null | boolean |
await orders.list({ where: { amount: { gte: 10 } } })
await orders.list({ where: { status: { in: ["paid", "sent"] } } })
await orders.list({ where: { title: { contains: "invoice" } } })
await orders.list({ where: { dueAt: { lt: "2026-10-01" } } })
const open = await orders.count({ where: { status: "open" } }) // a number
const paid = await orders.stats("amount", { where: { status: "paid" } }) // { count, sum, avg, min, max }
The same grammar serves list, watch, count and
stats, and a relay's when and where. Server-managed
fields are refused. A where outside the
grammar is 400 invalid_filter, and the message names the field and the
operator.
In a live collection with fixed fields, a field's default is
read by eq, ne, in and nin; the ordered
operators, contains, startsWith and exists read the
stored value only.
count({ where?, search? })answers how many records this person could list, under the same safety rule, scope and filters, without paging them.list({ count: true })adds the total to a page:total, exact up to 10,000, or past thattotalAtLeast: 10000(show "10,000+"). It counts under the same safety rule, filters and search as the page, reads at most 10,001 records, rides the first page only (a page fetched withcursorcarries none), and is refused on asincedelta (400 invalid_count).list({ author: "me" })orlist({ author: userId })lists one person's records onshared,community,public_readanddirect, inside the reader's own scope, so it only ever returns fewer records. Onprivate,addressedandadmin_writeit is403 author_not_visible.ownerUserIdstays refused inwhere;authoris the filter for it.stats(field, { where?, search? })reduces one numeric field over the same records.countis how many held a number there (a record whose field is missing or not a number is skipped).sum,avg,minandmaxarenullwhen none did. Asumpast 253 is answered as a string.- Neither pages its results, so
since,sort,cursor,limitandexpandare refused (400 invalid_count/400 invalid_stats). - A
statsfield name starts with a letter and uses only letters, digits and underscores; anything else is400 invalid_stats. - Both share
list's read rate limit, and each call counts as one read for a secret key.
The grammar has no or group, and the only sort by a data field is a
rankable number; search is a case-insensitive substring
match over stored text; stats reduces one field per call.
Show and hide times
A record can wait to appear, or disappear on its own: a product drop, a scheduled
announcement, a story that is gone after a day. Set showFrom and
showUntil as options on create or update, never as data
fields. They sit on the record beside published.
await drops.create({ title: "Winter box" }, { showFrom: "2026-11-01T09:00:00Z" })
await stories.create({ text: "Open late tonight" }, { showUntil: "2026-10-18T23:00:00Z" })
await drops.update(id, {}, { showUntil: new Date() }) // take it down now
await drops.update(id, {}, { showFrom: null }) // clear a bound
- Before
showFrom, and fromshowUntilon, the record is hidden from every reader except its author. Your dashboard and your server always see it. It is hidden in every read:list,get,count,stats,search,expand, open fields andwatch. - Gemmein's clock decides, never the browser's, so don't filter by time in the app. Nothing
needs to be written when the time comes: a watcher receives the record when its
showFromarrives, and its id indeletedwhen itsshowUntilpasses. - It works on
shared,community,public_read,admin_write,addressed(a scheduled inbox message reaches its recipient atshowFrom) anddirect. Onprivateandapp_onlynobody else reads the record, so a time there is refused. - Each time is an ISO 8601 instant with a zone, such as
2026-11-01T09:00:00Z, between 1970 and 9999, andshowUntilmust be later thanshowFrom. Anything else, ornullon a create, is400 invalid_show_timeand the message names which part. - An option only ever hides. It never lets anyone read a record the safety rule keeps from them.
Ranked lists
A leaderboard, a top-ten, the fastest times: name up to 3 number fields as
rankable in the collection's file, and the list can be ordered by one of them.
// gemmein/collections/scores.json
{ "rule": "admin_write", "rankable": ["score"] }
const { records } = await g.collection("scores").list({ sort: { by: "score" }, limit: 10 })
records[0].rank // 1 — highest first; order: "asc" puts the lowest first
- Each record on a ranked list carries
rank: 1 + how many records the same list holds with a better value. Ties share a place: 1, 2, 2, 4. - It ranks only what this person may already read under the safety rule, with the same
where,search,countandcursor. Awherefor this week ranks this week. Onprivate, each person ranks only their own records. - A record with no number in that field, and no default, isn't on the ranked list, and
count: truecounts only ranked records. - On
community,sharedanddirect, each rankable field must be a declared number field. More than 3 fields, a field named twice, or a name that isn't a data field is400 invalid_rankablewhen the file is read bygemmein devornpx gemmein sync. - Sorting by a field that isn't declared rankable is
400 not_rankable.sorttakes only"newest","oldest","updated"or{ by, order }, andorderonly"desc"or"asc"; anything else is400 invalid_sort. A ranked list can't usesince(400 invalid_since).
Trust comes from who writes the number. Keep scores in an
admin_write or public_read collection, or app_only read by
your server, written by your server or a relay after it checked the result. A number each
player writes into their own record can be anything they send.
Thanks for telling us.
Thanks for telling us.
Last updated