PouchDB-懒惰地获取和复制文档 [英] PouchDB - Lazily fetch and replicate documents

查看:54
本文介绍了PouchDB-懒惰地获取和复制文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:我想要一个行为类似Ember Data的PouchDB数据库:首先从本地存储中获取数据,如果找不到,请转到远程数据库.在两种情况下都只复制该文档.

TL;DR: I want a PouchDB db that acts like Ember Data: fetch from the local store first, and if not found, go to the remote. Replicate only that document in both cases.

我的PouchDB/CouchDB服务器中只有一个文档类型,称为 Post .我希望PouchDB查看本地存储,如果它具有文档,请返回该文档并开始复制.如果不是,请转到远程CouchDB服务器,获取文档,将其存储在本地PouchDB实例中,然后开始仅复制该文档.在这种情况下,我不想复制整个数据库,只复制用户已经获取的内容.

I have a single document type called Post in my PouchDB/CouchDB servers. I want PouchDB to look at the local store, and if it has the document, return the document and start replicating. If not, go to the remote CouchDB server, fetch the document, store it in the local PouchDB instance, then start replicating only that document. I don't want to replicate the entire DB in this case, only things the user has already fetched.

我可以这样写:

var local = new PouchDB('local');
var remote = new PouchDB('http://localhost:5984/posts');

function getDocument(id) {
  return local.get(id).catch(function(err) {
    if (err.status === 404) {
      return remote.get(id).then(function(doc) {
        return local.put(id);
      });
    }
    throw error;
  });
}

这也不能解决复制问题,但这是我想要做的一般方向.

This doesn't handle the replication issue either, but it's the general direction of what I want to do.

我猜我可以自己编写这段代码,但是我想知道是否有一些内置方法可以做到这一点.

I can write this code myself I guess, but I'm wondering if there's some built-in way to do this.

推荐答案

很遗憾,您所描述的内容并不存在(至少作为内置函数).您绝对可以使用上面的代码从本地回退到远程(完美的BTW :)),但是 local.put()会给您带来问题,因为本地文档的结尾会有所不同 _rev 比远程文档要晚一些,这可能会导致复制稍后混乱(这将被解释为冲突).

Unfortunately what you describe doesn't quite exist (at least as a built-in function). You can definitely fall back from local to remote using the code above (which is perfect BTW :)), but local.put() will give you problems, because the local doc will end up with a different _rev than the remote doc, which could mess with replication later on down the line (it would be interpreted as a conflict).

您应该能够使用 {revs:true} 来获取带有修订历史记录的文档,然后使用 {new_edits:false} 插入以正确复制丢失的文档doc,同时保留修订历史记录(这是复制器在后台执行的操作).看起来像这样:

You should be able to use {revs: true} to fetch the doc with its revision history, then insert with {new_edits: false} to properly replicate the missing doc, while preserving revision history (this is what the replicator does under the hood). That would look like this:

var local = new PouchDB('local');
var remote = new PouchDB('http://localhost:5984/posts');

function getDocument(id) {
  return local.get(id).catch(function(err) {
    if (err.status === 404) {
      // revs: true gives us the critical "_revisions" object,
      // which contains the revision history metadata
      return remote.get(id, {revs: true}).then(function(doc) {
        // new_edits: false inserts the doc while preserving revision
        // history, which is equivalent to what replication does
        return local.bulkDocs([doc], {new_edits: false});
      }).then(function () {
        return local.get(id); // finally, return the doc to the user
      });
    }
    throw error;
  });
}

应该可以!让我知道是否有帮助.

That should work! Let me know if that helps.

这篇关于PouchDB-懒惰地获取和复制文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆