PouchDB Replication




PouchDB Replication

  • Replication means make a copy of a database. We can replicate either a CouchDB instance stored remotely or a PouchDB instance stored locally.

Syntax

PouchDB.replicate(source, target, [options])

Replicating PouchDB (Local) to CouchDB

  • Let's take a database named "sample_database" in PouchDB which have 3 documents doc1, doc2, and doc3, having following content.
{
  "_id": "001",
  "name": "Admin",
  "age": "23",
  "Designation": "Programmer",
  "_rev": "1-a87843ccb443c46028a7704a82de00fc"
}
{
  "_id": "002",
  "name": "nisha",
  "age": "24",
  "Designation": "Teacher",
  "_rev": "1-ed94f5772105ae15ae508a46f19f9941"
}
{
  "_id": "003",
  "name": "Asha",
  "age": "25",
  "Designation": "Mechanic",
  "_rev": "1-513742e37a5cb67c2cb017f04fa329d8"
}

Read Also

  • Now create a replication of this database in CouchDB:
//Requiring the package   
var PouchDB = require('PouchDB');  
var localDB = 'sample_database';  
//Creating remote database object   
var remoteDB = 'http://localhost:5984/sample_database';  
//Replicating a local database to Remote   
PouchDB.replicate(localDB, remoteDB);   
console.log ("Database replicated successfully");  

Save the file name as "Remote_Replication.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node Remote_Replication.js  

Output

 PouchDB Replication

PouchDB Replication

Verification

  • You can open CouchDB link and see that a database "sample_database" is created.
 PouchDB Replication

PouchDB Replication

 PouchDB Replication

PouchDB Replication

 PouchDB Replication

PouchDB Replication

 PouchDB Replication

PouchDB Replication

Replicate CouchDB to PouchDB

  • Let's create a database named "remote_database" on CouchDB server with three documents doc1, doc2, and doc3, having contents as following:
doc1 = {_id: '001', name: 'princy', age: 20, Designation: 'Teacher'}  
doc2 = {_id: '002', name: 'priya', age: 24, Designation: 'Lawyer'}  
doc3 = {_id: '003', name: 'John', age: 36, Designation: 'Plumber'}  
 PouchDB Replication

PouchDB Replication

  • Let's replicate this database in local server PouchDB.
//Requiring the package  
var PouchDB = require('PouchDB');  
var localdb = 'remote_database';  
var remotedb2 = 'http://localhost:5984/remote_database';  
//Replicating a local database to Remote  
PouchDB.replicate(remotdb, localdb);  
console.log("Database replicated successfully to PouchDB");  

Save the file name as "Local_Replication.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node Local_Replication.js  

Output:

 PouchDB Replication

PouchDB Replication

  • You can verify if the database is replicated in your Pouch instance by using the following 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);  
   }  
});  
  • .Open the command prompt and execute the JavaScript file using node:
node Verify_Replication.js  

Output

 PouchDB Replication

PouchDB Replication

[
  {
id: '001',
key: '001',
value: { rev: '1-c1b32f27861dc6b2f16c22baf6547c9a' },
doc: {
name: 'princy',
age: '20',
      Designation: 'Teacher',
      _id: '001',
      _rev: '1-c1b32f27861dc6b2f16c22baf6547c9a'
    }
  },
  {
id: '002',
key: '002',
value: { rev: '1-57098ae011ecba8a95d5e42c1ec4ebb5' },
doc: {
name: 'priya',
age: '24',
      Designation: 'Lawyer',
      _id: '002',
      _rev: '1-57098ae011ecba8a95d5e42c1ec4ebb5'
    }
  },
  {
id: '003',
key: '003',
value: { rev: '1-1351db822f6565cee5ad7bbb08c6d27f' },
doc: {
name: 'John',
age: '36',
      Designation: 'Plumber',
      _id: '003',
      _rev: '1-1351db822f6565cee5ad7bbb08c6d27f'
    }
  }
]


Related Searches to PouchDB Replication