PouchDB在不影响远程同步的情况下删除设备上的数据

bry*_*yan 5 javascript couchdb pouchdb

现在,我将整个设备数据库复制到远程数据库中。

完成此操作后,我将使用过滤器从远程数据库中获取所有不超过1个月的数据,并将其带入我的设备。

过滤

{
  _id: '_design/filters',
  "filters": {
    "device": function(doc, req) { 
      if(doc.type == "document" || doc.type == "signature") { 
        if(doc.created >= req.query.date) return true;
        else return false;
      } 
      else return true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

复制

device_db.replicate.to(remote_db)
.on('complete', function () {

  device_db.replicate.from(remote_db, {

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

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

    console.log("localtoRemoteSync replicate.to success");
    callback(true);

  });

});
Run Code Online (Sandbox Code Playgroud)

我的问题:

我希望能够定期从我的设备中删除3个月以上的数据(在我已经知道已同步的足够旧的数据中)

但是只是因为我从设备中删除了数据,所以当我将数据复制回remote_db时,我也不想在此删除它。

如何复制设备上的特定数据,但复制时不翻译该删除内容?

Ale*_*ôté 5

过滤器

在这里,我们有 2 个过滤器:

noDeleted :此过滤器不会推送_deleted文档。

device :过滤以仅获取最新数据。

{
  _id: '_design/filters',
  "filters": {
      "device": function(doc, req) {
          if (doc.type == "document" || doc.type == "signature") {
              if (doc.created >= req.query.date) return true;
              else return false;
          }
          return true;
      },
       "noDeleted": function(doc, req) {
          //Document _deleted won't pass through this filter.
          //If we delete the document locally, the delete won't be replicated to the remote DB 
          return !doc._deleted;
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

复制

device_db.replicate.to(remote_db, {
      filter: "filters/noDeleted"
  })
  .on('complete', function() {
      device_db.replicate.from(remote_db, {
              filter: "filters/device",
              query_params: { "date": (Math.floor(Date.now() / 1000) - 2419200) }
          })
          .on('complete', function() {
              console.log("localtoRemoteSync replicate.to success");
              callback(true);
          });
  });
Run Code Online (Sandbox Code Playgroud)

工作流程

  1. 您推送所有文档,而不推送已删除的文档。
  2. 您可以获得最新数据的所有更新
  3. 您删除旧文档
    • 您可以查询远程数据库以获取太旧的文档的 ID,然后在本地删除它们。请注意,文档仍将以_deleted形式存在。要完全去除它们,需要压实。
    • 您也可以在步骤 1 之后完全销毁本地数据库并从头开始。
  4. 回调(真);