bson-mapping-0.1.2: Mapping between BSON and algebraic data types.



This module aims to make mapping between algebraic data types and bson documents easy.

You can also generate documents with selectFields, which takes a list of functions names that of type a -> b and returns a function of type a -> Document.


 import Data.Bson.Mapping
 import Data.Time.Clock

 data Post = Post { time :: UTCTime
                  , author :: String
                  , content :: String 
                  , votes :: Int
           deriving (Show, Read, Eq, Ord)
 $(deriveBson ''Post)

 main :: IO ()
 main = do
   now <- getCurrentTime
   let post = Post now "francesco" "lorem ipsum" 5
   (fromBson (toBson post) :: IO Post) >>= print
   print $ toBson post
   print $ $(selectFields ['time, 'content]) post



class (Show a, Eq a, Data a, Typeable a) => Bson a whereSource


toBson :: a -> DocumentSource

fromBson :: Monad m => Document -> m aSource

deriveBson :: Name -> Q [Dec]Source

Derive Bson and Val declarations for a data type.

selectFields :: [Name] -> Q ExpSource

Select only certain fields in a document, see the code sample at the top.

Please note that there is no checking for the names to be actual fields of the bson document mapped to a datatype, so be careful.

getConsDoc :: Name -> Q ExpSource

Get a document that identifies the data type - getConsDoc ''Post.

This is useful to select all documents mapped to a certain data type.

subDocument :: Label -> Document -> DocumentSource

Simple function to select fields in a nested document.

getField :: Name -> Q ExpSource

Returns a function that gets a datatype and a value, and generates a Document consisting of one field - the label provided - and the value of that datatype.

$(getField 'time) post will generate ["time" =: time post].