我一直在尝试通过与mtl结合使用来构建项目,从而对persistent有很好的了解。
该项目的一个模块具有使用insertMany_的功能
service
:: (MonadReader ApplicationConfig m, MonadIO m) =>
ReaderT SqlBackend (ExceptT ApplicationError m) ()
service = insertMany_ =<< lift talkToAPI
这里talkToAPI
可能失败,因此响应被包装在ExceptT
中,其类型为
ExceptT ApplicationError m [Example]
简而言之,service
的工作是与API对话,解析响应,然后使用insertMany_
将响应存储到数据库中。
实际的存储动作由withPostgresqlConn处理
withPostgresqlConn
:: (MonadUnliftIO m, MonadLogger m) =>
ConnectionString -> (SqlBackend -> m a) -> m a
在runReaderT
函数上使用service
产生
ghci> :t runReaderT service
ghci> (MonadReader ApplicationConfig m, MonadIO m) =>
SqlBackend -> ExceptT ApplicationError m ()
因此要处理此[[我相信我需要使用runExceptT
这样的]
runService :: ConnectionString -> IO ()
runService connStr = either print return
=<< runStdLoggingT (runExceptT $ withPostgresqlConn connStr $ runReaderT service)
但是我遇到了这两个错误
• No instance for (MonadUnliftIO (ExceptT ApplicationError IO)) arising from a use of ‘withPostgresqlConn’ • No instance for (MonadReader ApplicationConfig IO) arising from a use of ‘service’
这里可能是什么问题?我可能有一个错误,但我不确定在哪里寻找。
ExceptT ApplicationError IO
没有-实际上没有-具有MonadUnliftIO
实例。很少有Monad具有该实例:MonadUnliftIO
(小写),IO
和类似Identity
的Monad。