Robert Kowalski is a Software Engineer who loves JavaScript on the client and on the server. Robert contributes to a lot of Open Source projects, including CouchDB and npm - the node package manager. He occasionally writes tutorials in his spare time.

Robert's articles

  1. Tracking Expenses with CouchDB and Angular

    In this tutorial we will build an application using CouchDB as our backend and Angular as the front end technology of choice. CouchDB is a NoSQL database, and Angular one of the newer JavaScript MVC frameworks. The exciting and awesome thing is that CouchDB is a database with an HTTP API – our client application will talk directly to the database: the CouchDB will act as the only backend we need for our client application!

    We will focus on a small app to track our expenses. There will be a commit for every step, and sometimes the commit also includes tests. The tests will not be a topic in this tutorial, but if you are interested in it you should have a look! You will find the whole code that is used in this tutorial in a repository on GitHub.

    Why CouchDB?

    Some of you might say that we could use client side alternatives instead. IndexedDB or Local Storage are technologies that work locally on the client to persist data. But using a database server has several advantages: we can connect with many clients to our app. Your partner could update the expenses list while you are on your own in another supermarket, also adding expenses.

    Using CouchDB brings advantages: CouchDB “speaks” HTTP natively, so we will not need another layer between our database and app. Our JavaScript application can talk directly with the CouchDB database using a RESTful interface provided by CouchDB!

    And, if we would want to use replication for our database, it would be as easy as slicing bread: as CouchDB is designed for creating distributed database systems.

    Requirements

    For this Tutorial you will need to install the latest release of CouchDB (1.6) and the latest stable Node.js (currently 0.10.x) release.

    Installing Node.js & Yo

    As a Mac user you can get an official installer on the Node homepage. Another way to manage Node.js installations on Linux and OSX is the awesome nvm from Tim Caswell.

    We will install Yo to scaffold our app. Yo will ask us some questions during the process of creating our skeleton. Yo asks if we want to use SASS, and if you are unsure just answer “no” – but we definitely want to include Bootstrap and the preselected Angular-Modules.

    In our shell we type:

    [code]
    npm install -g yo generator-angular grunt-cli couchapp
    mkdir expenses && cd expenses
    yo angular expenses
    [/code]

    As part of our scaffolding, Yo created a Gruntfile (Gruntfile.js) for us. Grunt is a task runner in JavaScript with a lot of already written plugins to automate tasks and make your life easier.

    With the command grunt serve a development server gets started and http://127.0.0.1:9000 should open in a browser after the grunt tasks have finished. An example of this is shown in the following image.

    Scaffolding

    Installing CouchDB

    There are awesome docs for installing CouchDB on a lot of platforms – there are packages for all major operating systems and on OSX you can use brew to install CouchDB.

    First steps with CouchDB

    Let’s start our first CouchDB instance and create a database:

    [code]
    couchdb & # start a CouchDB
    curl -X PUT http://127.0.0.1:5984/expenses # create the database expenses
    [/code]

    CouchDB answers with:

    [js]
    {“ok”:true}
    [/js]

    We just created our first database by using HTTP!

    Let’s explore the HTTP API of CouchDB further: we can now insert the first document, let’s say we want to track some popcorn we bought (we will need these calls to CouchDB later for our application).

    [code]
    curl -X POST http://127.0.0.1:5984/expenses -H "Content-Type: application/json" -d '{"name": "Popcorn", "price": "0.99"}'
    [/code]

    CouchDB answers:

    [js]
    {“ok”:true,”id”:”39414de82e814b6e1ca754c61b000efe”,”rev”:”1-2b0a863dc254239204aa5b132fda8f58″}“
    [/js]

    We can now access the document using a GET request and the id that CouchDB assigned to our document, as we did not provide a specific id:

    [code]
    curl -X GET http://127.0.0.1:5984/expenses/39414de82e814b6e1ca754c61b000efe
    [/code]

    CouchDB answers:

    [js]
    {“_id”:”39414de82e814b6e1ca754c61b000efe”,”_rev”:”1-2b0a863dc254239204aa5b132fda8f58″,”name”:”Popcorn”,”price”:”0.99″}
    [/js]

    After that, we insert another document:

    [code]
    curl -X POST http://127.0.0.1:5984/expenses -H "Content-Type: application/json" -d '{"name": "Washing powder", "price": "2.99"}'
    [/code]