From 4c121d24806ddf1229b86f99b3d7d9aaff8925c3 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sun, 3 Oct 2021 00:59:19 +0300 Subject: [PATCH 1/2] Add note about multiple nested resources can have multiple resources --- Guide/relationships.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Guide/relationships.markdown b/Guide/relationships.markdown index ae9849eaf..40c8ade69 100644 --- a/Guide/relationships.markdown +++ b/Guide/relationships.markdown @@ -59,6 +59,8 @@ The `post |> get #comments` returns a list of the comments belonging to the post The type of `post` is `Include "comments" Post` instead of the usual `Post`. This way the state of a fetched nested resource is tracked at the type level. +It is possible to have multiple nested resources. For example, if Post had a list of comments and tags related to it, it can be defined as `Include "comments" (Include "tags" Post)` + ### Order by When we want to order the relationship in a certain way, we can just apply our commonly used `orderBy` function: From 403924d2f17015cec9904a1b6fbf7d8d2234aae7 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sun, 3 Oct 2021 12:06:45 +0300 Subject: [PATCH 2/2] More multiple Include docs --- Guide/relationships.markdown | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Guide/relationships.markdown b/Guide/relationships.markdown index 40c8ade69..964587676 100644 --- a/Guide/relationships.markdown +++ b/Guide/relationships.markdown @@ -59,7 +59,17 @@ The `post |> get #comments` returns a list of the comments belonging to the post The type of `post` is `Include "comments" Post` instead of the usual `Post`. This way the state of a fetched nested resource is tracked at the type level. -It is possible to have multiple nested resources. For example, if Post had a list of comments and tags related to it, it can be defined as `Include "comments" (Include "tags" Post)` +It is possible to have multiple nested resources. For example, if Post had a list of comments and tags related to it, it can be defined as `Include "comments" (Include "tags" Post)` or with the more convinient way as `Include' ["comments", "tags"] Post`. + +Note that for the above example, it is expected that the query will change as-well: + +```haskell +let postId :: Id Post = ... + +post <- fetch postId + >>= fetchRelated #comments + >>= fetchRelated #tags +``` ### Order by @@ -235,13 +245,13 @@ Of course, you can change this using the Schema Designer by clicking on the fore It is possible to join tables to a given primary table (the one associated with the queried type) and use the joined table to select rows from the primary table. For instance, the following code could be used to retrieve all posts by users from department 5: ```haskell -query @Posts - |> innerJoin @User (#authorId, #id) - |> innerJoinThirdTable @Department @User (#id, #departmentId) +query @Posts + |> innerJoin @User (#authorId, #id) + |> innerJoinThirdTable @Department @User (#id, #departmentId) |> filterWhereJoinedTable @Department (#number, 5) ``` -`innerJoin` is used to join the `users` table (for type `User`) to the primary table `posts` (for type `Posts`) on the columns `posts.author_id` and `users.id`. Type checks ascertain that both tables actually have the pertinent columns. +`innerJoin` is used to join the `users` table (for type `User`) to the primary table `posts` (for type `Posts`) on the columns `posts.author_id` and `users.id`. Type checks ascertain that both tables actually have the pertinent columns. The function `innerJoinThirdTable` is used to join a third table on a column of some previously joined table. In the example, the table is `departments` and it is joined on `departments.id = users.department_id`. Again, the type system ascertains that the columns actually exist on the pertinent tables. It is furthermore ascertained that the table associated with the second type `User` has been joined before. @@ -303,11 +313,11 @@ We can use it like this: ```haskell action PostsAction = do posts <- query @Post |> fetch - + postsTags <- query @PostTag |> filterWhereIn (#postId, ids posts) |> fetch - + tags <- query @Tag |> filterWhereIn (#id, map (get #tagId) postsTags) |> fetch