Getting Started with PouchDB to Replace Parse

by | Feb 22, 2016

With the demise of Parse, I had to be receptive to alternatives. Luckily I had already been suggested a contender, PouchDB. Theoretically, the first thing needed is to create a JSON payload with some important data you need to replicate from the client.

Now, where is this data going to be stored?

If you are already using indexedDB for client-side storage of JSON documents, PouchDB could make storing that data on the server a whole lot simpler.

Even for strictly local development, the system seems quite useful.

cap-theorem

CAP theorem, named after the three guarantees that are supposed to be impossible to achieve simultaneously in distributed systems, can help show where mongoDB and CouchDB differ.

Practically this means the PouchDB/CouchDB is updated in a more distributed, eventually consistent manner. Whereas MongoDB has a central node that coordinates all reads and writes to the secondary stores.

So what is PouchDB? It’s an in-browser database, with the same API as CouchDB, but as suggested by the name, a portable version. Even npm runs using CouchDB. But that was a relatively recent 2014.

It uses a REST like API. So mainly you will be using GET, PUT, POST, HEAD, DELETE to interact with your data.

Now to get started with your project:

npm init

npm i pouchdb —save

then require,

var

 PouchDB = require('pouchdb');

var

 db = new PouchDB('Local_database');

A unique _id property is necessary on each object. Any new data set on objects with the same _id property will merely update that object.

First set up the listener and add data. Listen for changes on the db object:

db.changes({

since: 'now',

live: true

}).on('change', showData);

Add the data to the db object:

function

addTitle(title){

var

data = {

_id: Date.now(),

title: title,

completed: false

};

db.put(data,

function

cb(err, result){

if

 (!err){

console.

log

('error: ', err);

}

})

}

Once you put some JSON data into the pouch db instance, to retrieve that data just use an accessor method on the db instance, like allDocs.

function

showData(){

db.allDocs(

{include_docs: true, descending: true},

function

(err, doc) {

console.log(doc.rows);});

};

Now that you have verified that data has been added, sync the data with something you need at least a local pouchDB database and a remote couchDB database.

To test syncing you need to have setup CouchDB. From the Futon admin interface, the data can be seen syncing.

replicate

To replicate a PouchDB you need a CouchDB instance. you can download it from http://couchdb.apache.org/ .

or use http://www.iriscouch.com/ .

 

 

cors

Couched needs to be CORS enabled.

The methods GET, PUT, POST, HEAD, DELETE need to be added in the core config.

You can go to http://localhost:5984/_utils/config.html add a new section then set credentials to true;

headers to accept, authorization, content-type, origin, referer;

methods to GET, PUT, POST, HEAD, DELETE;

origins

origins to * .

Lastly make sure the config key the httpd is set to true for the enable_cors option.

Now that CouchDB is setup you try syncing

 

var

CouchDB_URI = 'http://localhost:5984/pouch';

function

testAddData(){

addTitle(“Title-“+ Date.now());

db.sync(CouchDB_URI);

}

testAddData();

and what you get is:

Another benefit of using pouchDB for files is that it can handle images and store them to blob for you as well as syncing it.

Lastly, if you were worried about migrations, don’t. New versions of PouchDB auto migrate so that your PouchDB can be accessed from the new version of PouchDB.

With pouchdb syncing data becomes quite simple. It’s just a matter of locating the path to your remote db and the name of the local db. Then it just 2 replicate calls away from syncing the data which you can bypass with a single sync call.

Cited

  1. https://pouchdb.com/getting-started.html
  2. https://pouchdb.com/2014/07/25/pouchdb-levels-up.html
  3. http://db-engines.com/en/system/CouchDB%3BPouchDB%3BSequoiadb
  4. https://pouchdb.com/2015/09/01/pouchdb-4.0.1-gotta-go-fast.html

Recent Posts