Lesson 5

Introduction to PouchDB

An introduction to the basics of PouchDB


Lesson Outline

Introduction to PouchDB

We've covered the basics of what CouchDB is and why we are going to be using it to build this application, but I've also mentioned another technology called PouchDB. In this lesson, we are going to cover what PouchDB is, why we would want to use it, and how it differs to CouchDB.

Essentially, PouchDB is a JavaScript implementation of a CouchDB database. Rather than sitting on a server somewhere "in the cloud", the database is on the user's device itself. PouchDB also provides a useful API that we can use to interact with the local database.

You can run a PouchDB database in complete isolation if you want to. If all you are interested in is local data storage, but the requirements of your application would benefit from a CouchDB style document database, you can just install PouchDB and use it locally. In this scenario, it would be similar to using Ionic's Storage API, just with a lot more functionality (Ionic's Storage API is more like a simple key-value NoSQL database, whereas PouchDB would provide us with a fully featured document based database).

Although PouchDB and CouchDB are two different things, any database that uses the CouchDB protocol can be easily synced to any other database that uses the same protocol.

We can easily sync a local PouchDB database to a remote CouchDB database. Syncing means that any changes made to the local PouchDB database will automatically be reflected in the remote CouchDB database, and any changes made to the remote CouchDB database will be reflected in the local PouchDB database. This isn't just limited to two databases either, you could have any number of local PouchDB databases synced to any number of remote CouchDB databases.

To summarise:

  • PouchDB is a CouchDB style database that runs locally on the user's device. It is implemented with JavaScript and uses browser storage mechanisms. It can be used independently, or it can be used in conjunction with other remote CouchDB databases.

When building an Ionic application, the two most noticeable benefits of using a local PouchDB database (instead of just interacting with a remote CouchDB database) will likely be:

  • The ability to provide offline data access with online syncing
  • An easier API to interact with the CouchDB database – rather than making HTTP requests to the CouchDB database, we can just use PouchDB's JavaScript API and it will handle the requests for us

Another key benefit of the PouchDB/CouchDB combo is data persistence/fault tolerance. Since we can set up a remote CouchDB database that data is synced too, we don't need to worry too much about the data on a user's device being wiped - as soon as the user syncs back to the network it can start pulling the appropriate data back in. The same goes for the CouchDB database itself, if we have multiple CouchDB databases syncing to each other from multiple different locations the loss of any one (or multiple) databases isn't usually a big deal.


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).