ErrorsReads and writes · rule
data.read_in_loop
Query or get inside a for loop.
Area 6, Reads and writes; not declinable; the next step is usually the agent's.
type Title = Text { max_length: 80}entity Book { id: Uuid title: Title subtitle: Title? available: Bool}type BookNotFound = Failure { kind: FailureKind.not_found}type Count = Int { min: 0}action available_count(book_ids: List<Book.id>) fails BookNotFound -> Count { var mut count: Count = 0 for book_id in book_ids { var book: Book = attempt Book.get(book_id) if (book.available) { count = Count(count + 1) } } return count}
error Book.get reads once for every item of the loop
--> main.jadpo:23:34
|23 | var book: Book = attempt Book.get(book_id) | ^^^^^^^^ inside `for`
- reason
- A read inside a loop makes one database query per item. Read the whole set with one query, testing the field with
ina list of the values, before the loop. - next
- Read the records with one query using
inbefore the loop (agent) - rule
- data.
read_ in_ loop - help
- https://jadpo.
dev/docs/errors/data. read_ in_ loop
Markdown for agents: /docs/errors/data.read_in_loop.md.Built from Jadpo main at aee2e8f.