可以PouchDB代理客户端上的一个大的数据库? [英] Can PouchDB proxy a big database on the client side?

查看:99
本文介绍了可以PouchDB代理客户端上的一个大的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单页的应用程序工作,我必须写很多的样板code,以便与服务器端的数据同步。

Working on single page applications i have to write a lot of boilerplate code in order to synchronise with the server side data.

PouchDB 提供了一个优雅的解决了这个问题,允许本地访问在客户端的数据。

PouchDB offers an elegant solution to this problem allowing to access the data locally on the client side.

我不明白,是袋是否适合作为数据库代理与否,在情况下,当数据库太大,无法完全适合在浏览器内存。

What i don't understand, is whether Pouch is suitable as a database proxy or not, in cases when the database is too big to fully fit in the browser memory.

据我可以读,袋作品复制整个远程数据库,从而可以只在这些情况下使用时,整个数据库在浏览器内存的容量。

As far as i can read, Pouch works duplicating a whole remote database, and thus can be used just in those cases when the whole database fits in the browser memory.

让我们说,我有所有的维基百科文章的数据库,我想在客户端操纵它们的一部分。复制是不是要走的路,所需要的是代理方式。例如,当一个查询是在客户端侧使用当地,只是匹配结果应该转移。它是不可行的只是对复制值运行查询,因为它是不可能的本地复制整个数据库。

Let's say that i have a database with all Wikipedia articles and i want to manipulate part of them on the client side. Replication is not the way to go, what is needed is proxing. For example when a query is issued locally in the client side, just the matching results should be transferred. It is not feasible to run a query just on the replicated values, because it is not possible to replicate the whole database locally.

推荐答案

您说得对,PouchDB同步​​不会真的做你想要它做的事情。它的设计同步整个数据库或$ P $使用服务器端的设计文档pdefined数据库的子集。

You're right that PouchDB sync wouldn't really do what you want it to do. It's designed to sync entire databases, or predefined subsets of a database using server-side design docs.

如果我是你,我可能会仍然使用PouchDB,但我会手动处理的同步。事情是这样的:

If I were you, I would probably still use PouchDB, but I would handle the syncing manually. Something like this:

var localDB = new PouchDB('localDB');
var remoteDB = new PouchDB('http://some-site.com:5984/somedb');

function searchForDocs(docId) {
  // try the local DB first
  localDB.get(docId).catch(function (err) {
    if (err.name !== 'not_found') {
      throw error;
    }
    // not found, so fall back to the remote DB
    return remoteDB.get(docId).then(function (doc) {
      // cache in the local DB
      delete doc._rev;
      return localDB.put(doc).then(function () {
        return doc;
      });
    });
  }).then(function (doc) {
    // do something with our doc
  }).catch(function (err) {
    // handle any errors along the way
  });
}

使用的get()在这里有点简单化;在维基百科的情况下,你可能会想要做的 allDocs({startkey:查询,endkey:查询+\\ uffff'})查找ID开始一个查询的所有文档。或者你可以使用辅助索引。

Using get() is a little simplistic here; in your Wikipedia case you would probably want to do allDocs({startkey: query, endkey: query + '\uffff'}) to find all docs whose ID start with a query. Or you could use a secondary index.

所以,虽然你不会得到PouchDB内置的同步,你得到的是能写对服务器作为客户端,再加上PouchDB的跨浏览器支持相同的code的好处的好处。因此,我不认为这是一个糟糕的方​​式去了解它。

So although you wouldn't be getting the benefits of PouchDB's built-in sync, you are getting the benefits of being able to write the same code against the server as the client, plus PouchDB's cross-browser support. So I don't think this is a bad way to go about it.

这篇关于可以PouchDB代理客户端上的一个大的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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