Prash's Blog

First CouchApp with CouchDB April 19, 2012

Filed under: Nodejs — prazjain @ 7:09 pm
Tags: ,

CouchDB is a database that uses JSON for documents, Javascript for MapReduce queries and HTTP for API.

CouchApp is basically a Java script and Html5 application that can be deployed into CouchDB. The app is stored in
JSON format in CouchDB and served through HTTP by CouchDB. You will want to use CouchDB if you are more of a front-
end guy who needs just html and javascript to design the site, no need for server side code, just the data from
database. So your technology stack will look like Html + JavaScript + CouchDB.

Futon is a web interface for database management in CouchDB, you can access it on your machine as

Create a database by name portfoliodb, and add a few documents.
As you are adding these documents through the web interface you will notice two other properties are added automatically, “_id” and “_rev”. _id is the id for document and _rev is to track multiple revisions for the document.

  • Add sample data as below for the documents :

{ “name” : “Microsoft”, “sector” : “Technology” , “count” : 10, “price” : 30 }
{ “name” : “Google” , “sector” : “Technology” , “count” : 10, “price” : 600 }
{ “name” : “Facebook” , “sector” : “Technology” , “count” : 10, “price” : 700 }
{ “name” : “Credit Suisse” , “sector” : “Banking” , “count” : 10, “price” : 25 }
{ “name” : “Goldman Sachs” , “sector” : “Banking” , “count” : 10, “price” : 113 }

  • Now create a CouchApp
couchapp generate myapp

This will create a dir structure for the app. Move into that directory.

cd myapp
  • Now we will create Views to get documents from CouchDB.

Views use map reduce to return the list of documents, and this map reduce is done in javascript.

couchapp generate view techworth

This will create a directory techworth in views directory, inside this directory you will see two js, one is map and another reduce.

  • Change map.js file as below :
	if (doc.sector=="Technology")
		emit(,doc.count * doc.price);
  • Change reduce.js file as below :
	return sum(values);
  • Run this command to push your couch app to couchdb.
couchapp push portfoliodb

And you will see a result like this :


This tutorial is just to show how you can have javascript to pull data from database, you can access this data using myapp/techworth as JSON is your javascript in front-end. And now that we have done it, you can add a lot more on top of it to make some amazing CouchApps.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s