Quite possibly the main highlights of PouchDB is replication, for example you can make a duplicate of a data set. You can repeat either a PouchDB occasion put away locally or a CouchDB occurrence put away distantly.
Syntax
Following is the language structure of imitating a data set in PouchDB. Here, a duplicate of the source data set is the objective. To this technique, you can straightforwardly pass the area of source and objective information bases in String organization, or you can pass objects speaking to them.
PouchDB.replicate(source, target, [options])
Both the source and targets can be either PouchDB occasions or CouchDB cases.
Replicating LocalDB to CouchDB
Assume there is an information base with the name sample_database in PouchDB, and it contains 3 reports doc1, doc2, and doc3, having substance as demonstrated beneath.
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 a model which makes a duplicate of the data set named sample_database that is put away 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 document with name Replication_example.js. Open the order incite and execute the JavaScript record utilizing hub as demonstrated beneath.
C:\PouchDB_Examples >node Replication_example.js
This makes a duplicate of the information base named sample_database in CouchDB occasion and shows a message on the reassure as demonstrated underneath.
Database replicated successfully
You can confirm whether the data set is recreated in your CouchDB example by tapping the accompanying connection http://127.0.0.1:5984/_utils/index.html.
On clicking, you can see the rundown of information bases in your CouchDB. You can likewise see that a duplicate of the data set sample_database is made here.
On the off chance that you select the reproduced information base, you can see its substance as demonstrated beneath.
Replicating CouchDB to PouchDB
Assume there is a data set with the name Remote_Database in CouchDB and it contains 3 reports, doc1, doc2, and doc3, having substance as demonstrated underneath.
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 a model which makes a duplicate of the information base named Remote_Database that is put away in CouchDB in the nearby stockpiling.
//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 record with the name Replication_example2.js. Open the order provoke and execute the JavaScript record utilizing hub as demonstrated beneath.
C:\PouchDB_Examples >node Replication_example2.js
This makes a duplicate of the data set named remote_database in PouchDB example and showcases a message on the reassure as demonstrated beneath.
Database replicated successfully
You can confirm whether the information base is repeated in your Pouch example by executing the accompanying 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);
}
});
In the event that the data set is duplicated on executing the above code, you will get the substance of the recreated information base as demonstrated underneath.
[
{
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'
}
}
]