on master help sign in
Document listAgg and nonEmptyAgg
669b262Ollie Charles
src/Rel8/Table/Aggregate.hs+17
@@ -44,6 +44,22 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
44 dicts = hdicts @(Columns exprs) @(ConstrainDBType DBEq)
44 dicts = hdicts @(Columns exprs) @(ConstrainDBType DBEq)
45
45
46
46
47+-- | Aggregate rows into a single row containing an array of all aggregated
48+-- rows. This can be used to associate multiple rows with a single row, without
49+-- changing the over cardinality of the query. This allows you to essentially
50+-- return a tree-like structure from queries.
51+--
52+-- For example, if we have a table of orders and each orders contains multiple
53+-- items, we could aggregate the table of orders, pairing each order with its
54+-- items:
55+--
56+-- @
57+-- ordersWithItems :: Query (Order Expr, ListTable (Item Expr))
58+-- ordersWithItems = do
59+-- order <- each orderSchema
60+-- items <- aggregate $ listAgg <$> itemsFromOrder order
61+-- return (order, items)
62+-- @
47listAgg :: Table DB exprs => exprs -> Aggregate (ListTable exprs)
63listAgg :: Table DB exprs => exprs -> Aggregate (ListTable exprs)
48listAgg (toColumns -> exprs) = fromColumns $
64listAgg (toColumns -> exprs) = fromColumns $
49 hvectorize
65 hvectorize
@@ -51,6 +67,7 @@ listAgg (toColumns -> exprs) = fromColumns $
51 (pure exprs)
67 (pure exprs)
52
68
53
69
70+-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
54nonEmptyAgg :: Table DB exprs => exprs -> Aggregate (NonEmptyTable exprs)
71nonEmptyAgg :: Table DB exprs => exprs -> Aggregate (NonEmptyTable exprs)
55nonEmptyAgg (toColumns -> exprs) = fromColumns $
72nonEmptyAgg (toColumns -> exprs) = fromColumns $
56 hvectorize
73 hvectorize