title: Haskell Persistent Joins with Esqueleto
body: <p>I've been looking into the Persistent library to interface with sql databases. Suppose I have a database containing recipes, with Recipe, Ingredient, and RecIng tables.</p>
  
  <p>My (admittedly limited) understanding of persistent leads me to believe I should define the tables like this:</p>
  
  <pre><code>share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
  Recipe 
      title String
  Ingredient 
      name String
  RecIng
      recId RecipeId
      ingId IngredientId
      quantity Int
  |]
  </code></pre>
  
  <p>With this, it is possible to use Esqueleto to get the inner join between these tables:</p>
  
  <pre><code>select $
  from $ \(i `InnerJoin ` ri `InnerJoin` r) -&gt; do
      on (r ^. RecipeId ==. ri ^. RecIngIngId)
      on (i ^. IngredientId ==. ri ^. RegIngRecId)
      return (r, ri, i)
  </code></pre>
  
  <p>This returns  tuple of (Recipe, RecIng, Ingredient). </p>
  
  <p>What I really want is a way to query recipes that results in the following:</p>
  
  <pre><code>data Recipe = Recipe { title :: String
                       , ingredients :: [Ingredient]
                       }
  
  data Ingredient = Ingredient { name :: String
                               , quantity :: Integer
                               }
  </code></pre>
  
  <p>Other than defining an additional set of data types, and converting the tuples, is there a best practice for doing this kind of thing?</p>