on master help sign in
Rough documentation for constructing orderings
3e8f632Oliver Charles
docs/concepts/queries.rst+23−9
@@ -193,8 +193,8 @@ and this query can be written in Rel8 as::
193 where::
193 where::
194
194
195 authorForBlogPost :: BlogPost Expr -> Query (Author Expr)
195 authorForBlogPost :: BlogPost Expr -> Query (Author Expr)
196− authorForBlogPost blogPost =
196+ authorForBlogPost blogPost =
197− filter ((blogPostAuthorId blogPost ==.) authorId author) =<<
197+ filter ((blogPostAuthorId blogPost ==.) authorId author) =<<
198 each authorSchema
198 each authorSchema
199
199
200 While this is a little more code over all, in our experience this style
200 While this is a little more code over all, in our experience this style
@@ -210,7 +210,7 @@ row.
210In Rel8, a ``LEFT JOIN`` is introduced by converting an inner join with
210In Rel8, a ``LEFT JOIN`` is introduced by converting an inner join with
211``optional``. While this approach might seem a little foreign at first, it has a
211``optional``. While this approach might seem a little foreign at first, it has a
212strong similarity with the ``Control.Applicative.optional`` function, and allows
212strong similarity with the ``Control.Applicative.optional`` function, and allows
213−you to reuse previous code.
213+you to reuse previous code.
214
214
215To see an example of this, let's assume that we want to get the latest comment
215To see an example of this, let's assume that we want to get the latest comment
216for each blog post. Not all blog posts are popular though, so some blog posts
216for each blog post. Not all blog posts are popular though, so some blog posts
@@ -218,11 +218,11 @@ might have no comment at all. To write this in Rel8, we could write::
218
218
219 blogPost <- each blogPostSchema
219 blogPost <- each blogPostSchema
220
220
221− latestComment <-
221+ latestComment <-
222− optional $ limit 1 $
222+ optional $ limit 1 $
223− orderBy (commentCreatedAt >$< desc) $
223+ orderBy (commentCreatedAt >$< desc) $
224 commentsForBlogPost blogPost
224 commentsForBlogPost blogPost
225−
225+
226``optional`` will transform a ``Query a`` into a ``Query (MaybeTable a)``.
226``optional`` will transform a ``Query a`` into a ``Query (MaybeTable a)``.
227``MaybeTable`` is similar to the normal ``Maybe`` data type in Haskell, and
227``MaybeTable`` is similar to the normal ``Maybe`` data type in Haskell, and
228represents the choice between a ``justTable x`` and a ``nothingTable`` (like
228represents the choice between a ``justTable x`` and a ``nothingTable`` (like
@@ -241,9 +241,23 @@ operate on ``Maybe``. For more details, see the API documentation.
241Ordering results
241Ordering results
242----------------
242----------------
243
243
244−.. todo::
244+Rel8 supports ordering the results returned by a ``Query``, using SQL's ``ORDER
245+BY`` syntax. To specify an ordering, you use ``orderBy`` and supply an
246+appropriate ``Order`` value.
245
247
246− Write this
248+An ``Order`` is built by combining the order of individual columns, each of
249+which can be either ascending or descending. To order a single column, you
250+combine ``asc`` or ``desc`` with ``Order``\s *contravariant* interface. For
251+example, if we have a table with a ``orderId`` column, we can order a ``Query
252+(Order Expr)`` by ``orderId`` with::
253+
254+ orderBy (orderId >$< asc)
255+
256+To order by multiple columns, combine the individual orders with ``Order``\s
257+``Monoid`` instance. We could extend the above example to order by the order
258+date first (with the most recent orders first) with::
259+
260+ orderBy (mconcat [orderDate >$< desc, orderId >$< asc])
247
261
248Aggregating queries
262Aggregating queries
249-------------------
263-------------------