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

Safe HaskellNone



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
import Data.Data (Typeable)

data Post = Post { time :: UTCTime
                 , author :: String
                 , content :: String
                 , votes :: Int
          deriving (Show, Read, Eq, Ord, Typeable)
$(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, Typeable a) => Bson a where Source #

Minimal complete definition

toBson, fromBson


toBson :: a -> Document Source #

fromBson :: Monad m => Document -> m a Source #

deriveBson :: Name -> Q [Dec] Source #

Derive Bson and Val declarations for a data type.

selectFields :: [Name] -> Q Exp Source #

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 Exp Source #

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 -> Document Source #

Simple function to select fields in a nested document.

getField :: Name -> Q Exp Source #

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].