One of the most important features of PouchDB is replication, i.e. you can make a copy of a database. You can replicate either a PouchDB instance stored locally or a CouchDB instance stored remotely.
Following is the syntax of replicating 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.replicate(source, target, [options])
Both the source and targets can be either PouchDB instances or CouchDB instances.
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'; //Replicating a local database to Remote PouchDB.replicate(localDB, remoteDB); console.log ("Database replicated successfully");
Save the above code in a file with name Replication_example.js. Open the command prompt and execute the JavaScript file using node as shown below.
C:\PouchDB_Examples >node Replication_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 replicated successfully
You can verify whether the database is replicated in your CouchDB instance by clicking the following link http://127.0.0.1:5984/_utils/index.html.
On clicking, you can see the list of databases in your CouchDB. You can also observe that a copy of the database sample_database is created here.
If you select the replicated database, you can view its contents as shown below.
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 Ali', 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'; //Replicating a local database to Remote PouchDB.replicate(remotedb, localdb); console.log("Database replicated successfully");
Save the above code in a file with the name Replication_example2.js. Open the command prompt and execute the JavaScript file using node as shown below.
C:\PouchDB_Examples >node Replication_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 replicated successfully
You can verify whether the database is replicated 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 replicated on executing the above code, you will get the contents of the replicated 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 Ali', 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' } } ]