Queries and indexes
Start from a primary key or declare an index for the order and fields a query needs.
Read by primary key
export fn lookup(id: UInt8) -> Text? {
return Note.get(id)?.body;
}Note.get(id) returns an optional, deeply read-only row. Use Note.require(id) when a missing row should fail the call. For a compound primary key, pass a tuple containing every key component in declaration order.
Design a covering index
Imagine an inbox that lists note bodies for a book in ID order. Declare the equality filter first in the index key and the sort field second. Include the response fields in the index projection.
index by_book {
key: (book, id),
value: {body},
},The iterator yields one tuple per matching index entry. Its first projected field is entry.0, the body. Adding another projected field changes the tuple’s shape. Keep projections small when readers need just one field.
Bound a scan and resume it
export fn page(book: UInt8, after: UInt8, size: UInt8) -> List<Text> {
let mut bodies: List<Text> = [];
for entry in Note.by_book.scan(
prefix: (book),
start: (after) exclusive,
limit: size,
) {
bodies.append(entry.0);
}
return bodies;
}For page(7, 20, 5), prefix fixes the book to 7. The exclusive start compares the next key component, the note ID, to 20. The first result is the next larger ID in book 7. The scan returns up to five bodies; fewer entries are valid. An empty match returns an empty list.
| Option | Meaning |
|---|---|
prefix | Fix the leading key components. An omitted prefix scans the whole index. |
start, end | Set bounds on components after the prefix. Write inclusive or exclusive explicitly to specify whether the endpoint qualifies. |
direction | Default forward follows the declared index order, including desc components. reverse traverses the same bounds backwards. |
limit | A UInt8 count caps the number of results. Zero yields none. |
For a client-facing pagination cursor, also project the key fields and return the last one consumed. If secondary keys repeat, include the appended primary-key suffix in the continuation so an exclusive boundary can identify one exact entry.
Index expressions, collections, and enum variants
The search.dbl example shows three access patterns on one Article table. An expression can calculate a key from row fields. A collection index emits an entry for each element, so a repeated list element can match twice. A variant index includes only rows in its declared match arm.
table Article {
id: UInt8,
title: Text,
tags: List<Text, max: 16>,
state: State,
primary key(id),
index by_title_length {
key: (text.byte_length(title), id),
value: {title},
},
index by_tag {
for tag in tags {
key: (tag),
value: {id},
},
},
index by_publication {
match state {
Published(p) => {
key: (p.published_at desc),
value: {id},
},
},
},
}Each index adds work when the indexed row changes. With by_tag, a 16-item tags list can contribute 16 entries for one article. A query that only needs the article ID can read it directly from this index.