PouchDB Synchronization

PouchDB allows us to synchronize the database, which is stored locally with the ones stored in CouchDB. To replicate the database, we can use PouchDB.replicate(source, destination). 

Additionally, pouchDB also allows us to replicate data from the local database to the remotely stored database and also from the remotely stored database to the locally stored database by the use of replicate. to() a replicate.from() methods

//Replicating database from locally stored database to remotely stored database 

Local_DB.replicate.to(remote_DB);

//Replicating data from remotely stored database to locally stored database 

Local_DB.replicate.from(remote_DB);

Here the instances Local_DB and remote_DB are the databases stored locally and remotely, respectively.

Let's see an example where a database named local_db contains two documents, namely doc_A and doc_B which have the following information.

doc_A = {_id: '001', name: 'Anuj', age: 23, Designation: 'Programmer'}  
doc_B = {_id: '002', name: 'Dhrub', age: 24, Designation: 'Journalist'} 

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

doc1 = {_id: '101', name: 'Sutton', age: 24, Designation: 'Designer'}  
doc2 = {_id: '102', name: 'Jane', age: 25, Designation: 'Writer'}  
doc3 = {_id: '103', name: 'Gabriel', age: 26, Designation: 'Chef'}

Let's see an example to synchronize two databases that are stored in two different places by the use of replicate.to() method and replicate.from the () method.

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


//Creating local database object
var localDB = new PouchDB('local_db');


//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5000/remote_db');


//Synchronising both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successfully");

Store the above code snippet into the file, and the name is sync.js .

Next, open a node command prompt and run the following command:

C:\Users\ACER\Documents\PouchDB_Examples>node sync.js

This command will synchronize the database and show the following statement on the console:

Databases synchronized successfully
PouchDB Synchronization

To verify the synchronization on the remote database, go to the URL http://127.0.0.1:5000/_utils/#database/remote_db and select the remote_db. We can see the documents(doc_A and doc_B) were counterfeited to the remote database:

PouchDB Synchronization

Similarly, we can verify with the local database we can see it has all 5 documents as follows:

{
    id: '001',
    key: '001',
    value: { rev: '1-e35fb7c74878016a5e74de0ee9ec7aae' },
    doc: {
      name: 'Anuj',
      age: 23,
      Designation: 'Programmer',
      _id: '001',
      _rev: '1-e35fb7c74878016a5e74de0ee9ec7aae'
    }
  },
  {
    id: '002',
    key: '002',
    value: { rev: '1-ab696bca102253eacadbe79c9418126f' },
    doc: {
      name: 'Dhrub',
      age: 24,
      Designation: 'Journalist',
      _id: '002',
      _rev: '1-ab696bca102253eacadbe79c9418126f'
    }
  },
  {
    id: '101',
    key: '101',
    value: { rev: '1-f21dd7ffa68feee75c57137ce4fc59bb' },
    doc: {
      name: 'Sutton',
      age: 24,
      Designation: 'Designer',
      _id: '101',
      _rev: '1-f21dd7ffa68feee75c57137ce4fc59bb'
    }
  },
  {
    id: '102',
    key: '102',
    value: { rev: '1-cafbc5227ad3d161ee15698a62df2b6a' },
    doc: {
      name: 'Jane',
      age: 25,
      Designation: 'Writer',
      _id: '102',
      _rev: '1-cafbc5227ad3d161ee15698a62df2b6a'
    }
  },
  {
    id: '103',
    key: '103',
    value: { rev: '1-92e758dcd76930574b55296cc520eaa0' },
    doc: {
      name: 'Gabriel',
      age: 26,
      Designation: 'Chef',
      _id: '103',
      _rev: '1-92e758dcd76930574b55296cc520eaa0'
    }
  }
]

We can also write the above code by the use of the sync() method instead of using two various methods replicate. to() and replicate. from():

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


//Creating local database object
var localDB = new PouchDB('local_db');


//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5000/remote_db');


//Synchronising Remote and local databases
localDB.sync(remoteDB, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log(response);
   }
});

After running the above code, it will synchronize two databases and show the following:

{
  push: {
    ok: true,
    start_time: '2021-11-10T19:04:19.638Z',
    docs_read: 0,
    docs_written: 0,
    doc_write_failures: 0,
    errors: [],
    last_seq: 3,
    status: 'complete',
    end_time: '2021-11-10T19:04:20.126Z'
  },
  pull: {
    ok: true,
    start_time: '2021-11-10T19:04:19.639Z',
    docs_read: 1,
    docs_written: 1,
    doc_write_failures: 0,
    errors: [],
    last_seq: 4,
    status: 'complete',
    end_time: '2021-11-10T19:04:20.166Z'
  }
}