Records / Reading
Reading
store.events() reads every stream; store.events('audit') reads one. A
query finds events by time, stream, name, level, trace, a field's value or
text anywhere in them, a page at a time. The same query counts them, finds a
key's most frequent values, and aggregates them by span and key.
const page = await store.events().list({ since: '1h', minLevel: 'warn' }) // { events, next }
const more = await store.events().list({ since: '1h', minLevel: 'warn', after: page.next })
for await (const event of store.events().all({ since: '24h', search: 'connection reset' })) show(event)
const mine = await audit.list({ where: { user: 42 }, newest: true, limit: 50 })page = await store.events().list(since="1h", min_level="warn") # EventPage(events, next)
more = await store.events().list(since="1h", min_level="warn", after=page.next)
async for event in store.events().all(since="24h", search="connection reset"):
show(event)
mine = await audit.list(where={"user": 42}, newest=True, limit=50)events, err := records.OpenAllEvents(store)
page, err := events.List(ctx, records.Query{Since: time.Hour, MinLevel: slog.LevelWarn})
more, err := events.List(ctx, records.Query{Since: time.Hour, MinLevel: slog.LevelWarn, After: page.Next})
for event, err := range events.All(ctx, records.Query{Since: 24 * time.Hour, Search: "connection reset"}) {
show(event, err)
}
mine, err := audit.List(ctx, records.Query{Where: records.Where{"user": 42}, Newest: true, Limit: 50})There is no read: list reads a page, all walks every page.
One event, every way to find it
A webhook that arrived from GitHub for the target home:
await store.events('webhooks').append('github', { requestId: 'r-81', status: 200 }, { context: { target: 'home' } })await store.events("webhooks").append("github", {"requestId": "r-81", "status": 200}, context={"target": "home"})webhooks, err := records.OpenEvents(store, "webhooks")
_, err = webhooks.Append(ctx, "github", records.Fields{"requestId": "r-81", "status": 200},
records.Context(records.Fields{"target": "home"}))is read back as:
{
"at": "2026-10-21T20:24:49.322Z",
"stream": "webhooks",
"name": "github",
"fields": { "requestId": "r-81", "status": 200 },
"context": { "target": "home" }
}| Finds it | Finds nothing |
|---|---|
streams: ['webhooks'] | streams: ['github'] |
names: ['github'] | |
where: { requestId: 'r-81' } | |
where: { target: 'home' } | |
where: { status: 200 } | where: { status: '200' } |
search: 'r-81' |
github is the event's name, not its stream. where finds a key of
context as it finds one of fields, and a value by the JSON it was written
as, a number as a number.
A query
| In a query | Finds |
|---|---|
since | the span before now, '1h'; or from and to |
from, to | the range's start, included, and its end, excluded |
streams | any of these streams; store.events()'s alone |
names | any of these names: ['log'] is the lines |
minLevel | this level and worse; an event without a level is not found |
traceId | one trace's events, every stream |
where | each key with a value, one of a list, { not } or { prefix } |
search | this text in the message, the name or any value, its case ignored |
newest | newest first; oldest first without it |
limit | the events of a page: 1,000, and 10,000 at most |
after | a page's next, with the rest of the query as it was |
- Every condition given holds. Events come in the order of their times. Events with the same time says how events that share a time are ordered.
wherefinds a key at the top offieldsorcontext, by the JSON its value was written as:42finds42, and neither'42'nor42.0. A key written twice, as another program's line may write it, is found by its last value, the one the event reads back with.- A list in
wheremeans any of its values,{ not }none of them, and{ prefix }a text that starts with it: A where. A database's statement means any value in its one column: A where kept to a database's statement. searchfinds a phrase in the message, the name, and a value at any depth. It does not look at keys;fieldNameslists those.- A condition given as
undefinedisinvalid, never left out: a trace id that was not there must not read every event. So is a field no query has, in TypeScript and Python, such asnamewritten fornames: the error names the field, and the one it was likely meant to be. Go and Rust refuse it when the program compiles.
The time, the stream, the level, the trace and a where on a value that
rarely repeats, a request's id, skip the blocks that cannot match, as
Reading only what can
match
explains. search reads every event the rest of the query leaves, so give it
a range.
A where
await api.aggregate({ names: ['call'], since: '1d', by: ['op'], where: { op: ['grading.works', 'drafts.get'] } })
await api.list({ since: '1d', where: { userId: [7, 12, 31], route: { prefix: '/admin' }, env: { not: 'test' } } })
await api.list({ since: '1d', where: { tags: [['a', 'b']] } }) // the list ['a', 'b'] itselfawait api.aggregate(names=["call"], since="1d", by=["op"], where={"op": ["grading.works", "drafts.get"]})
await api.list(since="1d", where={"userId": [7, 12, 31], "route": {"prefix": "/admin"}, "env": {"not": "test"}})
await api.list(since="1d", where={"tags": [["a", "b"]]}) # the list ["a", "b"] itselfapi.Aggregate(ctx, records.Query{Names: []string{"call"}, Since: 24 * time.Hour, By: []string{"op"},
Where: records.Where{"op": records.OneOf("grading.works", "drafts.get")}})
api.List(ctx, records.Query{Since: 24 * time.Hour,
Where: records.Where{"userId": []int{7, 12, 31}, "route": records.Prefix("/admin"), "env": records.Not("test")}})
api.List(ctx, records.Query{Since: 24 * time.Hour, Where: records.Where{"tags": [][]string{{"a", "b"}}}})use tinystore::{Not, Prefix};
let day = Duration::from_hours(24);
api.list().since(day).filter("op", &["grading.works", "drafts.get"]).aggregate(&Aggregate::new().by(["op"]))?;
api.list().since(day).filter("userId", &[7, 12, 31]).filter("route", Prefix("/admin")).filter("env", Not("test")).page()?;
api.list().since(day).filter("tags", &[["a", "b"]]).page()?; // the list ["a", "b"] itself| A key's condition | Finds an event whose value there is |
|---|---|
a value: 'drafts.get' | that value, by the JSON it was written as |
a list: [7, 12, 31] | any of them, 1 to 1,000 values |
{ not: 'test' } | none of them, a value or a list, or an event without key |
{ prefix: '/admin' } | a text that starts with it; a number or a bool never does |
a list in a list: [[…]] | that list, or that object, itself |
- Go writes
records.OneOf(…),records.Not(…)andrecords.Prefix(…), and a bare slice means the same asOneOf. Rust writes&[…]for a list andtinystore::Not(…)andtinystore::Prefix(…), the words metrics' conditions use. - Each item matches by its exact JSON, as a single value does:
[42, '42']finds42and'42', and not42.0. - An empty list, a prefix of nothing, and any other object are
invalid. A list or an object as the value itself goes inside a list. - Each value of a list, and a prefix, is at most 4 KiB.
{ not }reads more than the other conditions, since a block without the key may hold what it finds: give it a range or another condition.
A where kept to a database's statement
For example, you want to know what teachers use: the events of the people whose id is in the teachers' table. The store counts them itself, so your program doesn't fetch the events of everybody and then filter out the teachers.
const teachers = db.statement`select id from teachers` // bound to its database; nothing runs yet
await events.count({ since: '1d', where: { userId: teachers } })
await events.aggregate({ since: '7d', by: ['name'], distinct: ['userId'], where: { userId: teachers } })
await events.list({ since: '1d', where: { userId: { not: teachers } } })teachers = db.statement("select id from teachers") # bound to its database; nothing runs yet
await events.count(since="1d", where={"userId": teachers})
await events.aggregate(since="7d", by=["name"], distinct=["userId"], where={"userId": teachers})
await events.list(since="1d", where={"userId": {"not": teachers}})teachers := db.Statement("select id from teachers") // bound to its database; nothing runs yet
events.Count(ctx, records.Query{Since: 24 * time.Hour, Where: records.Where{"userId": teachers}})
events.Aggregate(ctx, records.Query{Since: 7 * 24 * time.Hour, By: []string{"name"}, Distinct: []string{"userId"},
Where: records.Where{"userId": teachers}})
events.List(ctx, records.Query{Since: 24 * time.Hour, Where: records.Where{"userId": records.Not(teachers)}})let (teachers, day) = (db.statement("select id from teachers"), Duration::from_hours(24)); // nothing runs yet
events.list().since(day).filter("userId", &teachers).count()?;
events.list().since(day * 7).filter("userId", &teachers).aggregate(&Aggregate::new().by(["name"]).distinct(["userId"]))?;
events.list().since(day).filter("userId", Not(&teachers)).page()?;-
The store runs the statement once, when the query starts, on a reader of its database. The values of its one column work as a list: the key must equal one of them.
{ not }matches events whose key equals none of them, or that don't have the key. A page that you ask for later withafterruns the statement again. -
The statement and the events aren't read at the same moment. The events are in records.db and the rows are in the database's file, and the store doesn't promise one moment across two files.
-
Its values match the way a list's values do, by the JSON that the event's value was written as:
The statement returns Finds the integer 771or0also trueorfalsethe text '7'"7", and never7the real 7.07.0, and not7;0.5finds0.5nullnothing a blob refused with invalid: select a text -
The statement can return 100,000 rows at most, and each value can be 4 KiB at most as JSON, the same as for a list. Past these limits the query fails with
limit. It also fails withlimitwhen the store's memory can't hold the values while the query runs. A statement that writes, or that returns more or fewer than one column, fails withinvalid. -
count,list,top,largest,aggregateanddeleteaccept a statement. A delete runs it once, when it starts, and uses the values for every segment it changes. A tail keeps running, so it refuses a statement withinvalid. -
A stream lives in records.db and doesn't belong to any database, so the statement must say which database it reads. That is why a bare
sql`…`in a stream'swhereisinvalid. A table's index accepts one, because an index lives in its database. A statement of another store's database isinvalidtoo. -
If the statement returns more than 100 values, the read doesn't use the blocks' filters to skip blocks. A filter answers "maybe" for about one value in a hundred that the block doesn't contain, so with that many values it would skip almost no block. The read still looks up each event's value by its hash, once per event.
Pages
list gives one page, and all every page in turn. A page holds its
limit of events that match, or a few fewer where the next ones share a
time, which a page never splits. Only the last page holds fewer and has no
next. To fill a page, list reads past the events that match nothing:
mine above, one user's last 50 events, comes back in one call however many
other users' events lie between them. So a page of a rare value reads as much
of the range as count would, and skips blocks the same way. since is
counted once: a page asked with after reads the range its first page had.
Events with the same time
The events of one append that give no at take the time of the call, 100
nanoseconds apart in the order you list them: the first takes the call's
time, the next 100 ns more, and so on. 100 ns is the finest step a clock
keeps on every platform, Windows included, so each event reads back with the
time it was given. An event that gives its own at keeps it. So a list reads
back in the order you appended it, and a page can end between any two of its
events, however many there are:
await audit.append([{ name: 'cart.created' }, { name: 'item.added' }, { name: 'checkout' }])
const page = await audit.list({ since: '1m' }) // cart.created, item.added, checkoutawait audit.append([Event("cart.created"), Event("item.added"), Event("checkout")])
page = await audit.list(since="1m") # cart.created, item.added, checkoutEvents still share a time to the nanosecond when you give them the same
at, or when two appends that run at the same time give theirs the same
times.
- Events that share a time come in the order they were appended. This holds
across appends and across streams, for late events
too, and before and after TinyStore packs and merges them. With
newest, they come in the reverse order. - Events of one time from two appends that run at the same time come in the order the store wrote them, and every read gives that same order.
- A page never splits a time, so more events of one time than its
limitfail the page with alimiterror. Give such events times apart, or ask for a largerlimit. - Events that TinyStore v0.1.0 packed are the exception. They keep the order that v0.1.0 gave them, the same on every read, which is not always the order they were appended (contract NOT-12): in one stream a late event may come before or after the stream's other events of its time, and events of different streams come in the order of the blocks they were packed in. Events appended after you upgrade come after them, in the order they were appended.
Which events are late
TinyStore packs late events apart from the others, so that they don't force it to rewrite what it packed, as How logs and metrics pack explains. A read still gives them in the order they were appended, and a tail as they were written.
An event is late when its at is more than ten seconds behind the store's
clock, and also more than ten seconds behind the newest on-time event that
its stream had before it. These on-time events count as before it:
- the events earlier in the same
append - the events of the stream's earlier appends, until packing or a
dropleaves none of the stream's on-time events waiting to be packed. Packing that leaves some of them waiting doesn't reset this.
When the store opens, the earlier events are the stream's on-time events that
still wait to be packed, whatever appended them, and the ones already packed
stop counting. The same happens when a node becomes the home of records.db.
If an append runs at the same time as another append to its stream, or while
TinyStore packs or drops events of the stream, either may count as the earlier
one. Until the store opens again, an append that fails counts as none.
So an event appended without at is never late.
Count, top and aggregate
const errors = await store.events().count({ since: '1h', minLevel: 'error' }) // 42
const routes = await store.events().top('route', { since: '1h', minLevel: 'error' })
// [{ value: '/notes', count: 12 }, { value: '/login', count: 7 }, …]
const hourly = await store.events().aggregate({ since: '24h', every: '1h', by: ['level'] })
// [{ at, count: 3, by: { level: 'error' } }, { at, count: 41, by: { level: 'warn' } }, …]
const latency = await api.aggregate({ since: '24h', names: ['request'], every: '5m', stats: ['duration'] })
// [{ at, count: 310, stats: { duration: { samples: 287, sum: 12772, min: 3, max: 1200, avg: 44.5 } } }, …]errors = await store.events().count(since="1h", min_level="error")
routes = await store.events().top("route", since="1h", min_level="error") # [Top(value, count), …]
hourly = await store.events().aggregate(since="24h", every="1h", by=["level"])
latency = await api.aggregate(since="24h", names=["request"], every="5m", stats=["duration"])errors, err := events.Count(ctx, records.Query{Since: time.Hour, MinLevel: slog.LevelError})
routes, err := events.Top(ctx, "route", records.Query{Since: time.Hour, MinLevel: slog.LevelError})
hourly, err := events.Aggregate(ctx, records.Query{Since: 24 * time.Hour, Every: time.Hour, By: []string{"level"}})let errors = store.all_events()?.list().since(Duration::from_hours(1)).min_level(Level::ERROR).count()?;
let routes = store.all_events()?.list().since(Duration::from_hours(1)).top("route")?;
let hourly = store
.all_events()?
.list()
.since(Duration::from_hours(24))
.aggregate(&Aggregate::new().every(Duration::from_hours(1)).by(["level"]))?;- Each takes a query as
listdoes, withoutnewest,limitandafter. - When few events match, as with a search for a rare word, they read several
pages at once on several threads. In Rust,
alldoes the same. topgives a key's most frequent values, ten unlesslimitasks for more, 1,000 at most. The key is a field's or a context's, orstream,name,levelormessage.messageis the event's message intop,byanddistinct. An event without one gives a field or a context key namedmessageinstead.calendarwithtimeZone(time_zonein Python,CalendarandTimeZonein Go,.calendar(Calendar::Day, zone)in Rust) gives a row for each local day, week from Monday or month in place ofevery, as metrics' days do.aggregategives a row for each span ofeveryand each value of the keys inby, with itscount. A span without events has no row, unless the aggregate asks forfill.atis where a row's span begins, counted from 1970 in UTC. A row of a calendar also hasdate, the local date its span begins on.statsnames fields, and a row gives each one'ssamples,sum,min,maxandavgover the events whose value there is a number.distinctgives how many different values a field has.- A sum of integers is exact, a
bigintpast 2⁵³. - An answer is whole or an error: past 10,000 rows, or 64 MiB of different
values held for
topordistinct, it islimit, never a smaller answer. Ask for a shorter range or a widerevery.
Every span, and its local date
A chart wants a row for every hour, and a calendar's day by its own date.
fill gives a row for every span of the range, an empty one with count: 0,
and a calendar's row carries date:
const days = await api.aggregate({ names: ['call'], since: '30d', calendar: 'day', timeZone: 'Europe/Moscow', fill: true })
// [{ at, date: '2026-10-21', count: 0, by: {}, stats: {}, distinct: {} }, …, { at, date: '2026-11-19', count: 14, … }]days = await api.aggregate(names=["call"], since="30d", calendar="day", time_zone="Europe/Moscow", fill=True)
# [Row(at=…, count=0, …, date=datetime.date(2026, 10, 21)), …]- The range is what the stream keeps of it: nothing is filled before its
keep, nor past now when the query gives noto. The spans that cut the range at either end get their rows too. fillneeds a start,sinceorfrom. Without one it isinvalid.- With
by, every span gets a row for each value of the keys that the answer holds. A filled row hascount: 0, nostats, and 0 for eachdistinct. Withbyand no events at all, there is no row to fill. - Filled rows count toward the 10,000 rows of an aggregate, and more spans than that are refused before anything is read.
dateisYYYY-MM-DD: a day's own date, a week's Monday, a month's first. It is adatetime.datein Python and text elsewhere. Rows ofeveryhave none, since a span of UTC needs no time zone.- A server of a release before
fillrefuses it asunimplemented.
The largest numbers
top('ms') counts how often each value occurs. largest gives the events
themselves, those of a field's largest numbers first, such as the slowest page
opens with their path:
const slowest = await pages.largest('ms', { names: ['page-open'], since: '7d', limit: 20 }) // Event[]slowest = await pages.largest("ms", names=["page-open"], since="7d", limit=20) # list[Event]slowest, err := pages.Largest(ctx, "ms", records.Query{Names: []string{"page-open"}, Since: 7 * 24 * time.Hour, Limit: 20})let slowest = pages.list().names(["page-open"]).since(Duration::from_hours(7 * 24)).limit(20).largest("ms")?; // Vec<Event>- The key is a field's, else a context's. An event without a number there is passed over.
- Events of one number come in the order
listgives them. An integer is compared with a float exactly, past 2⁵³ too. limitgives how many: ten unless it says, 1,000 at most. It takes a query astopdoes, withoutnewestandafter, and the answer comes whole.- It reads every event the query finds, as
topdoes, and holds only the events it gives, out of the store's memory budget. Past the budget the answer islimit, never a shorter list.
Sessions
For example, you want to see what newcomers did on their first visit. This
call returns the first session of each student who appeared since from,
with up to 50 of its events. Here a session is a run of one person's events,
each within 30 minutes of the one before.
const visits = await events.sessions({
since: '30d', by: 'userId', gap: '30m', max: '4h', firstSessions: 1, maxEvents: 50,
where: { userId: db.statement`select id from students where first_seen_at > ${from}` },
}) // Session[]: [{ by: { userId: 1 }, from, to, count: 2, events: [{ at, name, fields… }, …], partial }]visits = await events.sessions(
since="30d", by="userId", gap="30m", max="4h", first_sessions=1, max_events=50,
where={"userId": db.statement("select id from students where first_seen_at > ?", start)},
) # list[Session]: Session(by={"userId": 1}, from_=…, to=…, count=2, events=[Event(…), …], partial=False)newcomers := db.Statement("select id from students where first_seen_at > ?", from)
visits, err := events.Sessions(ctx, records.Query{Since: 30 * 24 * time.Hour, By: []string{"userId"},
Gap: 30 * time.Minute, Max: 4 * time.Hour, FirstSessions: 1, MaxEvents: 50, Where: records.Where{"userId": newcomers}})use tinystore::records::Sessions;
let newcomers = db.statement(tinystore::sql!("select id from students where first_seen_at > ?", from));
let visit = Sessions::by(["userId"]).gap(minute * 30).max(minute * 240).first_sessions(1).max_events(50);
let visits = events.list().since(day * 30).filter("userId", &newcomers).sessions(&visit)?; // Vec<Session>| A session's | Is |
|---|---|
by | the value of each key, null for events without it |
from, to | the time of its first event and of its last |
count | the number of all its events, even if it keeps fewer |
events | its first maxEvents events, as list returns them; none unless you ask |
partial | true when the session may go on outside the range that was read |
byaccepts keys the wayaggregatedoes: a field, or else a context key, orstream,name,levelormessage. A session contains the events that have one value of each key. They are in time order, and events with the same time are in arrival order. A session spans streams, and includes events that are packed and events that are still waiting.- A pause longer than
gapstarts the next session.maxends a session when that much time has passed since its first event, with or without a pause, and the next event starts a new session. firstSessionskeeps only the first sessions of each value in the range. Once a value has them, the read skips its later events.- A session that the start or the end of the range cuts is still returned,
with
partialset. This happens when its first event lies withingapof the start, or its last event lies withingapof the end. If the range has no end, the end is now. - It reads the range once and holds one open session per value of
by. The answer comes whole, sorted by the sessions' first events and then by theirby. It contains 10,000 sessions at most, open ones included, and 64 MiB of what they keep. Past either limit the call fails withlimit, and never returns fewer sessions. It isn't paged yet. To stay within the limits, use a shorter range,firstSessionsor awhere. maxEventsis 1,000 per session at most.sessionsaccepts the same query ascount, exceptnewest,limitandafter.- Not built yet: a funnel over sessions, and a link from a metric's sample to an event.
What a store holds
const streams = await store.events().streams() // [{ name: 'api', keep: '14d', retainedFrom, newest }, …]
const keys = await store.events('api').fieldNames({ since: '1h' }) // ['ms', 'requestId', 'route', 'user']streams, err := events.Streams(ctx)
keys, err := api.FieldNames(ctx, records.Query{Since: time.Hour})let streams = store.all_events()?.streams()?;
let keys = api.list().since(Duration::from_hours(1)).field_names()?;streams lists every stream with events, its keep, where what it keeps
begins, and its newest event. fieldNames gives the keys events hold, which a
page's filter menu offers. A key's values are top's, and a stream's names
top('name')'s.
What a stream's events carry
fields says which names a stream's events have and which keys their fields
hold, with how many events carry each and what kinds of value it holds. So a
program can draw a chart of a stream it has never seen, such as orders paid
per day summed by amount.
const orders = await store.events().fields('orders')
// eventNames: [{ name: 'order.paid', count: 100 }]
// fields: [{ name: 'amount', kinds: ['float'], count: 100 },
// { name: 'currency', kinds: ['text'], count: 100 },
// { name: 'order_id', kinds: ['integer'], count: 100 },
// { name: 'paid', kinds: ['bool'], count: 100 }]
// context: []orders = await store.events().fields("orders")
# event_names: [NameCount("order.paid", 100)]
# fields: [FieldInfo("amount", ["float"], 100), FieldInfo("currency", ["text"], 100),
# FieldInfo("order_id", ["integer"], 100), FieldInfo("paid", ["bool"], 100)]
# context: []orders, err := events.Fields(ctx, "orders")
// EventNames: [{Name: "order.paid", Count: 100}]
// Fields: [{Name: "amount", Kinds: [KindFloat], Count: 100}, {Name: "currency", Kinds: [KindText], Count: 100},
// {Name: "order_id", Kinds: [KindInteger], Count: 100}, {Name: "paid", Kinds: [KindBool], Count: 100}]let orders = store.all_events()?.fields("orders")?;
// event_names: [NameCount { name: "order.paid", count: 100 }]
// fields: [FieldInfo { name: "amount", kinds: [Kind::Float], count: 100 },
// FieldInfo { name: "currency", kinds: [Kind::Text], count: 100 }, …]eventNames / event_names / EventNames / event_nameslists the names of the stream's events, each with how many events have it.kindslists each kind of value the key holds, in this order:float,integer,text,bool,object,listandnull.floatis a number with a fraction or an exponent, such as12.5, andintegeris a whole number, such as42. A key that holds12in one event and12.5in another has both.countis how many events carry the key. An event that writes a key twice counts once.contextlists the keys of the events' contexts in the same way, apart from their fields.- The counts include every event the stream holds, the packed ones and the
ones that wait to be packed. When
keepremoves old events, their counts go with them. - A stream without events carries nothing, and a name that no stream can have fails with an invalid error.
TinyStore counts a segment's events when it packs them and keeps the counts
beside the segment, so a stream's packed events cost one small query
however many there are. The events that wait to be packed, at most an hour's
worth, are read from their rows, so fields takes longer on a stream that
has many events waiting.
See also
- Records: the engine's overview.
- Traces: one request's events across streams.
- Tail and poll: events as they arrive.
tinystore logs: the same lines from a shell, beside the program or server that holds the directory.