多个远程数据库,单个本地数据库(高级复制) [英] Multiple remote databases, single local database (fancy replication)

查看:122
本文介绍了多个远程数据库,单个本地数据库(高级复制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可管理用户的PouchDB应用程序。

I have a PouchDB app that manages users.

用户有一个本地PouchDB实例,该实例可与单个CouchDB数据库进行复制。非常简单。

Users have a local PouchDB instance that replicates with a single CouchDB database. Pretty simple.

这是使事情变得复杂的地方。我在设计中引入了组的概念。组将是不同的CouchDB数据库,但是在本地,它们应该是用户数据库的一部分。

This is where things get a bit complicated. I am introducing the concept of "groups" to my design. Groups will be different CouchDB databases but locally, they should be a part of the user database.

我在pouchDB网站上读了一些有关精美复制 的信息,这似乎是我所追求的解决方案。

I was reading a bit about "fancy replication" in the pouchDB site and this seems to be the solution I am after.

现在,我的问题是,我该怎么办?更具体地说,如何从多个远程数据库复制到一个本地数据库中?一些代码示例将是超级示例。

Now, my question is, how do I do it? More specifically, How do I replicate from multiple remote databases into a single local one? Some code examples will be super.

从下面的图表中,您会注意到我实际上需要根据用户所在的组动态添加数据库。对我的设计的批评也将受到赞赏。

From my diagram below, you will notice that I need to essentially add databases dynamically based on the groups the user is in. A critique of my design will also be appreciated.

应该流程是这样的:


  1. 从他/她的数据库中检索所有用户文档到 localUserDB

  2. var groupDB = new PouchDB('remote-group-url');
    groupDB.replicate.to(localUserDB);

    (多个pouchdb实例0_0是否存在性能问题?)

  3. 在本地用户进行与特定组相关的更改,我们确定相应的数据库并通过以下操作进行复制:

    localUser DB.replicate.to(groupDB)(是否需要过滤复制?)

  1. Retrieve all user docs from his/her DB into localUserDB
  2. var groupDB = new PouchDB('remote-group-url'); groupDB.replicate.to(localUserDB);
    (any performance issues with multiple pouchdb instances 0_0?)
  3. Locally, when the user makes a change related to a specific group, we determine the corresponding database and replicate by doing something like:
    localUserDB.replicate.to(groupDB) (Do I need filtered replication?)


推荐答案

从许多远程数据库复制到本地数据库:

Replicate from many remote databases to your local one:

remoteDB1.replicate.to(localDB);
remoteDB2.replicate.to(localDB);
remoteDB3.replicate.to(localDB);
// etc.

然后从中进行过滤复制您的本地数据库到应该接收更改的远程数据库:

Then do a filtered replication from your local database to the remote database that is supposed to receive changes:

localDB.replicate.to(remoteDB1, {
  filter: function (doc) {
    return doc.shouldBeReplicated;
  }
});

为什么要过滤复制?因为您的本地数据库包含来自许多来源的文档,并且您不想将所有内容复制回一个远程数据库。

Why filtered replication? Because your local database contains documents from many sources, and you don't want to replicate everything back to the one remote database.

为什么要使用过滤功能?由于您是从本地数据库复制 ,因此使用设计文档,视图等不会带来任何性能提升。更简单。 :)

Why a filter function? Since you are replicating from the local database, there's no performance gain from using design docs, views, etc. Just pass in a filter function; it's simpler. :)

希望有帮助!

编辑:好的,听起来像用户所属的组的名称实际上包含在第一个数据库中,这就是您所说的迭代结束。不,您可能不应该这样做。 :)您试图绕过CouchDB的内置身份验证/特权系统。

okay, it sounds like the names of the groups that the user belongs to are actually included in the first database, which is what you mean by "iterate over." No, you probably shouldn't do this. :) You are trying to circumvent CouchDB's built-in authentication/privilege system.

相反,您应该使用CouchDB的内置角色,将其应用角色,然后使用每个角色的数据库方案来确保用户只能访问其正确的组DB。用户始终可以查询 _users API来查看他们属于哪些角色。简单!

Instead you should use CouchDB's built-in roles, apply those roles to the user, and then use a "database per role" scheme to ensure users only have access to their proper group DBs. Users can always query the _users API to see what roles they belong to. Simple!

有关更多详细信息,请阅读 pouchdb身份验证自述文件

For more details, read the pouchdb-authentication README.

这篇关于多个远程数据库,单个本地数据库(高级复制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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