I’m a complete CouchDB novice. Here is what I learnt in the first few hours.
What is it?
tldr – It’s a document database, that heavily uses http for querying and administration
Installation
Setting up a dev environment on a mac is pretty easy. I have homebrew installed as my package manager.
brew search couch
showed that the package is called ‘couchdb’
brew install couchdb
went off and did it’s thing. After 45 minutes or so, it seemed to have hung on the make task. The CouchDB Mac installation instructions suggested using the command
brew install -v couchdb
and this worked for me.
Couch is now installed in /usr/local/bin/couchdb
on my system.
We start the server with couchdb
and see the following:
Apache CouchDB 1.4.0 (LogLevel=info) is starting. Apache CouchDB has started. Time to relax. [info] [] Apache CouchDB has started on http://127.0.0.1:5984/
In another window, we can act as a client – curl http://127.0.0.1:5984/
which tells us
{"couchdb":"Welcome","uuid":"8e9e41844b1be7e68475d288b5d14bc3","version":"1.4.0","vendor":{"version":"1.4.0-1","name":"Homebrew"}}
Web Admin via Futon
Futon is a web interface to CouchDB. We can perform database administration tasks, and create and edit documents. I was able to create a new database and insert a simple document.
Note that CouchDB has assigned and Id and a Revision to the document. You can assign your own unique identifiers should you wish.
You can even ask CouchDB to give you a range of UUIDs :
curl -X GET http://127.0.0.1:5984/_uuids?count=5
{"uuids":"0b225462ddccbb8f19ec612ef70319f9","0b225462ddccbb8f19ec612ef7031eba",
"0b225462ddccbb8f19ec612ef7032269","0b225462ddccbb8f19ec612ef7032e8d",
"0b225462ddccbb8f19ec612ef7032f8d"]}
Please don’t use the above UUIDs, they belong to me
Retrieving data with node.js
Following the code from https://github.com/cloudhead/cradle it was super simple to query the document we have already created.
After we have run npm install cradle
the following was enough to get us started.
var cradle = require('cradle'); var db = new(cradle.Connection)().database('hello_couch'); db.get('0b225462ddccbb8f19ec612ef70007fd', function(err, doc) { console.log(doc); });
which returns
{ _id: '0b225462ddccbb8f19ec612ef70007fd', _rev: '1-7f6fc4765d88ba5da29b5f5938b46c2c', message: 'hello from couch db' }
Getting started was simple enough. Now to learn more about the ecosystem and how to use it in a real application.