on master help sign in
Rough documentation for constructing orderings
commit3e8f632cd1dd7d5cc3bce2a622e9fc30da3492e8 authorOliver Charles <ollie@ocharles.org.uk> dateSat 24 Apr 2021, 22:29 (6 years ago) parent26ccab3
1 file changed+23−9
docs/concepts/queries.rst+23−9
docs/concepts/queries.rst+23−9
@@ -193,8 +193,8 @@ and this query can be written in Rel8 as::
193193 where::
194194
195195 authorForBlogPost :: BlogPost Expr -> Query (Author Expr)
196− authorForBlogPost blogPost =
197− filter ((blogPostAuthorId blogPost ==.) authorId author) =<<
196+ authorForBlogPost blogPost =
197+ filter ((blogPostAuthorId blogPost ==.) authorId author) =<<
198198 each authorSchema
199199
200200 While this is a little more code over all, in our experience this style
@@ -210,7 +210,7 @@ row.
210210In Rel8, a ``LEFT JOIN`` is introduced by converting an inner join with
211211``optional``. While this approach might seem a little foreign at first, it has a
212212strong similarity with the ``Control.Applicative.optional`` function, and allows
213−you to reuse previous code.
213+you to reuse previous code.
214214
215215To see an example of this, let's assume that we want to get the latest comment
216216for 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::
218218
219219 blogPost <- each blogPostSchema
220220
221− latestComment <-
222− optional $ limit 1 $
223− orderBy (commentCreatedAt >$< desc) $
221+ latestComment <-
222+ optional $ limit 1 $
223+ orderBy (commentCreatedAt >$< desc) $
224224 commentsForBlogPost blogPost
225−
225+
226226``optional`` will transform a ``Query a`` into a ``Query (MaybeTable a)``.
227227``MaybeTable`` is similar to the normal ``Maybe`` data type in Haskell, and
228228represents the choice between a ``justTable x`` and a ``nothingTable`` (like
@@ -241,9 +241,23 @@ operate on ``Maybe``. For more details, see the API documentation.
241241Ordering results
242242----------------
243243
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.
245247
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])
247261
248262Aggregating queries
249263-------------------