Safe Haskell | Safe-Infered |
---|
A convenient wrapper around Database.CouchDB.Enumerator and Data.Aeson.Generic
The aeson library has the ability to encode and decode JSON using the generic
Data and Typeable classes via the Data.Aeson.Generic module. It isn't too
hard to use fromJSON
and toJSON
combined with the functions in
Database.CouchDB.Enumerator, except that in several cases Couch DB uses
system fields _id and _rev which present a small difficulty.
For example, Couch DB will return an object like the following
{ "_id": "somedoc", "_rev": "11-52b4f9b471de393fab82313b9d8571c1", "foo": 3, "bar": true }
Also, occasionally (not always) the _rev field must be present in an object that is sent to Couch DB during a PUT.
The short wrapper functions in this module take care of handling the _id and _rev fields separately from the encoding and decoding to the generic data structure.
import Data.Data (Data, Typeable) import Data.Bytestring (Bytestring) import Database.CouchDB.Enumerator hiding (couchGet, couchPut) import qualified Database.CouchDB.Enumerator.Generic as G data Rec = Rec { field1 :: Int , field2 :: ByteString } deriving (Data, Typeable) testCouch :: IO () testCouch = runCouch "localhost" 5984 "test" $ do -- Insert doc rev1 <- G.couchPut "doc1" Nothing [] $ Rec 1 "foo" -- Get doc G.CouchDoc p r doc1 <- G.couchGet "doc1" [] -- New revision rev2 <- G.couchPut "doc1" (Just rev1) [] $ Rec 2 "bar"
- data CouchDoc a = CouchDoc Path Revision a
- couchGet :: (MonadCouch m, Data a) => Path -> Query -> m (CouchDoc a)
- couchPut :: (MonadCouch m, Data a) => Path -> Revision -> Query -> a -> m Revision
- couchPut' :: (MonadCouch m, Data a) => Path -> Query -> a -> m Revision
- consumeView :: (MonadCouch m, Data a) => Path -> Query -> m [a]
- parseGeneric :: (Monad m, Data a) => Enumeratee Object a m b
Couch DB documents API for Generic
CouchDB document with path and revision.
:: (MonadCouch m, Data a) | |
=> Path | the dbname is prepended to this string to form the full path. |
-> Query | Query arguments. |
-> m (CouchDoc a) |
Load a single object from couch DB.
:: (MonadCouch m, Data a) | |
=> Path | the dbname is prepended to this string to form the full path. |
-> Revision | Revision. Empty string for new documents. |
-> Query | Query arguments. |
-> a | Data |
-> m Revision |
Put an object in Couch DB, returning the new Revision.
:: (MonadCouch m, Data a) | |
=> Path | the dbname is prepended to this string to form the full path. |
-> Query | Query arguments. |
-> a | Data |
-> m Revision |
Brute force version of couchPut
. Stores document regardless of presence
in database (catches couchRev
CouchError
404).
This version is slower that couchPut
because it first tries to find the
document revision.
Also, there are no guarantees that some other thread or program updated the object (and thus generated a new revision) between loading the existing revision and deleting the object. If this occurs, an error will still be thrown.
Couch DB views API for Generic
:: (MonadCouch m, Data a) | |
=> Path | the dbname is prepended to this string to form the full path. |
-> Query | Query arguments. |
-> m [a] |
Strictly consumes all view result. Use this if all view data is mandatory and all errors must be handled.
parseGeneric :: (Monad m, Data a) => Enumeratee Object a m bSource
Parse Object
from extractViewValue
.