Thursday, February 14, 2013

Couchdb part 2

CYBERPOWERPC Gamer Xtreme GXi440 Gaming Computer (Google Affiliate Ad)
If you need help loading and starting Couchdb onto your server please go to one of these resources:

Couchdb the definitive guide
 Apache CouchDB
 and for

 There are many more out there, but I have found these most useful.

 As stated in my earlier post, my programming skills are a bit rusty, and at times I need someone to walk me bit by bit into the processes. I will, hopefully do that walk through here. I don’t really use forums such as stackoverflow, as I feel like an idiot when some answers are given to me. If you have questions, please ask, I hope I will not talk down or up to you.

 Couchdb, with JQuery and Javascript should be easy to use, and most of what you read tells you how easy it is, and once you cross the first hurdle it is. Crossing that hurdle took me several hours of work. Many more than it should have.

 Enough whining Let’s get started.

You need to have Couchdb and Couchapp installed, and if you are a normal computer person, I would assume you messed around a bit with both. You will be using the command line and the Couchdb web interface for most of this work. You will also need a text editing program. I use either notepad ++ or Komodo.

 These examples are done on a Linux Ubuntu box, not that it really makes too much of a difference.

 If you go to your web interface ( and make a new database, for this example call it ‘courses’ as this project is a catalog of training courses.
 Or you can use the curl command in the command line $ curl -X PUT 

If you messed with the security settings of couchdb, you may need to modify this command, something along the lines of: 

After that step, it is time to start making our application.

 Using the command line, change to a directory will want to work out of, I usually use the documents, or a subdirectory in documents, but have used others, just whatever you feel best using and does not get you confused.

This will be one of commands you will use over and over again, so learn to love it!
 $ couchapp generate app courses
What this does is makes a folder named courses.
The folder is full of many wonderful confusing things, most of which we will not deal with. There should be a few documents on the top level, couchappignore, couchapprc,, _id, couchapp.json, language. Not doing much with these for this tutorial. The folders in the directory should be _attachments, evently, lists, shows, updates, vendor, views.

 We will be dealing with the _attachements and views folders, the others are useful to poke around in, and will be useful for future projects.

 If you did not know already, Couchdb stores its data as JSON documents, which means that each piece of data can be searched by itself, and each set of data is its own document, no mucking about with too many key and making sure everything is set before starting. You can change you search and fields at anytime. If you need help understanding what JSON is there are many articles out there to help you.

We will start in the _attachments folder, specifically with the index.html file. As with most web applications, the index file is opened by default by a web browser. Right now the file is fairly boring, but we will change it. In a later part of this tutorial we will replace this index file with another, rename this file something else, and make a few similar copies of this index file. I am sure smarter people could get by with one index file, but I am not one. I am more of a code basher, than a finesse person. finesse is good. Bashing works for me

 This is what the index.html file should be changed to.

Now let us break this down to its parts. The header:

 Gives us the title , the css file location and loads up some scripts. As you can see there is a loader script named loader.js which loads most things, the recordedit.js script will be written by you soon, and I threw in a link to a general jquery library, as things were not working quite right before doing that.

 Now to the body:

 From the .css file we have a div named account. The title on the page will be “Course Data” The next line will make a link with the term Add Courses, to a form to add courses to our database. The next two divs will make a list of the current data or make a form to edit or add data to the database. The last two lines are used to maneuver between forms we will make later, feel free to leave them off for the time being. Ta-da one of three documents finished .

 The second document is even simpler. Making a view Views are how we, well, view the data. Please hang with me for a bit, as this may be a tiny bit confusing, hopefully not. When we make our database, we of course will have many named fields, the view allows us to search the JSON documents for that particular field, or a value in one of the fields. This file will go into the application in the views folder, in a folder named “byname” and name the file map.js, or using the command line, $couchapp generate view courses byname you will still need to edit the map file in the application as follows

 That is it What this looks for is a JSON document with the field named title, it will then ,gather this document for use. The web interface of Couchdb is an easier way to make views, we will use these the next time.

With that cliffhanger, I will quit. Next time, making the recordedit.js file, the heart of the application.

No comments: