跟踪pouchdb中的复制/更改

时间:2014-03-05 11:00:05

标签: couchdb replication pouchdb

我正在尝试跟踪pouchdb复制时的更改,以便创建加载屏幕。

我有几个数据库,每个数据库有2000个左右的文档,初始加载需要一段时间,所以我希望应用程序向用户显示后台发生的情况。

  //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


      }

    });
  });

上面的代码是我尝试跟踪从服务器下载到客户端的文档数量。

上面的代码可以看到文件正在下载。

但是,这只会考虑第一个负载情况,即用户何时更新文档?

这是我认为update_seq发挥作用的地方。

但我无法弄清楚如何使用update_seq来准确了解需要进行的更改。

总而言之,我可以看到是否发生了变化,但我无法看到如何获得应用程序认为自己最新所需的全部更改。

也许这种做法完全没了?

1 个答案:

答案 0 :(得分:0)

所以有一点需要注意

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

弃用赞成

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

update_seq不能用作文档数量的代理,它是从哪里复制的标记,可能也可能与文档写入一致,但是两者都doc_count复制的两面,docs_written似乎你有一个相当准确的计数器?

哦,我只是更多地阅读你的代码,你不需要调用更改,复制有一个onChange更多的信息,我们可能想要考虑使其更自动,这是一个相当常见的请求< / p>

对于超出首次加载的请求,看起来你会想要一个只需要很少量的单一更改的UI,除非你以大批量写入复制速度非常慢。