Dealing with CouchDB and Couchbeam

I'll put this disclaimer up right away: My experience with CouchDB and Couchbeam is limited so I am definitely missing out on some knowledge and understanding that would probably make things easier. I won't detail CouchDB's offerings here as I have noted above my limited insight into that world. I will try to comment on using Couchbeam to get documents into and out of CouchDB, as well as writing a layer to take Couchbeam's document format and cleaning it up into slightly prettier Erlang data structures (proplists of binaries just aren't fun to type). The first thing to do is to connect to your database and get the PIDs for the connection and the DB using the couchbeam\server:start\connection\link/{0,1} and couchbeam\db:open\or\create/2. Couchbeam allows you to name the DB PID so you don't have to carry it around with you; just pass {DB\NAME\AS\ATOM, "DB\NAME\AS\LIST"} as the second parameter to open\or\create/2. Now calls can use DB\NAME\AS\ATOM instead of the DB process's PID to communicate. The biggest stumbling block out of the gate is the datatypes used by Couchbeam. Look at the API docs and get familiar with the types you'll be expected to provide and receive from the calls to the database and views. A document is either a json\object() or a 2-tuple {DocID, Revision}. A json\object is a proplist of [{json\string(), json\term()}]. json\strings are simple, atom or binary. A json\term is an atom, binary, integer, float, a list of json\terms, or another json\object(). Practically, for my needs, all keys are binaries and the majority of values are either binaries or numeric. I do have one place where I need to store a nested json\object(); wrap it in a 1-tuple as the value and you should be good.

%% nesting a json_object() as a value in a json_object()
ToNest = [{< >, < >}],
[{< >, < >},
 {< >, { ToNest } }].

Saving a doc is pretty easy; create the proplist and pass it to couchbeam\db:save\doc/2. One big gotcha when saving a document was whether the < > key existed. If the \rev key was undefined, a big ugly bad\match error was thrown with all sorts of chaos going on around it.