I was looking for a permanent library for interacting with sql databases. Suppose I have a database containing recipes, with tables of recipes, ingredients and RecIng.
My (admittedly limited) understanding of resilience leads me to think that I should define the tables as follows:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| Recipe title String Ingredient name String RecIng recId RecipeId ingId IngredientId quantity Int |]
With this, you can use Esqueleto to get the inner join between these tables:
select $ from $ \(i `InnerJoin ` ri `InnerJoin` r) -> do on (r ^. RecipeId ==. ri ^. RecIngIngId) on (i ^. IngredientId ==. ri ^. RegIngRecId) return (r, ri, i)
This returns a tuple (Recipe, RecIng, Ingredient).
What I really want is a recipe request method, which leads to the following:
data Recipe = Recipe { title :: String , ingredients :: [Ingredient] } data Ingredient = Ingredient { name :: String , quantity :: Integer }
Besides defining an additional set of data types and converting tuples, is there a best practice for this kind of thing?
sql haskell
oneway
source share