我正在研究一个Servant 0.7.1应用程序,并尝试使用Persistent-2.5来查询Postgresql数据库,但是我遇到了与Persistent查询不匹配的类型。
此应用程序以前使用的是Servant 0.4和Persistent 2.2,但是当我去Servant 0.7.1尝试BasicAuth的东西时(不同的stack
解析器,这就是为什么我最终得到了更高的我也是Persistent
的版本,我从EitherT ServantErr IO
更改为Servant的Handler
monad,由于某种原因,我无法再获得Persistent查询进行编译。
这是我的模型定义:
share [mkPersist sqlSettings] [persistLowerCase|
ESeries json
label String
name String Maybe
relatedId ESeriesId Maybe
|]
根据此blog-post,我有一个runDb
函数看起来像这样,它将在ReaderT
内运行:
runDb query = do
pool <- asks getPool
liftIO $ runSqlPool query pool
最后,我有以下api定义和处理程序:
type ESeriesApi = "series" :> Get '[JSON] [ESeries]
eSeriesApi :: Proxy ESeriesApi
eSeriesApi = Proxy
type AppM = ReaderT Config Handler
readerToHandler :: Config -> AppM :~> Handler
readerToHandler cfg = Nat $ \x -> runReaderT x cfg
eServer :: Config -> Server ESeriesApi
eServer cfg = enter (readerToHandler cfg) eSeriesServer
app :: Config -> Application
app cfg = serve eSeriesApi (eServer cfg)
eSeriesServer :: ServerT ESeriesApi AppM
eSeriesServer = allSeries
allSeries :: AppM [ESeries]
allSeries = do
series <- runDb $ selectList [] []
let results = map (\(Entity _ e) -> e) series
liftIO $ sequence results
我已经尝试了很多不同的变体,但它总是归结为同样的错误:
• Couldn't match type ‘persistent-2.5:Database.Persist.Class.PersistEntity.PersistEntityBackend
(IO ESeries)’
with ‘Database.Persist.Sql.Types.Internal.SqlBackend’
arising from a use of ‘selectList’
• In the second argument of ‘($)’, namely ‘selectList [] []’
In a stmt of a 'do' block: series <- runDb $ selectList [] []
In the expression:
do { series <- runDb $ selectList [] [];
let results = map (\ (Entity _ e) -> ...) series;
liftIO $ sequence results }
似乎selectList
没有返回正确的类型?
修改
我应该提到我正在尝试使用Persistent 2.5执行此操作,此代码以前使用的是早期版本的Persistent。
看起来runSqlPool
期待SqlPersistT
或ReaderT SqlBackend
但selectList
正在返回PersistEntityBackend (IO ESeries)