PouchDB Replication - PouchDB

What is PouchDB - Replication?

Replication is one of the most important features of PouchDB, i.e. you can make a copy of a database. Either a PouchDB instance stored locally or a CouchDB instance stored remotely can be replicated.

Syntax

The syntax of replicating a database in PouchDB is given below. Here, a copy of the source database is the target. In this method, the location of source and destination databases can be directly passed in String format, or you can pass objects representing them.

The source and targets can be either PouchDB instances or CouchDB instances.

Replicating LocalDB to CouchDB

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.

An example which makes a copy of the database named sample_database that is stored locally in CouchDB is given below.

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.

With this a copy of the database named sample_database is made in CouchDB instance and a message is displayed on the console as shown below.

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, the list of databases in your CouchDB can be seen. It can also be observed that a copy of the database sample_database is created here.

PouchDB  Replication

If you select the replicated database, its contents can be viewed as shown below.

PouchDB  Replication

Replicating CouchDB to PouchDB

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.

An example to make a copy of the database named Remote_Database that is stored in CouchDB in the local storage is given below.

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

With this makes a copy of the database named remote_database in PouchDB instance is made and a message is displayed on the console as shown below.

By executing the code given below, you can verify whether the database is replicated in your Pouch instance or not .

If the database is replicated on executing the above code, you will get the contents of the replicated database as shown below.

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

PouchDB Topics