使用 CouchDB/PouchDB 时的应用架构

Architecture of an app when using CouchDB/PouchDB

我想知道在移动应用程序中使用 PouchDB 作为本地存储而不是 localStorage 时架构应该是什么样子。

此时我习惯于将我的应用程序数据缓存到localStorage中,并在需要时对后端执行API调用以请求或post数据。后端持有所有逻辑。如:

然后所有数据都存储到关系数据库中。我现在一直在阅读有关 NoSQL 数据库的文章,尤其是 CouchDBPouchDB。所以我想知道这个架构会是什么样子?此时我出现了三个问题:

  1. 如果我有多个用户拥有自己的身份验证,我如何才能确保这些用户只能访问他们的数据?我在服务器端还会有 1 个数据库吗?
  2. 客户端
  3. PouchDB 可以与远程 PouchDB 同步。但是,当使用 Javascript 构建应用程序时,您如何确保人们不会通过 'hacking' 客户端 Javascript 将数据插入 PouchDB
  4. 在这些类型的设置中,后端的使用会消失吗?如果您想为 3rd party 使用 API,您只需在 CouchDB?
  5. 周围放置一个 Sails.js 后端

这里是 PouchDB 维护者,很高兴回答您的问题。 :)

If I have multiple users with there own authentication, how can I make sure that the users get access to only their data? And will I still have 1 database on server end?

the pouchdb-authentication README 中有指南。 Cloudant 和 Couchbase 也有自己的用户管理系统。

PouchDB on the client side can be in sync with a remote PouchDB. But when an application is build with Javascript how do you make sure that people are not inserting data into PouchDB by 'hacking' the client-side Javascript?

你在服务器端写了一个validate_doc_update函数。当 PouchDB 尝试同步到 CouchDB 时,任何失败的文档都会发出 'denied' 事件,并且这些文档不会同步。

至于本地数据库,你不能阻止用户写坏数据(他们总是可以打开控制台做任何他们想做的事),但是你可以使用像pouchdb-validation这样的东西来重新使用你的客户端验证功能。或者您可以在 put() 文档时自己做。

Would the use of a backend be gone in these kinds of setups? And if you want to have an API for 3rd party, you just put for example an Sails.js backend around the CouchDB?

有些人在没有任何后端的情况下编写 PouchDB 应用程序(仅使用纯 CouchDB/Cloudant/Couchbase),而其他人则喜欢将数据库与他们选择的服务器架构混合使用。由你决定。 :)

如果您使用 Express,一个有趣的解决方案是使用 express-pouchdb,这样您就可以在现有架构中公开一个类似 PouchDB 服务器的端点。但是,对于 Sails.js,您必须自己编写。

PouchDB on the client side can be in sync with a remote PouchDB. But when an application is build with Javascript how do you make sure that people are not inserting data into PouchDB by 'hacking' the client-side Javascript?

为了降低风险,您可以 remove/redefine 全局变量 window.PouchDB。因此,当您的代码开始时(假设它 运行s 在闭包中),它可以执行以下操作:

function(){
    // your closure
    var PouchDB = window.PouchDB;
    window.PouchDB = null;
    Object.freeze(window);
}

现在 PouchDB 在闭包中可见,但在控制台中不可见。

最后一行冻结整个 window 对象,因此代码在执行冻结后不能添加任何全局变量。您必须在收到并初始化所有库后调用 Object.freeze(window)。请注意,此技巧会产生很多副作用,因此请仔细测试您的代码。

Object.freeze 保证用户不能 copy/paste PouchDB 的源代码来控制和 运行 它,但不保证用户不能访问底层存储 (IDB/WebSQL ) 直接或使用控制台的资源选项卡。