You can synchronize the data sets put away locally in PouchDB with those that are put away in CouchDB. In the past part, we have perceived how to repeat data sets utilizing PouchDB. There we have utilized the technique PouchDB.replicate(source, objective).
Moreover, we can likewise duplicate the information, from the nearby data set to the far off data set, and from the distant data set to the neighborhood data set utilizing replicate.to() and replicate.from() strategies as demonstrated beneath.
//Replicating data from local database to remote database
localDB.replicate.to(remoteDB);
//Replicating data from remote database to local database
localDB.replicate.from(remoteDB);
Where, localDB is an object of information base put away locally in PouchDB and remoteDB is an object of a data set that is put away in CouchDB.
Example
Assume there is an information base with the name local_database in PouchDB, and it contains 3 archives, doc1, doc2, and doc3, having substance as demonstrated beneath.
doc1 = {_id: '003', name: 'Ram', age: 26, Designation: 'Programmer'}
doc2 = {_id: '004', name: 'Robert', age: 27, Designation: 'Programmer'}
doc3 = {_id: '005', name: 'Rahim', age: 28, Designation: 'Programmer'}
Also, there is a data set with the name Remote_Database in CouchDB and it contains 2 reports doc1, doc2, having substance as demonstrated underneath.
doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}
Following is an illustration of synchronizing these two data sets, where one is put away in PouchDB and other is put away in CouchDB, utilizing the replicate.to() and replicate.from() techniques.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating local database object
var localDB = new PouchDB('local_database');
//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5984/remote_database');
//Synchronising both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successfully");
Save the above code in a document with the name Synchronising_databases.js. Open the order incite and execute the JavaScript record utilizing hub as demonstrated underneath.
C:\PouchDB_Examples >node Synchronising_databases.js
This synchronizes the two data sets remoteDB and localDB, and showcases a message on the comfort as demonstrated underneath.
Databases synchronized successfully.
Subsequent to synchronizing the two information bases visit the http://127.0.0.1:5984/_utils/index.html and select the remote_database. You can see that the archives of nearby information base (003, 004, 005) were replicated in this data set as demonstrated underneath.
Similarly, in the event that you bring the substance of the local_database put away in PouchDB you can will see that reports of the data set that is put away in CouchDB were duplicated here.
[
{
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-bf4619471ac346fdde46cfa8fbf3587f' },
doc: {
name: 'Ram',
age: 26,
Designation: 'Programmer',
_id: '003',
_rev: '1-bf4619471ac346fdde46cfa8fbf3587f'
}
},
{
id: '004',
key: '004',
value: { rev: '1-29b8f803958c994e3eb37912a45d869c' },
doc: {
name: 'Robert',
age: 27,
Designation: 'Programmer',
_id: '004',
_rev: '1-29b8f803958c994e3eb37912a45d869c'
}
},
{
id: '005',
key: '005',
value: { rev: '1-0eb89f71998ffa8430a640fdb081abd2' },
doc: {
name: 'Rahim',
age: 28,
Designation: 'Programmer',
_id: '005',
_rev: '1-0eb89f71998ffa8430a640fdb081abd2'
}
}
]
You can change the above program utilizing the sync() technique given by PouchDB rather than the two strategies replicate.to() and replicate.from() as demonstrated underneath.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating local database object
var localDB = new PouchDB('local');
//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5984/remote_database');
//Synchronising Remote and local databases
localDB.sync(remoteDB, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log(response);
}
});
On executing the above program, it synchronizes the two information bases showing the accompanying message.
{
push: {
ok: true,
start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
docs_read: 6,
docs_written: 6,
doc_write_failures: 0,
errors: [],
last_seq: 10,
status: 'complete',
end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
},
pull: {
ok: true,
start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
docs_read: 0,
docs_written: 0,
doc_write_failures: 0,
errors: [],
last_seq: 2,
status: 'complete',
end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
}
}