CouchDBI’ve been playing with CouchDB which is a “NoSQL” non-relational database server designed specifically for web development.  There are a lot of things I really like about it.  One of those is the integrated web user interface called Futon.  I was trying to create views but the only really obvious way I found was to use a command line tool called CouchApp.  CouchApp is great for building web apps on CouchDB but is really overkill when you want to just try some things in Futon.  Read on for more details on creating design documents from Futon.

Once I had created a view using CouchApp I found that I could edit the view from Futon…  The only problem was creating the view in the first place.  Maybe I’m missing something, but I didn’t see a way to do this in a straight forward way.  Fortunately it isn’t too difficult to do manually.

To create a new design document for your first view, create a new document with the New Document button in Futon and specify _id in the form _design/bar where _design tells CouchDB that this is a design document, and bar is the name we want to give this new design document:

New Design Document in CouchDB Futon

Then we’ll need to add two fields.  CouchDB design documents have a field for the language their functions are written in, and at this time only JavaScript is supported.  So a field called “language” with a value of “JavaScript” is required.  Finally we need a field for the views themselves.  This is supposed to be JSON with view names and values for map and reduce functions.  Futon only gives us one line to enter this value though when we’re adding a new value, so we don’t have a lot of room to work.  We could just enter an empty object literal {}, click the green checkbox, and then double click it to get a multi-line editor.  What I do though is keep a stub view handy in Evernote which I can enter in one line:

{ "viewname": { "map": "function(doc) {}" } }

This should be called ‘views’, so our new document looks like this in Futon:

CouchDB Design Document

You’ll want to change viewname to something more descriptive.  Once you’ve done that click the green check mark to set this value, and then the save document link at the top.  Then click the database name in the breadcrumbs at the very top (foo in my example) and drop down the view list to see that your new view is available:

CouchDB Views List

If you select your new view you’ll be able to open the View Code block to see your empty map function and an empty reduce field:

CouchDB stub view with empty map function

This panel can be enlarged by dragging the handle at the bottom so you have plenty of room to work on your view.  The great thing about working on these in Futon is that you can use the Run button to see how your code runs at any time without even having to save it first.

If you want to add another view to your design document you can use the view drop down again to choose Design Documents and then click the design document you want to add to.  Then double click the views value to edit it.  This is still not an ideal place to be editing views, but an empty stub like the first one can be easily added to the view’s JSON.  Add a comma after the last view and then paste in this stub:

"anotherview": { "map": "function(doc) {}" }

You’ll want to change anotherview to something more appropriate to your purpose.  Here’s what it would look like if we didn’t yet add to our first view:

{
"viewname": {
"map": "function(doc) {}"
},
"anotherview": { "map": "function(doc) {}" }
}

Remember to set this value with the green check mark and save the change.  Now your new view will be available in the view drop down list for editing.

For more on CouchDB design documents see the Design Documents chapter of CouchDB: The Definitive Guide.  If you like CouchDB consider supporting the authors by purchasing a copy.

I will be delivering a session on CouchDB at the upcoming TEC 20ii Conference in Toronto and will post my slides with more information on this subject then.

Advertisements