我正在尝试在Servant中编写一个API,我正在编写很多看起来像这样的处理程序:
getTableA :: Handler [ARecord]
getTableA = do
xs <- runDB (selectList [] [])
let records = map (\(Entity _ v) -> v) xs
return records
getTableB :: Handler [BRecord]
getTableB = do
xs <- runDB (selectList [] [])
let records = map (\(Entity _ v) -> v) xs
return records
getTableC :: Handler [CRecord]
getTableC = do
xs <- runDB (selectList [] [])
let records = map (\(Entity _ v) -> v) xs
return records
等等。
我希望能够拥有更像
的东西getTable :: Handler [a]
getTable = do
xs <- runDB (selectList [] [])
let records = map (\(Entity _ v) -> v) xs
return records
但是我收到了错误
Couldn't match expected type ‘SqlBackend’
with actual type ‘PersistEntityBackend val’
Relevant bindings include
getAll :: Handler [val] (bound at src/Novation/Handler.hs:101:1)
In the first argument of ‘runDB’, namely ‘(selectList [] [])’
In a stmt of a 'do' block: xs <- runDB (selectList [] [])
In the expression:
do { xs <- runDB (selectList [] []);
let records = map (\ (Entity _ v) -> ...) xs;
return records }
我希望最终我能够拥有这样的东西
server :: Server MyAPI
server = getTable :<|> getTable :<|> getTable
type MyAPI =
"tableA" :> Get '[JSON] [ARecord]
:<|> "tableB" :> Get '[JSON] [BRecord]
:<|> "tableC" :> Get '[JSON] [CRecord]
而不是
server :: Server MyAPI
server = getTableA :<|> getTableB :<|> getTableC