Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

Tracking Replication / Changes in pouchdb

$
0
0

I'm trying to track the changes when pouchdb replicates in order to create a loading screen.

I have several databases with a 2000 or so documents in each and initial load takes quite a while so I want the app to give an indication to the user what is happening in the background.

  //define vars
  var nameDB = "location";
  var remoteDB = new PouchDB("http://localhost:5984/" + nameDB);
  var localDB = new PouchDB(nameDB);
  var localdocCount;
  var localdocCounter = 0;
  var remotedocCount;

  //wrap replication in reusable function
  function replicate(){
    //replicate TO the server
    PouchDB.replicate(localDB,remoteDB, {
      continuous: false,
      attachments: true
    },
    function(){

        //on callback replicate FROM server
        PouchDB.replicate(remoteDB,localDB, {
          continuous: false,
          attachments: true
        },
        function(){
           //replicate FROM complete
        );
    });

    //compact local database to keep size down
    //all revisions are replicated to the server 
    //no need to keep them locally
    localDB.compact();
  }

  //get the remote db info first and see how many documents it has
  //remote update_seq how does it match up to localdb update_seq?
  remoteDB.info(function(err, info) {
    remotedocCount = info.doc_count;
    console.log(info);
  });

  //get info for local db for comparison with remote
  localDB.info(function(err, info) {

    localdocCount = info.doc_count;

    //Actually run the replication now that we have the 
    //remote and local db info
    replicate();

    //track any changes to the localdb
    localDB.changes({
      continuous: true,
      onChange: function(){

        //change detected increment counter
        localdocCounter++;

        console.log("Downloaded " 
          + (localdocCount + localdocCounter) 
          + " of " + remotedocCount + " to " + nameDB);

        //localdocCount === The total Documents in the local database
        //localdocCounter === The changes that have been made since
        //remotedocCount === The amount of documents we need


      }

    });
  });

The code above is my attempt at tracking the amount of documents being downloaded from the server to the client.

The code above works I can see that the documents are downloading.

However this only takes care of a first load scenario what about when a user updates a document?

This is where I think update_seq comes in play.

But I cant figure out how to use update_seq to get an accurate idea of the changes that need to take place.

So to summarise I can see if changes are taking place, but I cant see how to get a total amount of changes needed for the application to consider itself up to date.

Maybe this approach is completely off?


Viewing all articles
Browse latest Browse all 17268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>