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

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

问题描述

处理单页应用程序时,我必须编写大量样板代码才能与服务器端数据同步.

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.

我不明白的是,在数据库太大而无法完全放入浏览器内存的情况下,Pouch 是否适合作为数据库代理.

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.

据我所知,Pouch 可以复制整个远程数据库,因此可以在整个数据库适合浏览器内存的情况下使用.

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 同步不会真正执行您希望它执行的操作.它旨在使用服务器端设计文档同步整个数据库或数据库的预定义子集.

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: query, endkey: query + '\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 的跨浏览器支持的好处.所以我不认为这是一个糟糕的方法.

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天全站免登陆