Anonymous avatar Anonymous committed c9bd997

Added better README

Comments (0)

Files changed (3)

 
 Depends on Snap 0.7.* and <others>
 
-Based on work of Ozgun Ataman
-His Snap Extension for Snap < 0.6.* is here:
-https://github.com/ozataman/snap-extension-mongodb
+Based on work of Massive Tactical
 
-Code intended for review and refactor.
+Their MongoDB helper package ( mostly used with Yesod ) is there:
+https://github.com/MassiveTactical/mt-mongodb
+
+Usage ( assuming you've used snap init to layout your app ):
+
+In Application.hs:
+```Haskell
+import Snap.Snaplet.MongoDB
+```
+
+then add mongoDB snaplet to App state:
+```Haskell
+data App = App
+    { _heist :: Snaplet (Heist App)
+    , _mongoDB :: Snaplet MongoDBSnaplet
+    ...
+```
+
+then make App an instance of HasMongoDBState:
+```Haskell
+instance HasMongoDBState App where
+  getMongoDBState = with mongoDB Snap.get
+  setMongoDBState s = undefined -- with mongo $ puts s
+```
+
+next in Site.hs ( in app = ...):
+```Haskell
+m <- nestSnaplet "mongoDB" mongoDB $ mongoDBInit (host "localhost") 12 "my_db"
+```
+and don't forget to pass this "m" to your App data constructor..
+
+... and voila!
+
+Now you've got "withDB" function which "lifts" your Handler Monad
+so You can use db functions ( as described at mt-mongodb pages ).
+
+ex:
+```Haskell
+some_handler :: Handler App App ()
+some_handler = do
+  ret <- withDB $ select [mongo| {} |][limit 10]
+  case ret of
+    ...
+```

snaplet-mongoDB.cabal

 -- The package version. See the Haskell package versioning policy
 -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for
 -- standards guiding when and how versions should be incremented.
-Version:             0.1
+Version:             0.2
 
 -- A short (one-line) description of the package.
 Synopsis:            Snap Framework MongoDB support as Snaplet
   library
   exposed-modules:  Snap.Snaplet.MongoDB
                   , Snap.Snaplet.MongoDB.Template
+                  , Snap.Snaplet.MongoDB.FilterOps
+                  , Snap.Snaplet.MongoDB.MongoEntity
+                  , Snap.Snaplet.MongoDB.MongoValue
+                  , Snap.Snaplet.MongoDB.Parse
   
   -- Packages needed in order to build this package.
   Build-depends:

src/Snap/Snaplet/MongoDB.hs

   , MongoDBSnaplet(..)
   , HasMongoDBState(..)
 
+  , MongoValue (..)
+  , MongoEntity (..)
+
   , mongoDBInit
 
   , insert
   , limit
   , orderAsc
   , orderDesc
+
+  , objid2bs
+  , bs2objid
+  , bs2objid'
+  , bs2cs
+  , getObjId
    
   , module Snap.Snaplet.MongoDB.FilterOps
   , module Snap.Snaplet.MongoDB.MongoEntity
 import           Data.Bson ((=:))
 import qualified Data.Bson as BSON
 import qualified Data.UString as US
-import           Data.Maybe (catMaybes)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Char8 as B8
+import qualified Data.CompactString.Internal as CSI
+import           Data.Maybe (catMaybes, fromJust)
+import qualified Data.Map as Map
+import           Numeric (showHex, readHex)
+import           Safe
 import           Snap.Snaplet.MongoDB.FilterOps
 import           Snap.Snaplet.MongoDB.MongoEntity
 import           Snap.Snaplet.MongoDB.MongoValue
       throwError $ MongoDB.QueryFailure 1000 "Expected to find an _id field in result of selection"
 
 
+------------------------------------------------------------------------------
+-- | Convert 'ObjectId' into 'ByteString'
+objid2bs :: ObjectId -> BS.ByteString
+objid2bs (Oid a b) = B8.pack . showHex a . showChar '-' . showHex b $ ""
+
+
+------------------------------------------------------------------------------
+-- | Convert 'ByteString' into 'ObjectId'
+bs2objid :: BS.ByteString -> Maybe ObjectId
+bs2objid bs = do
+  case B8.split '-' bs of
+    (a':b':_) -> do
+      a <- fmap fst . headMay . readHex . B8.unpack $ a'
+      b <- fmap fst . headMay . readHex . B8.unpack $ b'
+      return $ Oid a b
+    _ -> Nothing
+
+------------------------------------------------------------------------------
+-- | Like 'bs2objid', but may blow with an error if the 'ByteString' can't be
+-- converted to an 'ObjectId'
+bs2objid' :: BS.ByteString -> ObjectId
+bs2objid' = fromJust . bs2objid
+
+
+bs2cs :: BS.ByteString -> US.UString
+bs2cs = CSI.CS
+
+
+------------------------------------------------------------------------------
+-- | If the 'Document' has an 'ObjectId' in the given field, return it as
+-- 'ByteString'
+getObjId :: US.UString -> BSON.Document -> Maybe BS.ByteString
+getObjId v d = MongoDB.lookup v d >>= fmap objid2bs
+
+
 dummyFromKey :: Key a -> a
 dummyFromKey _ = undefined
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.