Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- What is NoSQL?
- Installing CouchDB on Mac, Windows, and Linux
- Storing data
- Nesting document data
- Specifying search criteria
- Reducing data
- Attaching and retrieving images
- Deploying applications
- Securing CouchDB
- Understanding the CAP theorem
Skill Level Intermediate
While CouchDB is designed so you can store and serve your entire web application from it, you maybe more comfortable with using a different programming environment for your development. Many languages and frameworks have libraries ready to connect to CouchDB. Let's take a look at how you can use Node.js to connect to CouchDB. You can download Node.js from nodejs.org. For more information on installing and using Node.js refer to other lynda.com online training library titles.
Let's go to the exercise files. There is an application folder inside the exercise files. Let's copy this folder to the Desktop. Now let's take a look at the contents of this folder. If you open package.json you'll notice cradle is listed as a dependency. Cradle is a module that you can use to connect to the CouchDB. Let's install this dependency now. I'm going to Applications > Utilities > Terminal, and then I'm changing directory to the application folder.
Once you're inside the application folder, type npm install. Now cradle and all of its dependencies are installed. Now let's take a look at db.js. Db.js requires the cradle module, then it uses the cradle module to create a connection to the CouchDB instance at 127.0.0.1. We also supply the port number as well as the username and password.
Finally we tell cradle to use restaurant as the database. Then we set the db object as our export. Now let's take a look at list_items.js. This file first includes db.js by using ./db. With that object we can then query the view. This function will query the menu/items view and then log every result to the console. Let's run list_items.js now.
Go back to Terminal and type node list_items.js. Cradle retrieved all the information from this view and then we logged every result to the console. Let's take a look at get_delivery.js. Here we have the hash for the delivery page, and then inside the callback function we're logging the result to the console. Let's run this script now, type in node get_delivery.js.
Finally, let's take a look at doing a save. Open save.js. Again, we're requiring the DB module, then we're calling the save function. As the first argument, we're passing an object representing the document that we want to store in the database. Notice that we're not supplying an ID. The Cradle module will automatically let couch supply an ID for us, and then it will return that ID as a part of the response.
Let's run this script now. Type in node and save.js. So the response came back saying that the save was successful and then it also gave us the first revision hash, as well as the ID for the document. Let's look up this document in the CouchDB Futon interface. I'm in Futon and I'm navigating to restaurant. You might have to navigate to the next page. Notice that this document has the name and category we specified in the Node.js script.
The Cradle module for Node.js has everything you need to start working with a CouchDB database. The Cradle Module makes all of the HTTP requests for you so you don't have to write them manually.