Database.Rows
Database
Build and run queries over table rows.
Start from Table.access, optionally filter / join / orderBy / limit, then **map before select or selectAll**. Skipping map is a compile error (pipeline is missing map). Aggregates count and sum do not need map.
access todos user
|> filter (\todo -> todo.completed == False)
|> orderByDesc .createdAt
|> map (\todo -> { id = todo.id, title = todo.title })
|> selectAllmap projections must copy fields (row.field or a joined pair). Do not compute or nest values in map. Join example:
access comments token
|> join .articleId articles token
|> map (\(comment, article) -> { body = comment.body, title = article.title })
|> selectAllTypes
Rows
type Rows a = RowsA query over rows of type a.
Examples
access todos token
|> map (\todo -> { id = todo.id })
|> selectAllValues
map
map : (a -> b) -> Rows a -> Rows bProject each row. Required before select / selectAll. Copy fields only; do not call helpers or build nested records with computed values.
Examples
access todos token
|> map (\todo -> { id = todo.id, title = todo.title })
|> selectAllfilter
filter : (a -> Bool) -> Rows a -> Rows aKeep only rows that match a predicate (WHERE).
Examples
access todos token
|> filter (\todo -> todo.completed == False)
|> map (\todo -> { id = todo.id })
|> selectAlljoin
join : (a -> key) -> Table token b -> token -> Rows a -> Rows (a, b)Inner-join along a declared foreign key on the left table.
Examples
access comments token
|> join .articleId articles token
|> map (\(comment, article) -> { body = comment.body, title = article.title })
|> selectAllorderBy
orderBy : (a -> comparable) -> Rows a -> Rows aOrder rows ascending by a field (ORDER BY col). Call before map.
Examples
access todos token
|> orderBy .title
|> map (\todo -> { id = todo.id, title = todo.title })
|> selectAllorderByDesc
orderByDesc : (a -> comparable) -> Rows a -> Rows aOrder rows descending by a field (ORDER BY col DESC).
Examples
access todos token
|> orderByDesc .createdAt
|> map (\todo -> { id = todo.id })
|> selectAlllimit
limit : Int -> Rows a -> Rows aCap how many rows are returned (LIMIT n). Call before map.
Examples
access todos token
|> limit 20
|> map (\todo -> { id = todo.id })
|> selectAlloffset
offset : Int -> Rows a -> Rows aSkip the first n rows (OFFSET n). Call before map.
Examples
access todos token
|> offset 40
|> limit 20
|> map (\todo -> { id = todo.id })
|> selectAllselect
select : Rows a -> Transaction aSelect a single row. Requires a prior map. Fails the transaction when zero or many rows match (same idea as expecting one result).
Examples
access todos token
|> filter (\todo -> todo.id == id)
|> map (\todo -> { id = todo.id, title = todo.title })
|> selectselectAll
selectAll : Rows a -> Transaction (Rows a)Select every matching row. Requires a prior map.
Examples
access todos token
|> map (\todo -> { id = todo.id })
|> selectAllreduce
reduce : Reducer row a -> Rows row -> Transaction aRun a typed aggregate over a query (no map required).
Examples
access orders token
|> reduce (Database.Int64.sum .total)
access items token
|> reduce Reducer.counttoList
toList : Rows a -> List aMaterialized rows as a List (identity at runtime after select).
Examples
access todos token
|> map (\todo -> { id = todo.id })
|> selectAll
|> andThen (\rows -> succeed (toList rows))length
length : Rows a -> IntHow many rows a materialized query result contains.
Examples
access todos token
|> map (\todo -> { id = todo.id })
|> selectAll
|> andThen (\rows -> succeed (length rows))count
count : Rows a -> Transaction IntCount rows in a query (SELECT COUNT(*)). Ignores limit / offset. Does not need map.
Examples
access todos token
|> filter (\todo -> todo.completed == False)
|> countsum
sum : (a -> number) -> Rows a -> Transaction numberSum a numeric field (SELECT SUM(col)). The projection must be a field accessor (.total). Does not need map.
Examples
access orders token
|> sum .total