snaplet-mongodb-minimalistic 0.0.2 → 0.0.3
raw patch · 3 files changed
+73/−12 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- snaplet-mongodb-minimalistic.cabal +1/−1
- src/Snap/Snaplet/MongoDB/Core.hs +18/−2
- src/Snap/Snaplet/MongoDB/Functions.hs +54/−9
snaplet-mongodb-minimalistic.cabal view
@@ -1,5 +1,5 @@ name: snaplet-mongodb-minimalistic-version: 0.0.2+version: 0.0.3 synopsis: Minimalistic MongoDB Snaplet. description: Minimalistic MongoDB Snaplet. license: BSD3
src/Snap/Snaplet/MongoDB/Core.hs view
@@ -7,14 +7,30 @@ import System.IO.Pool --------------------------------------------------------------------------------- |+-- | Snaplet's data type.+--+-- Example:+-- @+-- data App = App+-- { _heist :: Snaplet (Heist App)+-- , _database :: Snaplet MongoDB+-- }+-- @ data MongoDB = MongoDB { mongoPool :: Pool IOError Pipe , mongoDatabase :: Database } --------------------------------------------------------------------------------- |+-- | Snaplet's type-class.+--+-- Example:+-- @+-- instance HasMongoDB App where+-- getMongoDB = getL (snapletValue . database)+-- @+--+-- Note: The (.) is from Control.Category. class HasMongoDB app where getMongoDB :: app -> MongoDB
src/Snap/Snaplet/MongoDB/Functions.hs view
@@ -23,12 +23,24 @@ import System.IO.Pool --------------------------------------------------------------------------------- |+-- | Description text used in mongoDBInit as makeSnaplet argument. description :: Text-description = "Minimalistic MongoDB Snaplet"+description = "Minimalistic MongoDB Snaplet." --------------------------------------------------------------------------------- |+-- | Initializer function.+-- 1. argument: Maximum pool size.+-- 2. argument: Host (e.g. return value of MongoDB's host function).+-- 3. argument: Database name.+--+-- Example:+-- @+-- app :: SnapletInit App App+-- app = makeSnaplet "app" "An snaplet example application." Nothing $ do+-- h <- nestSnaplet "heist" heist $ heistInit "resources/templates"+-- d <- nestSnaplet "database" database $ mongoDBInit 10 (host "127.0.0.1") "Snaplet-MongoDB"+-- return $ App h d+-- @ mongoDBInit :: Int -> Host -> Database -> SnapletInit app MongoDB mongoDBInit n h d = makeSnaplet "snaplet-mongodb" description Nothing $ do pool <- liftIO $ newPool (Factory (connect h) close isClosed) n@@ -38,36 +50,69 @@ instance (MonadIO m, MonadState app m, HasMongoDB app) => HasMongoDB' app m --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: Action to perform. (Defaults to UnconfirmedWrites AccessMode)+-- Returns: The action's result; in case of failure error is called.+--+-- Example:+-- > unsafeWithDB $ insert "test-collection" ["some_field" = " something" ] unsafeWithDB :: (HasMongoDB' app m) => Action IO a -> m a unsafeWithDB = unsafeWithDB' UnconfirmedWrites --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: AccessMode.+-- 2. argument: Action to perform.+-- Returns: The action's result; in case of failure error is called.+--+-- Example:+-- > unsafeWithDB' UnconfirmedWrites $ insert "test-collection" ["some_field" = " something" ] unsafeWithDB' :: (HasMongoDB' app m) => AccessMode -> Action IO a -> m a unsafeWithDB' mode action = do res <- (eitherWithDB' mode action) return $ either (error . show) id res --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: Action to perform. (Defaults to UnconfirmedWrites AccessMode)+-- Returns: Nothing in case of failure or Just the rsult of the action.+--+-- Example:+-- > maybeWithDB $ insert "test-collection" ["some_field" = " something" ] maybeWithDB :: (HasMongoDB' app m) => Action IO a -> m (Maybe a) maybeWithDB = maybeWithDB' UnconfirmedWrites --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: AccessMode.+-- 2. argument: Action to perform.+-- Returns: Nothing in case of failure or Just the rsult of the action.+--+-- Example:+-- > maybeWithDB' UnconfirmedWrites $ insert "test-collection" ["some_field" = " something" ] maybeWithDB' :: (HasMongoDB' app m) => AccessMode -> Action IO a -> m (Maybe a) maybeWithDB' mode action = do res <- (eitherWithDB' mode action) return $ either (const Nothing) Just res --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: Action to perform. (Defaults to UnconfirmedWrites AccessMode)+-- Returns: Either Failure or the action's result.+--+-- Example:+-- > eitherWithDB $ insert "test-collection" ["some_field" = " something" ] eitherWithDB :: (HasMongoDB' app m) => Action IO a -> m (Either Failure a) eitherWithDB = eitherWithDB' UnconfirmedWrites --------------------------------------------------------------------------------- |+-- | Database access function.+-- 1. argument: AccessMode.+-- 2. argument: Action to perform.+-- Returns: Either Failure or the action's result.+--+-- Example:+-- > eitherWithDB' UnconfirmedWrites $ insert "test-collection" ["some_field" = " something" ] eitherWithDB' :: (HasMongoDB' app m) => AccessMode -> Action IO a -> m (Either Failure a) eitherWithDB' mode action = do (MongoDB pool database) <- gets getMongoDB