title: Handling List-types with Esqueleto
body: <p>I have data types defined as:</p>
  
  <pre><code>data ComitteeView = CommitteeView { committeeId :: CommitteeId
                                    , committeeMembers :: [Person] 
                                    }
  
  data CommitteesView = CommitteesView { committeeView :: [CommitteeView] }
  </code></pre>
  
  <p>Now, as it stands, I have a Persistent model defined as:</p>
  
  <pre><code>Person
    name  Text
  
  Committee
    name  Text
  
  CommitteePerson
    personId    PersonId
    committeeId CommitteeId
  </code></pre>
  
  <p>I can pretty easily create a query to populate a CommitteeView, using Esqueleto.  It would go something like this:</p>
  
  <pre><code>getCommitteeView cid = 
    CommitteeView &lt;$&gt; runDB $ 
      select $
        from (person `InnerJoin` pxc `InnerJoin` committee) -&gt; do
          on  (committee ^. CommitteeId ==. pxc ^. CommitteePersonCommitteeId)
          on  (person ^. PersonId       ==. pxc ^. CommitteePersonPersonId)
          where_ (committee ^. CommitteePersonCommitteeId ==. val cid)
          return person
  </code></pre>
  
  <p>Now, consider the problem of populating <code>CommitteesView</code>.  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 <code>group by</code> in SQL? How can I fold rows so that I can end up with a list of lists of people?</p>
  
  <p>I get the impression that <code>esqueleto</code> 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 <code>n+1</code> queries?  Are there any other options?  </p>