| Safe Haskell | None |
|---|
Web.Wheb.Plugins.Mongo
Description
Basic implementation of MongoDB connection.
Adds default instances for SessionApp and AuthApp for MongoApp.
You can override the collection names for the Auth and Session documents by using addSetting and setting keys for "session-collection"
and "auth-collection"
opts <- generateOptions $ do
addSetting "session-collection" "my-collection"
Reimplimentation of official example below. Use with language extensions OvererloadedStrings & ExtendedDefaultRules.
import qualified Data.Text.Lazy as T
import Web.Wheb
import Web.Wheb.Plugins.Mongo
data MyApp = MyApp MongoContainer
data MyRequestState = MyRequestState
instance MongoApp MyApp where
getMongoContainer (MyApp mc) = mc
homePage :: WhebHandler MyApp MyRequestState
homePage = do
mongoRes <- runAction $ do
delete (select [] "team")
insertMany "team" [
["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"],
["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"],
["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"],
["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ]
rest =<< find (select [] "team") {sort = ["home.city" =: 1]}
case mongoRes of
Left err -> text $ spack err
Right teams -> text $ T.intercalate " | " $ map spack teams
main :: IO ()
main = do
opts <- generateOptions $ do
addGET "." rootPat $ homePage
mongo <- initMongo "127.0.0.1:27017" "master"
return (MyApp mongo, MyRequestState)
runWhebServer opts
- runAction :: (MongoApp g, MonadIO m) => Action IO a -> WhebT g s m (Either Failure a)
- initMongo :: Text -> Text -> InitM g s m MongoContainer
- catchResult :: Monad m => WhebT g s m (Either Failure b) -> WhebT g s m b
- class MongoApp a where
- getMongoContainer :: a -> MongoContainer
- data MongoContainer
- module Database.MongoDB
Documentation
runAction :: (MongoApp g, MonadIO m) => Action IO a -> WhebT g s m (Either Failure a)Source
Run a MongoDB Action Monad in WhebT
initMongo :: Text -> Text -> InitM g s m MongoContainerSource
Initialize mongo with "host:post" and default database.
catchResult :: Monad m => WhebT g s m (Either Failure b) -> WhebT g s m bSource
Push an error from Mongo to a 500 Error.
Methods
module Database.MongoDB