Calvin's Maps

PouchDB: an intro

So I’ve been hacking around with PouchDB a bit lately and I’m going to explain why I think it is so awesome.

Before we discus PouchDB we’re going to need to talk about CouchDB which Pouch is based on. CouchDB is an open source nosql document database written in Erlang that grew out of Lotus Notes of all things. I’m personally a fan of CouchDB but of it’s features the ones that matters here are, data is stored in JSON, you use JavaScript to query the data, you communicate with it entirely through a RESTful interface, and something called “Master-Master Replication,” now to put that in terms my cat would understand, this means that two databases can sync without one being master and the other slave but as equals, and this done in a way such that replication can be done in real time or sporadically. Think of email on your cellphone, you can have it so that it tries to sync every x minutes which just fails if it has no reception, or you can set it up so then when you have service you get push notification and when you loose reception you just get all of the emails when you get reception again. This is similar to how Couch can work, though it can be much more complex as well. If you want to know more about Couch then CouchDB: The Definitive Guide is a good place to start.

So one of the issues with CouchDB is that Erlang…well lets just say people have mixed feelings about it, which lead to pretty quickly, CouchDB compatible Databases, Big Couch from Cloudant which you can cluster, TouchDB is a version written in Objective-C targeting embedded apps, and then we have PouchDB.

PouchDB is written in JavaScript and targets the browser, but also works with Node.js. It has a number of different back ends that it can use so at the very basic you can run this (notice the Node.js style async callbacks, they always are function which take as a first argument an err object which is undefined unless there is an error):

Which lets you access and modify a CouchDB on the same origin like so.

image

The benefits of this setup is that it works on browsers as old as Internet Explorer 8, but on the other hand this only works with an internet connection.

We can also set up one locally like this:

This uses either IndexedDB or Web SQL depending on whats available and works on on all browsers except Internet Explorer versions besides 10. This is nice because we can create a large local storage with the same API whether on Chrome, Firefox, or mobile Safari, and the API is a whole lot easier then the IndexedDB API.

Now the neat thing we can do is combine the local and remote data sources with:

Which sets a pouch db up that syncs to a local database (either IndexedDB or web SQL) which syncs to a remote CouchDB, like so:

image

If the browser looses it’s internet connection it can then still work with the local database, and it then can sync when it gets internet access again. If the browser doesn’t support IndexedDB or webSQL it just creates a direct connection to the remote Couch.

You can checkout Pouch on:

Next time, example apps.

  1. full-phone-specifications reblogged this from cwmma
  2. cwmma posted this