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 })
    |> selectAll

map 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 })
    |> selectAll

Types

Rows

type Rows a = Rows

A query over rows of type a.

Examples

access todos token
    |> map (\todo -> { id = todo.id })
    |> selectAll

Values

map

map : (a -> b) -> Rows a -> Rows b

Project 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 })
    |> selectAll

filter

filter : (a -> Bool) -> Rows a -> Rows a

Keep only rows that match a predicate (WHERE).

Examples

access todos token
    |> filter (\todo -> todo.completed == False)
    |> map (\todo -> { id = todo.id })
    |> selectAll

join

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 })
    |> selectAll

orderBy

orderBy : (a -> comparable) -> Rows a -> Rows a

Order 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 })
    |> selectAll

orderByDesc

orderByDesc : (a -> comparable) -> Rows a -> Rows a

Order rows descending by a field (ORDER BY col DESC).

Examples

access todos token
    |> orderByDesc .createdAt
    |> map (\todo -> { id = todo.id })
    |> selectAll

limit

limit : Int -> Rows a -> Rows a

Cap how many rows are returned (LIMIT n). Call before map.

Examples

access todos token
    |> limit 20
    |> map (\todo -> { id = todo.id })
    |> selectAll

offset

offset : Int -> Rows a -> Rows a

Skip the first n rows (OFFSET n). Call before map.

Examples

access todos token
    |> offset 40
    |> limit 20
    |> map (\todo -> { id = todo.id })
    |> selectAll

select

select : Rows a -> Transaction a

Select 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 })
    |> select

selectAll

selectAll : Rows a -> Transaction (Rows a)

Select every matching row. Requires a prior map.

Examples

access todos token
    |> map (\todo -> { id = todo.id })
    |> selectAll

reduce

reduce : Reducer row a -> Rows row -> Transaction a

Run a typed aggregate over a query (no map required).

Examples

access orders token
    |> reduce (Database.Int64.sum .total)

access items token
    |> reduce Reducer.count

toList

toList : Rows a -> List a

Materialized 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 -> Int

How 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 Int

Count rows in a query (SELECT COUNT(*)). Ignores limit / offset. Does not need map.

Examples

access todos token
    |> filter (\todo -> todo.completed == False)
    |> count

sum

sum : (a -> number) -> Rows a -> Transaction number

Sum a numeric field (SELECT SUM(col)). The projection must be a field accessor (.total). Does not need map.

Examples

access orders token
    |> sum .total