mongoDB
ist generisch genug, um in jeder Monade zu funktionieren, die eine Instanz von MonadBaseControl IO ist und MonadIO .
Sie können beispielsweise IO auswählen Monade. In diesem Fall benötigen Sie liftIO . runQuery in Scottys Aktion:
import Web.Scotty
import Database.MongoDB
import qualified Data.Text.Lazy as T
import Control.Monad.IO.Class
runQuery :: Pipe -> Query -> IO [Document]
runQuery pipe query = access pipe master "nutrition" (find query >>= rest)
main = do
pipe <- connect $ host "127.0.0.1"
scotty 3000 $ do
get "/" $ do
res <- liftIO $ runQuery pipe (select [] "stock_foods")
text $ T.pack $ show res
Nach @Sebastian Philipp
hinzugefügt
MonadBaseControl Instanz für Scotty.ActionT , es besteht keine Notwendigkeit, etwas zu heben. Sie können transparent mit mongoDB Form Scotty arbeiten. Ändern Sie einfach die Typsignatur und löschen Sie liftIO s:
runQuery :: Pipe -> Query -> ActionM [Document]
...
get "/" $ do
res <- runQuery pipe (select [] "stock_foods")
text $ T.pack $ show res