我正在尝试将这个Heist 教程和这个postgresql-simple 教程结合起来。
我尝试对此进行不同的变体。
splice :: C.Splice IO
splice = do
projects <- query_ "SELECT * FROM projects"
C.manyWithSplices C.runChildren projectSplice $ return (projects :: [Project])
where
projectSplice = do
"title" ## (C.pureSplice . C.textSplice $ title)
"description" ## (C.pureSplice . C.textSplice $ description)
但我一直收到这个错误。
No instance for (HasPostgres (HeistT IO IO))
arising from a use of 'query_'
Possible fix:
add an instance declaration for (HasPostgres (HeistT IO IO))
In a stmt of a 'do' block:
projects <- query_ "SELECT * FROM projects"
In the expression:
do { projects <- query_ "SELECT * FROM projects";
C.manyWithSplices C.runChildren projectSplice
$ return (projects :: [Project]) }
我不知道如何实现该实例声明,我仍然没有完全掌握单子。我不确定我是否走在正确的轨道上。
编辑:感谢@mightybyte 的回答,我想出了这个。
projectSplice = do
"title" ## (C.pureSplice . C.textSplice $ title)
"description" ## (C.pureSplice . C.textSplice $ description)
splice :: C.Splice (Handler App App)
splice = C.manyWithSplices C.runChildren projectSplice $ lift $ query_ "SELECT * FROM projects"
getHeistState heistConfig = liftIO $ either (error "Heist Init failed") id <$> (runEitherT $ initHeist heistConfig)
getBuilder heistState = maybe (error "Render template failed") fst $ C.renderTemplate heistState "database"
getAllProjectsHeist :: Handler App App ()
getAllProjectsHeist = do
let heistConfig = HeistConfig defaultInterpretedSplices defaultLoadTimeSplices ("project" ## splice) noSplices [loadTemplates "templates"]
heistState <- getHeistState heistConfig
builder <- getBuilder heistState
writeBS $ toByteString builder