【问题标题】:Pouchdb filtered replication for large database大型数据库的 Pouchdb 过滤复制
【发布时间】:2018-02-16 17:26:25
【问题描述】:

以下是我编写的一项服务,用于尝试将远程 couchdb 位置的数据同步到设备。但是,我遇到的最大问题是只想检索具有type: document 的数据(如果它在时间戳期间)。

现在,我很确定它会抓取每个文档,然后将其过滤掉。

我想在它通过之前先过滤掉它,因为我有很多文档。

有谁知道我是怎么做到的?

.service("$pouchDB", ["$rootScope", "$q", "$cordovaNetwork", "$state", function($rootScope, $q, $cordovaNetwork, $state) {

  var self = this;

  self.db = new PouchDB("IDHERE", {auto_compaction: true});

  self.remoteToDevice = function(s) {
      var remote_db = new PouchDB('https://SOMETHING@URL/IDHERE', {ajax: {timeout: 180000}});

      return self.db.replicate.from(remote_db, {

        filter: function (doc) {

            if(doc.type == 'document')
              if(doc.timestamp >= (Math.floor(Date.now() / 1000)-2419200)) 
                return doc;
            else return doc;
        }

      }).on('complete', function () {

        console.log("Remote to Device - Success");

      })
      .on('error', function (err) {

        console.log("Remote to Device - Error", JSON.stringify(err));
        return err;

      });

  }

}

编辑:

感谢 Alexis,这是我认为可行的解决方案

新增remoteToDevice过滤功能

{   
  filter: "filters/device", 
  query_params: { 
    "timestamp": (Math.floor(Date.now() / 1000)-2419200) 
  }
}

couchdb 中的过滤功能

"filters": {
    "device": "function(doc, req) { 
        if(doc.type == \"document\") { 
            if(doc.timestamp >= req.query.timestamp) return true; 
            else return false;
        } 
        else return true;  
    }"
}

【问题讨论】:

    标签: javascript cordova ionic-framework couchdb pouchdb


    【解决方案1】:

    您应该在 CouchDB 的设计文档中定义过滤器函数。

    复制时,您需要指定过滤器名称。

    完整文档在这里:https://pouchdb.com/api.html#filtered-replication

    【讨论】:

    • 谢谢亚历克西斯,我已经根据您的链接使用解决方案(我相信它有效)编辑了我的问题。我很感激;一定错过了那个文件
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2018-07-07
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多