Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PockDB-惰性地获取和复制文档_Javascript_Couchdb_Pouchdb - Fatal编程技术网

Javascript PockDB-惰性地获取和复制文档

Javascript PockDB-惰性地获取和复制文档,javascript,couchdb,pouchdb,Javascript,Couchdb,Pouchdb,TL;DR:我想要一个类似于余烬数据的数据库:首先从本地存储获取数据,如果找不到,则转到远程。在这两种情况下都只复制该文档 在我的PockDB/CouchDB服务器中有一个名为Post的文档类型。我希望PockDB查看本地存储,如果它有文档,请返回文档并开始复制。如果没有,请转到远程CouchDB服务器,获取文档,将其存储在本地数据库实例中,然后开始仅复制该文档在这种情况下,我不想复制整个数据库,只复制用户已经获取的内容。 我可以通过写这样的东西来实现: var local = new Pouc

TL;DR:我想要一个类似于余烬数据的数据库:首先从本地存储获取数据,如果找不到,则转到远程。在这两种情况下都只复制该文档

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

我可以通过写这样的东西来实现:

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;
  });
}
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;
  });
}
这也不能解决复制问题,但这是我想做的事情的一般方向


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

不幸的是,您所描述的并不完全存在(至少作为内置函数)。使用上面的代码,您肯定可以从本地返回到远程(顺便说一下:),但是
local.put()

您应该能够使用
{revs:true}
获取文档及其修订历史记录,然后使用
{new\u edits:false}
插入以正确复制缺少的文档,同时保留修订历史记录(这是复制器在引擎盖下所做的)。看起来是这样的:

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;
  });
}
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;
  });
}

那应该行!如果有帮助,请告诉我。

谢谢!到目前为止,这对我是有效的。我想知道是否有办法让它活下来?我应该让我原来的问题具体说明一下,对不起,没关系!看起来我可以使用doc_id选项是的,doc_id完全符合您的要求;我想这就是你说的“活着”的意思吧?如中所示,一旦用户请求文档,将其添加到“所需”文档ID列表中,然后永远复制这些文档ID?