PouchDB Replication

PouchDB provides us a very prime feature Replication. It allows us to make a duplicate copy of the particular database. It offers us the feature of replicating the PouchDB object, which is stored remotely as well as stored locally.

To achieve replication, we have to use the method called replicate (), which needs two parameters: source and destination. Source and destination are the locations of the database in the format of the string, or we can also use objects which represent the database as parameters.

Below given is the syntax to use the method:

PouchDB.replicate(source_of_db, destination_of_db, [opiotns_if_any])

Both source_of_db and destination_of_db are the objects of PouchDB.

Let's see a database replication locally, i.e., PouchDB to remotely stored, known as CouchDB.

Lets see an example where we have a database named local_db in the PouchDB which contains three documents namely doc_A, doc_B, and doc_C which is created as follows:

//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('local_db');  
//Preparing the documents array  
doc_A = {_id: '001', name: 'Anuj', age: 23, Designation: 'Programmer'}  
doc_B = {_id: '002', name: 'Dhrub', age: 24, Designation: 'Journalist'}  
doc_C= {_id: '003', name: 'Nikhil', age: 25, Designation: 'Writer'}  
docs = [doc_A, doc_B, doc_C]  
//Inserting Documents  
db.bulkDocs(docs, function(err, response) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Documents created Successfully");  
   }  
});  

Let’s see how it looks

PouchDB Replication

It will contain following information:[

{
    id: '001',
    key: '001',
    value: { rev: '1-e35fb7c74878016a5e74de0ee9ec7aae' },
    doc: {
      name: 'Anuj',
      age: 23,
      Designation: 'Programmer',
      _id: '001',
      _rev: '1-e35fb7c74878016a5e74de0ee9ec7aae'
    }
  },
  {
    id: '002',
    key: '002',
    value: { rev: '1-ab696bca102253eacadbe79c9418126f' },
    doc: {
      name: 'Dhrub',
      age: 24,
      Designation: 'Journalist',
      _id: '002',
      _rev: '1-ab696bca102253eacadbe79c9418126f'
    }
  },
  {
    id: '003',
    key: '003',
    value: { rev: '1-e5eabcd16e4449ff266644655dc5a4e6' },
    doc: {
      name: 'Nikhil',
      age: 25,
      Designation: 'Writer',
      _id: '003',
      _rev: '1-e5eabcd16e4449ff266644655dc5a4e6'
    }
  }
]

After creating the database lets replicate it in CouchDB:

Now create a replication of this database in CouchDB:

//Requiring the package   
var PouchDB = require('PouchDB');  
var localDatabase = 'local_db';  
//Creating remote database object   
var remoteDatabase = 'http://localhost:5000/remote_db';  
//Replicating a local database to Remote   
PouchDB.replicate(localDatabase, remoteDatabase);   
console.log ("Database is replicated successfully to CouchDB"); 

Store the above code snippet in the file, and name it as “replicateToCouchdb”.

Next, open the node command prompt and run the file using the following command:

C:\Users\ACER\Documents\PouchDB_Examples>node replicationToCouchdb.js

This will result in the sentence saying: Database is replicated successfully to CouchDB

Output:

PouchDB Replication

To verify this, we can visit the URL http://127.0.0.1:5000/_utils/ 

And look for the database named remote_db with all the three documents present in local_db:

PouchDB Replication

Doc_A

PouchDB Replication

Doc_B

PouchDB Replication

Doc_C

PouchDB Replication

This means we have successfully replicated the data from the local to the remote database.

Replicate Remote to Local

Lets create a database remote named “details” with three docuemnts namely doc1, doc2 and doc3, which is created as follows:

//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('http://localhost:5000/Details');  
//Preparing the documents array  
doc1 = {_id: '001', name: 'Sutton', age: 24, Designation: 'Designer'}  
doc2 = {_id: '002', name: 'Jane', age: 25, Designation: 'Writer'}  
doc3 = {_id: '003', name: 'Gabriel', age: 26, Designation: 'Chef'}  
docs = [doc1, doc2, doc3]  
//Inserting Documents  
db.bulkDocs(docs, function(err, response) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Documents (Batch) created Successfully");  
   }  
});

And it looks like:

PouchDB Replication PouchDB Replication PouchDB Replication
Doc1: {
      name: 'Sutton',   age: 24,
      Designation: 'Designer',
      _id: '001',
      _rev: '1-501894a594a9c9ad0ea0ea32fb08cd2f'
    }
  },
 Doc2: {
    id: '002',
    key: '002',
    value: { rev: '1-9ceb45028d0a548c9b68dce136d4e167' },
    doc: {
      name: 'Jane',
      age: 25,
      Designation: 'Writer',
      _id: '002',
      _rev: '1-9ceb45028d0a548c9b68dce136d4e167'
    }
  },
Doc3:  {
    id: '003',
    key: '003',
    value: { rev: '1-dc1f8a1a9df2059bea462c32848b2d58' },
    doc: {
      name: 'Gabriel',
      age: 26,
      Designation: 'Chef',
      _id: '003',
      _rev: '1-dc1f8a1a9df2059bea462c32848b2d58'
    }
  }

Let's replicate this database in local server PouchDB.

//Requiring the package  
var PouchDB = require('PouchDB');  
var localdb = 'remotedb';  
var remotedb2 = 'http://localhost:5000/Details';  
//Replicating a local database to Remote  
PouchDB.replicate(remotedb2, localdb);  
console.log("Remote database replicated successfully to Local PouchDB");

Store the above file to the file and name the file as replicateToLocal.js.

Open the node command prompt and run the following command:

C:\Users\ACER\Documents\PouchDB_Examples>node replicateToLocal.js

Output:

PouchDB Replication

We can also verify the replication by use of follwong code:

//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('remotedb');  
//Retrieving all the documents in PouchDB  
db.allDocs({include_docs: true, attachments: true}, function(err, docs) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log(docs.rows);  
   }  
}); 

Store the above code snippet in the file, and name it as verifyReplication.js

Open the node command prompt and run the following command:

C:\Users\ACER\Documents\PouchDB_Examples>node verifyreplication.js
[
  {
    id: '001',
    key: '001',
    value: { rev: '1-501894a594a9c9ad0ea0ea32fb08cd2f' },
    doc: {
      name: 'Sutton',
      age: 24,
      Designation: 'Designer',
      _id: '001',
      _rev: '1-501894a594a9c9ad0ea0ea32fb08cd2f'
    }
  },
  {
    id: '002',
    key: '002',
    value: { rev: '1-9ceb45028d0a548c9b68dce136d4e167' },
    doc: {
      name: 'Jane',
      age: 25,
      Designation: 'Writer',
      _id: '002',
      _rev: '1-9ceb45028d0a548c9b68dce136d4e167'
    }
  },
  {
    id: '003',
    key: '003',
    value: { rev: '1-dc1f8a1a9df2059bea462c32848b2d58' },
    doc: {
      name: 'Gabriel',
      age: 26,
      Designation: 'Chef',
      _id: '003',
      _rev: '1-dc1f8a1a9df2059bea462c32848b2d58'
    }
  }
]

Following will be the output that shows the contents form the documents stored on the remote database.

PouchDB Replication