Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

The Node.js -- Part3: CouchDB Basics

node node.js javascript basic couchdb felix-couchdb

  • Please log in to reply
No replies to this topic

#1 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 07 February 2013 - 03:48 AM

In the last tutorial we had come with the basics of a http server. There we implemented a simple http server that can respond as the client requests. The http server was able to serve each of the requested files. Today we will start learning about CouchDB database.  We will learn how to install CouchDB, how to install external node modules, the basic operations of CouchDB etc.
Check out our other node.js content!
What is couchDB?
CouchDB is a database introduced by Apache and its one of the NoSQL types. Its JSON based, there is no relational system in CouchDB that means its not a relational database management system as we see MYSQL. It only stores data in JSON file. The table concept can be replaced here by document (The JSON data file) but no relation between the documents.
Advantages of CouchDB
  • Stores data in JSON document
  • Very fast in action
  • This is not a relational database, so no need of database normalization
  • There is no limit of creating documents under a database
Installing CouchDB
Download CouchDB from here. Install it and run the database by the starting the “Start CouchDb” program. CouchDB comes with a built-in admin interface called Futon. We can see the interface on http://localhost:5984/_utils/.  There  we can create database, documents and can do all other operations in a graphical way. This interface should look like this
See there is a create database button on top left corner. We will use this to create a new database.
Installing felix-couchdb Module on Node.js
Node.js lives with node modules. There are lots of internal and external modules for different purpose. Simply node modules are function libraries are exported for use with various application. For interaction with CouchDB there a good module named
felix-couchdb. Felix-couchdb module is made for interaction between node application and CouchDB. Installing felix-couchdb is easy. Run the command in the node command prompt
Npm install felix-couchdb
Npm is Node package manager, it comes with node distribution. It handles everything about node modules like installing, updating modules type operations. After running the command in node CLI the felix-couchdb module is installed and ready to work with our node application. 
Working on CouchDB : The Futon Interface
Creating database in CouchDB is easy. Use the 'create database' button on the top left corner in the CouchDB interface to create a database. For example 'testdatabase'. After successfully database creation we can see the internal window of the 'testdatabase'.
Now we will create a new document in the 'testdatabase' database. For example we will create the document named 'testdocument'.
  • Press the 'New Document' button and the document test page is ready with a field name _id, this is the name of our document
  • Change the value of the _id as 'testdocument'
  • On the right side click on the green tick, this will temporarily save the document
  • To add new field click add field button and name the field as 'testfield' and set a value for the field testfield
  • Finally click the Save Document button on the top
  • After successfully document creation we can see our document
Notice that an extra field name _rev is automatically created, notice that the value of the field, it describes the revision number with the unique id number separated by a - hyphen mark and its very sensitive, so don't change the _rev value. We can edit the document any time by just double click on the value.
To delete the document there is a button 'Delete Document', simply use this to delete a document.
CouchDb with Node.js and felix-couchdb module
We can use this module by simply require(‘felix-couchdb’). Then we need to create a client to get interacted with our app with CouchDB. We can use password for some specific username , here is a way default way to connecting with CouchDB from Node.js application.
var  couchdb = require('felix-couchdb'),
        //if username: root password : root this will be ..createClient(5984, 'localhost','root','root');
       client = couchdb.createClient(5984, 'localhost',), 
        db = client.db('newtestdatabase');
    if (error) throw new Error(JSON.stringify(error));
    console.log('Created new db. with name '+JSON.stringify(db.name));
Run this on the CLI, if the database is created then the message will come with the database name. If any error occurred node will show the error message. So our first database is created. Now we will create a document in the newly created database.
db.saveDoc('newtestdocument', {"newtestfield" : "newtestfieldvalue"}, function(error, ok) {
    if (error) throw new Error(JSON.stringify(error));
    console.log('Saved my first doc to the couch! operation info: '+JSON.stringify(ok));

Attached Files

Edited by Roger, 19 February 2013 - 02:43 PM.
added links

  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download