English 中文(简体)
PouchDB - Replication
  • 时间:2024-09-17

PouchDB - Reppcation


Previous Page Next Page  

One of the most important features of PouchDB is reppcation, i.e. you can make a copy of a database. You can reppcate either a PouchDB instance stored locally or a CouchDB instance stored remotely.

Syntax

Following is the syntax of reppcating a database in PouchDB. Here, a copy of the source database is the target. To this method, you can directly pass the location of source and destination databases in String format, or you can pass objects representing them.

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

Both the source and targets can be either PouchDB instances or CouchDB instances.

Reppcating LocalDB to CouchDB

Suppose there is a database with the name sample_database in PouchDB, and it contains 3 documents doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id:  001 , name:  Ram , age: 23, Designation:  Programmer } 
doc2 = {_id:  002 , name:  Robert , age: 24, Designation:  Programmer } 
doc3 = {_id:  003 , name:  Rahim , age: 25, Designation:  Programmer }

Following is an example which makes a copy of the database named sample_database that is stored locally in CouchDB.

//Requiring the package 
var PouchDB = require( PouchDB );

var localdb =  sample_database ;

//Creating remote database object 
var remotedb =  http://localhost:5984/sample_database ;

//Reppcating a local database to Remote 
PouchDB.reppcate(localDB, remoteDB); 
console.log ("Database reppcated successfully");

Save the above code in a file with name Reppcation_example.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:PouchDB_Examples >node Reppcation_example.js

This makes a copy of the database named sample_database in CouchDB instance and displays a message on the console as shown below.

Database reppcated successfully

You can verify whether the database is reppcated in your CouchDB instance by cpcking the following pnk http://127.0.0.1:5984/_utils/index.html.

On cpcking, you can see the pst of databases in your CouchDB. You can also observe that a copy of the database sample_database is created here.

Sample Database

If you select the reppcated database, you can view its contents as shown below.

Reppcated Database

Reppcating CouchDB to PouchDB

Suppose there is a database with the name Remote_Database in CouchDB and it contains 3 documents, doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id:  001 , name:  Geeta , age: 25, Designation:  Programmer }
doc2 = {_id:  002 , name:  Zara Ap , age: 24, Designation:  Manager }
doc3 = {_id:  003 , name:  Mary , age: 23, Designation:  Admin }

Following is an example which makes a copy of the database named Remote_Database that is stored in CouchDB in the local storage.

//Requiring the package
var PouchDB = require( PouchDB );

var localdb =  sample_database ;

var remotedb =  http://localhost:5984/sample_database1 ;

//Reppcating a local database to Remote
PouchDB.reppcate(remotedb, localdb);
console.log("Database reppcated successfully");

Save the above code in a file with the name Reppcation_example2.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:PouchDB_Examples >node Reppcation_example2.js

This makes a copy of the database named remote_database in PouchDB instance and displays a message on the console as shown below.

Database reppcated successfully

You can verify whether the database is reppcated in your Pouch instance by executing the following code.

//Requiring the package
var PouchDB = require( PouchDB );

//Creating the database object
var db = new PouchDB( remote_database );

//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);
   }
});

If the database is reppcated on executing the above code, you will get the contents of the reppcated database as shown below.

[ 
   { 
      id:  001 , 
      key:  001 , 
      value: { rev:  1-23cf3767e32a682c247053b16caecedb  }, 
      doc: { 
         name:  Geeta , 
         age: 25, 
         Designation:  Programmer , 
         _id:  001 ,
         _rev:  1-23cf3767e32a682c247053b16caecedb  
      } 
   }, 
   { 
      id:  002 , 
      key:  002 , 
      value: { rev:  1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79  }, 
      doc: { 
         name:  Zara Ap , 
         age: 24, 
         Designation:  Manager , 
         _id:  002 ,
         _rev:  1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79  
      } 
   }, 
   { 
      id:  003 , 
      key:  003 , 
      value: { rev:  1-c4cce025dbd30d21e40882d41842d5a4  }, 
      doc: { 
         name:  Mary , 
         age: 23, 
         Designation:  Admin , 
         _id:  003 , 
         _rev:  1-c4cce025dbd30d21e40882d41842d5a4  
      } 
   } 
]
Advertisements