Haskell/Persistent: повторное использование "selectList"
Я пытаюсь написать API на Servant и пишу много обработчиков, которые выглядят так:
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