Lesson 4

Introduction to CouchDB

An introduction to the basics of CouchDB

PRO

Lesson Outline

Introduction to CouchDB

We've already introduced the concept of a NoSQL database - a database that is not relational. There are many different types of NoSQL databases, and we know that CouchDB is a document based NoSQL database. This means that it stores its data as JSON objects called "documents" that look like this:

{
    "_id": 1,
    "name": 'Josh',
    "country": 'Australia',
    "interests": ['Ionic', 'Writing', 'Travelling']
}

We're going to be working with CouchDB quite extensively in this module, so we're going to need to get a little more familiar with it than that. As we established, there are a lot of different types of NoSQL databases, but then there are even different types of document based databases as well. You would have likely heard of Firebase, and that is also a document based database, but there are a lot of differences between Firebase and CouchDB even though they are both document based.

In this lesson we are going to get acquainted with some of CouchDB's inner workings.

Syncing and Offline Data

There are many advantages to using CouchDB including the ease of which it can be scaled, and the speed of read and write operations, but the killer feature when it comes to mobile applications is its ability to synchronise between multiple databases.

Synchronising two or more databases can be astoundingly difficult, and in order to provide offline functionality in an application, we need to provide local data to the user that can be read and modified, and then later synced back to the main database or databases when a network connection becomes available.

When using CouchDB, it is trivially easy to do this. CouchDB can run on just about anything, so we could potentially have CouchDB databases all over the place all syncing with each other. If we want to allow a user access to data whilst they are offline and then sync that data when they are online, we could have one CouchDB database running locally on the device, and another CouchDB database hosted on a server somewhere.

Interacting with a CouchDB Database

A CouchDB database implements a RESTful API, which means we can interact with it using HTTP methods like GET, PUT, POST, and DELETE. If we wanted to read some data from a CouchDB database we might make a GET request to the following URL:

http://someserver.com/mydatabase/_design/posts/_view/by_date_published

to retrieve some blog posts. We can also use this RESTful API to trigger the replication functionality. All we would have to do is fire off a POST request with the following data:

{"source":"http://example.org/example-database","target":"http://admin:[email protected]:5984/example-database", "continuous":true}

We're telling CouchDB to replicate the database at the first URL to the database at the second. This is an extremely complex problem made extremely easy to implement - you can literally make one simple POST request and the job is done. I wanted to show you that you can interact with a CouchDB database directly using the HTTP API, but we will be using PouchDB (more on that later) throughout this module to help us interact with CouchDB.

When using the PouchDB library in your Ionic application, you could trigger a sync between a local database (i.e. one running on the users phone) and a remote database (running on a server) with a single line of code:

PouchDB.sync('mydb', 'http://url/to/remote/database');

This is likely a more comfortable syntax for most people, rather than manually sending off HTTP requests with JSON configurations.

Data Structure

We've already touched on the document based data structure, but now we are going to talk about it in a little more depth.

Data in CouchDB, like in many (but not all) NoSQL databases, is stored as documents and there is no pre-defined schema. Unlike a relational database where we need to describe what the structure of our data is before we insert it (a schema), and can only insert data that matches that schema, with CouchDB you can store whatever kind of data you want, whenever you want.

This doesn't mean that there is no structure to a CouchDB database, it is critically important that you add data in a way that makes sense and will perform well for your application. This creates a lot of flexibility for data storage, but it also has its downsides and it can be quite hard to learn because there is no "100% correct" way to store the data, and in many cases, it's better to do things you will probably have learned to avoid. Duplicating data is often a strategy that will work well for a document based NoSQL database, which would typically be avoided in a typical relational database.

We have already discussed that a CouchDB document is just a JSON object, but the following is a more accurate example of what a CouchDB document will actually look like:

{
    "_id": "00a271787f89c0ef2e10e88a0c0001f4",
    "_rev": "1-2628a75ac8c3abfffc8f6e30c9949fd6",
    "item": "apple",
    "prices": {
        "Fresh Mart": 1.59,
        "Price Max": 5.99,
        "Apples Express": 0.79
    }
}

As I mentioned before, we call these "documents" because that is a metaphor for what they are supposed to be – a document like an invoice, an email, or a receipt that contains all of the relevant data in one place. We can immediately see in the document above the various prices for apples at different stores, but in a relational database these would likely be split up into different tables and joined using a foreign key when necessary.

PRO

Thanks for checking out the preview of this lesson!

The full version of this lesson is only available to pro members. If you would like full access to this module and all of the other pro modules on Elite Ionic you can become a pro member (or log in if you are already a member).