Saving Data With CouchDB
In my previous post, we queried some data in CouchDB. But the problem with that is we need to put some data into the database if we want to query it.
Adding data to CouchDB isn’t difficult. You just need to insert another document. Doing so is pretty straightforward.
If you want to insert a JSON document into CouchDB, you just need to call the INSERT method. The Nano framework makes doing so easy.
Calling that simple saveContact method will save a record to the database. But what if you want to update a record? That’s where things get a little more interesting. If you are accustomed to updating data, you are used to the concept of providing the ID for the record. But with CouchDB, you must provide the revision ID for the most recent save.
What?
It might be easier to show you.
If you save this:
You will get this back:
If you try and update the record, you MUST provide the same _rev back.
CouchDB is also a little unique because to update a record, you just call insert again.