couch-hs-0.1.3: A CouchDB view server for Haskell.




Map Functions

type MapSignature = Object -> ViewMap ()Source

The type of your map functions as they are stored in CouchDB. The trivial example:

 \doc -> return ()

type ViewMap a = ViewMapT Parser aSource

The monad within which a map computation takes place. This is a transformation of the Parser monad, although the precise nature and depth of the transformation is an internal detail and subject to change. ViewMapT is guaranteed to be an instance of the MonadParser class, allowing you to parse JSON structures.

JSON Parsing

JSON parsers lifted into our view monads. This also exports one or two useful symbols from Data.Aeson.Types.

class Monad m => MonadParser m whereSource

Like MonadIO, but for Parser. This allows JSON parsing operations to be lifted into our various view monads.


liftParser :: Parser a -> m aSource

parseJSON :: (MonadParser m, FromJSON a) => Value -> m aSource

Attempts to parse a JSON value into a given type. This is typically used with a type annotation to indicate the target type. If the value can not be parsed into that type, the entire computation will fail.

parseJSONList :: (MonadParser m, FromJSON a) => [Value] -> m [a]Source

Applies parseJSON to a list of values. This is commonly used with the reduce function arguments.

(.:) :: (MonadParser m, FromJSON a) => Object -> Text -> m aSource

Parses a required field of an object. If the field is not present, or the value can not be parsed into the target type, the computation will fail.

(.:?) :: (MonadParser m, FromJSON a) => Object -> Text -> m (Maybe a)Source

Parses an optional field of an object. This will not halt the computation on failure.

(.=) :: ToJSON a => Text -> a -> Pair

Construct a Pair from a key and a value.

object :: [Pair] -> Value

Create a Value from a list of name/value Pairs. If duplicate keys arise, earlier keys and their associated values win.

data Value

A JSON value represented as a Haskell value.

ViewMap Monads

emit :: (ToJSON k, ToJSON v) => k -> v -> ViewMap ()Source

Emit a key/value pair for the current document. The values will be turned into JSON objects for you, although you will have to provide type annotations somewhere.

\doc -> do value <- doc .: "value" :: ViewMap Double
           emit Null value

emitM :: (ToJSON k, ToJSON v) => ViewMap k -> ViewMap v -> ViewMap ()Source

Same as emit, but with wrapped key and value.

\doc -> emitM (return Null) (doc .: "value" :: ViewMap Double)

logMsg :: String -> ViewMap ()Source

Send a log message to the CouchDB server. Note that log messages are only sent if the computation succeeds. If you want to log a message in the event of a failure, look at Alternative.