从Haskell和Yesod开始,可能与Yesod相对于Haskell有点过头了:)
我使用Persistent via
构建实体share [mkPersist sqlSettings, mkMigrate "migrateAll"][persistLowerCase|
Game
title String
company String
UniqueTitle title
deriving Show
Tag
label String
description String Maybe
UniqueLabel label
deriving Show
GameTag
gameId GameId
tagId TagId
UniqueGameTag gameId tagId
|]
-- Yesod related code ...
主要是我
main :: IO ()
main = do
let taggings = fromFile :: [(Game, Tag)] -- fromFile code not included
runStderrLoggingT $ withSqlitePool ":inmemory:" 10 $ λpool → liftIO $ do
runResourceT $ flip runSqlPool pool $ do
runMigration migrateAll
let (g, t) = head taggings
gid ← insert g
tid ← insert t
insert (GameTag gid tid)
warp 3000 $ App pool
这样做我得到了第一个与数据库的关系,通过从列表中选择元素我可以手动添加更多,但我无法弄清楚如何通过某种方式迭代来获取所有关系到数据库taggings
。我如何定义一个可以映射到taggings ::[(Game, Tag)]
的函数并插入构造为GameTag
的游戏代码
通过Persistent?