I have data types defined as:
data ComitteeView = CommitteeView { committeeId :: CommitteeId
, committeeMembers :: [Person]
}
data CommitteesView = CommitteesView { committeeView :: [CommitteeView] }
Now, as it stands, I have a Persistent model defined as:
Person
name Text
Committee
name Text
CommitteePerson
personId PersonId
committeeId CommitteeId
I can pretty easily create a query to populate a CommitteeView, using Esqueleto. It would go something like this:
getCommitteeView cid =
CommitteeView <$> runDB $
select $
from (person `InnerJoin` pxc `InnerJoin` committee) -> do
on (committee ^. CommitteeId ==. pxc ^. CommitteePersonCommitteeId)
on (person ^. PersonId ==. pxc ^. CommitteePersonPersonId)
where_ (committee ^. CommitteePersonCommitteeId ==. val cid)
return person
Now, consider the problem of populating CommitteesView
. In principle, we get enough data to populate by running subquery in the above query. Okay, fair enough. Now how can I use "group by Haskell-list" like group by
in SQL? How can I fold rows so that I can end up with a list of lists of people?
I get the impression that esqueleto
can't handle the case as such (i.e., it doesn't have a combinator that would do it). And my underlying database obviously doesn't support Haskell lists as a column. But, surely, I can't be the only person to face this issue. What is an effective strategy? Folding an n-list of lists into a n-list? Or running n+1
queries? Are there any other options?
Data.List.groupBy
? – cdk