PouchDB Replication

Replication is a very important feature of PouchDB. It facilitates you to make a copy of a database. You can replicate either a PouchDB instance stored locally or a CouchDB instance stored remotely.

While using this method, you can directly pass the location of source and destination databases in String format, or you can pass objects representing them.

Syntax

Both the source and targets can be either PouchDB instances or CouchDB instances.


Replicating PouchDB (Local) to CouchDB

Let's take a database named "sample_database" in PouchDB which have 3 documents doc1, doc2, and doc3, having following content.

PouchDB Replication 1

Now create a replication of this database in CouchDB:

Save the above code in a file named "Remote_Replication.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output

PouchDB Replication 2

Verification

You can open CouchDB link and see that a database "sample_database" is created.

Image: Replication3

You can also see all three documents here:

PouchDB Replication 3
PouchDB Replication 4
PouchDB Replication 5
PouchDB Replication 6

Replicate CouchDB to PouchDB

Let's create a database named "remotedb" on CouchDB server with three documents doc1, doc2, and doc3, having contents as following:

PouchDB Replication 7

Let's replicate this database in local server PouchDB.

Save the above code in a file named "Local_Replication.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

PouchDB Replication 8

You can verify if the database is replicated in your Pouch instance by using the following code.

Save the above code in a file named "Verify_Replication.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output

PouchDB Replication 9
Next Topic#




Latest Courses