Permalink
Browse files

Add utility functions

  • Loading branch information...
1 parent f8b819e commit 9b446ca46ffe951d8706205863cc99cecce48815 @ozataman committed Dec 24, 2010
Showing with 42 additions and 0 deletions.
  1. +42 −0 src/Snap/Extension/MongoDB.hs
View
42 src/Snap/Extension/MongoDB.hs
@@ -27,6 +27,12 @@ module Snap.Extension.MongoDB
-- ** Initializing Your Applications
, mongoDBInitializer
+
+ -- * Utility Functions
+ , getObjId
+ , bs2objid
+ , objid2bs
+
-- * MongoDB Library
-- | Exported for your convenience.
, module Database.MongoDB
@@ -47,6 +53,8 @@ import qualified Data.Map as Map
import Data.Map (Map)
import Data.Word (Word8)
+import Numeric (showHex, readHex)
+
import Database.MongoDB
import Snap.Types
@@ -161,3 +169,37 @@ instance HasMongoDBState s => MonadMongoDB (SnapExtend s) where
withDB run = do
(MongoDBState pool db) <- asks getMongoDBState
liftIO . access safe Master pool $ use db run
+
+
+
+
+------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+-- Convenience Functions
+------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+
+
+------------------------------------------------------------------------------
+-- | Convert 'ObjectId' into 'ByteString'
+objid2bs :: ObjectId -> ByteString
+objid2bs (Oid a b) = B8.pack . showHex a . showChar '-' . showHex b $ ""
+
+
+------------------------------------------------------------------------------
+-- | Convert 'ByteString' into 'ObjectId'
+bs2objid :: ByteString -> ObjectId
+bs2objid bs = Oid a b
+ where (a',b') = break (== '-') . B8.unpack $ bs
+ a = fst . head . readHex $ a'
+ b = fst . head . readHex $ drop 1 b'
+
+
+------------------------------------------------------------------------------
+-- | If the 'Document' has an 'ObjectId' in the given field, return it as
+-- 'ByteString'
+getObjId :: UString -> Document -> Maybe ByteString
+getObjId v d = Database.MongoDB.lookup v d >>= fmap objid2bs
+
+
+

0 comments on commit 9b446ca

Please sign in to comment.