CodesJava

Easy learning with example program codes

PouchDB Replication

To make a copy of a database, the feature of Replication of PouchDB serves the best. It works both for a locally stored PouchDB instance or a remotely stored CouchDB instance.

Syntax:

PouchDB.replicate(source, target, [options])

Replicating PouchDB (Local) to CouchDB:

  • The documents doc1, doc2, and doc3 are present in the ‘Example_Database’ database in PouchDB. The documents contains the below contents. Here, we are replicating the ‘Example_Database’ database from PouchDB to CouchDB.

    [doc1, doc2, doc3] =
    [ {  id: '101',
    key: '101',
    value: { rev: '1-f56789f432154adb782da98358921a14' },
    doc:
    { name: 'Tom',
    salary: 25000,
    experience: 3,
    _id: '101',
    _rev: '1-f56789f432154adb782da98358921a14' } },
    { id: '102',
    key: '102',
    value: { rev: '1-3cacb3a45e5678d90870f4e92dc9876d' },
    doc:
    {name: 'Jerry',
    salary: 20000,
    experience: 2,
    _id: '102',
    _rev: '1-3cacb3a45e5678d90870f4e92dc9876d' } },
    { id: '103',
    key: '103',
    value: { rev: '1-afec1229a76543db973ace8fcbacd45' },
    doc:
    { name: 'Bruno',
    salary: 30000,
    experience: 4,
    _id: '103',
    _rev: '1-afec1229a76543db973ace8fcbacd45' } } ]
  • Create a file named “RemoteReplica.js” within a folder named “Examples”.
  • Add the below code to the file.
    RemoteReplica.js:

    var PouchDB = require('PouchDB');
    var localDB = 'Example_Database';
    var remoteDB = 'http://localhost:5984/Example_Database';
    PouchDB.replicate(localDB, remoteDB);
    console.log ("Database Successfully Replicated.");
  • Open the command prompt.
  • Execute the .js file.
    node RemoteReplica.js
    

    Output:

    Database Successfully Replicated.

Verification:

Open CouchDB and check for a database named “Example_Database” which is created along with all the three documents.

Replicate CouchDB to PouchDB:

  • The documents doc1, doc2, and doc3 are present in the ’employees’ database in CouchDB. The documents contains the below contents. Here, we are replicating the ’employees’ database from CouchDB to PouchDB.

    doc1 = {_id: '101', name: 'Shyam', salary: 50000, experience: 6}
    doc2 = {_id: '102', name: 'Mohan', salary: 60000, experience: 7}
    doc3 = {_id: '103', name: 'Krishna', salary: 70000, experience: 8}
  • Create a file named “RemoteReplica2.js” within a folder named “Examples”.
  • Add the below code to the file.

    RemoteReplica2.js:

    var PouchDB = require('PouchDB');
    var localdb = 'employees';
    var remotedb2 = 'http://localhost:5984/employees';
    PouchDB.replicate(employees2, localdb);
    console.log("Database Successfully Replicated.");
  • Open the command prompt.
  • Execute the .js file.
    node RemoteReplica2.js
    

    Output:

    Database Successfully Replicated.

Verification:

To verify the Database replication, follow the below steps:

  • Create a file named “VerifyReplica.js” within a folder named “Examples”.
  • Add the below code to the file.
    VerifyReplica.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('employees');
    db.allDocs({include_docs: true, attachments: true}, function(err, docs) {
    if (err) {
    return console.log(err);
    } else {
    console.log(docs.rows);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node VerifyReplica.js
    

    Output:

    [ {  id: '101',
    key: '101',
    value: { rev: '1-acb312f4321546901aada9835889ccca1' },
    doc:
    { name: 'Shyam',
    salary: 50000,
    experience: 6,
    _id: '101',
    _rev: '1-acb312f4321546901aada9835889ccca1' } },
    { id: '102',
    key: '102',
    value: { rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' },
    doc:
    {name: 'Mohan',
    salary: 60000,
    experience: 7,
    _id: '102',
    _rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' } },
    { id: '103',
    key: '103',
    value: { rev: '1-345ac229a76543db6ab5648fc987bbb5' },
    doc:
    { name: 'Krishna',
    salary: 70000,
    experience: 8,
    _id: '103',
    _rev: '1-345ac229a76543db6ab5648fc987bbb5' } } ]
Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status