PouchDB Synchronization - PouchDB

What is PouchDB - Synchronization?

The databases stored locally in PouchDB can be synchronized with those that are stored in CouchDB. In the previous chapter, we have seen how to replicate databases using PouchDB. There PouchDB.replicate(source, destination) method is used.

In addition to this, the data can also be replicated from the local database to the remote database, and from the remote database to the local database using replicate.to() and replicate.from() methods as shown below.

Here localDB is an object of database stored locally in PouchDB and remoteDB is an object of a database that is stored in CouchDB.


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

And there is a database called Remote_Database in CouchDB which contains 2 documents doc1, doc2, having contents as shown below.

An example of synchronizing these two databases, where one is stored in PouchDB and other is stored in CouchDB, using the replicate.to() and replicate.from() methods is given below.

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

With this the two databases remoteDB and localDB are synchronized, and a message is displayed on the console as shown below.

After synchronizing the two databases visit the and select the remote_database. It can be observed that the documents of local database (003, 004, 005) were copied in this database as shown below.

PouchDB Synchronization

In the same way, if you fetch the contents of the local_database stored in PouchDB it can be observed that documents of the database that are stored in CouchDB were copied here.

The above program can be re-written using the sync() method provided by PouchDB instead of the two methods replicate.to() and replicate.from() as shown below.

On executing the above program, the two databases are synchronized and the following message is displayed.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PouchDB Topics