简体   繁体   English

如何在pouchdb中使用数据库

[英]How to work with databases in pouchdb

I'm making a list of tasks to learn how to use PouchDB / CouchDB, the application is quite simple, would have authentication and the user would create their tasks. 我正在制作一个任务列表,以学习如何使用PouchDB / CouchDB,应用程序非常简单,将具有身份验证,用户将创建他们的任务。

My question is regarding how to store each user's information in the database. 我的问题是如何将每个用户的信息存储在数据库中。 Should I create a database for each user with their tasks? 我应该为每个用户创建一个包含任务的数据库吗? Or is there a way to put all of the tasks of all users into a database called "Tasks" and somehow filter the synchronization so that PouchDB does not synchronize the whole database (including other users' tasks) that is in the server? 或者有没有办法将所有用户的所有任务放入名为“任务”的数据库中,并以某种方式过滤同步,以便PouchDB不同步服务器中的整个数据库(包括其他用户的任务)?

(I have read the pouchdb documentation a few times and I have not been able to define this, if it is documented, please inform me where.) (我已经阅读了几次pouchdb文档而且我无法定义这个,如果有文档,请告诉我在哪里。)

You can use both approaches to fulfill your use case: 您可以使用这两种方法来完成您的用例:

Database per user 每个用户数据库

  • A database per user, is the db-per-user pattern in CouchDB. 每个用户的数据库是CouchDB中的每用户数据库模式。 CouchDB can handle the database creation/deletion each time a user is created/deleted in CouchDB. 每次在CouchDB中创建/删除用户时,CouchDB都可以处理数据库的创建/删除。 In this case each PouchDB client will replicate the complete user database. 在这种情况下,每个PouchDB客户端将复制完整的用户数据库。
  • You can enable it in the server config 您可以在服务器配置中启用它
  • This is a proper approach if the users data is isolated and you don't need to share information between users. 如果用户数据被隔离且您不需要在用户之间共享信息,这是一种正确的方法。 In this case you can have some scalability issues if you need you sync many user databases with another one in CouchDB. 在这种情况下,如果您需要在CouchDB中同步许多用户数据库和另一个用户数据库,则可能会出现一些可伸缩性问题。 See this post . 看这篇文章

Single database for every user 每个用户的单个数据库

  • You need to use the filtered-replication feature in CouchDB/PouchDB. 您需要在CouchDB / PouchDB中使用过滤复制功能。 This post explains how to use it. 这篇文章解释了如何使用它。
  • With this approach you can replicate a subset of the CouchDB database in PouchDB 使用此方法,您可以在PouchDB中复制CouchDB数据库的子集
  • As you have a single database is easier to share info between users 由于您拥有单个数据库,因此更容易在用户之间共享信息
  • But, this approach has some performance problems. 但是,这种方法存在一些性能问题。 The filtering process is very inefficient. 过滤过程效率很低。 As it has to process the whole dataset, including the deleted documents to determine the set of documents to be included in the replication. 因为它必须处理整个数据集,包括已删除的文档以确定要包含在复制中的文档集。 This filtering is done in a couchdb external process in the server which add more cost to the process. 此过滤在服务器中的couchdb外部进程中完成,这会为进程增加更多成本。
  • If you need to use the filtering approach it is better to use a Mango Selector for this purpose as it is evaluated in the CouchDB main process and it could be indexed. 如果您需要使用过滤方法,最好为此目的使用芒果选择器 ,因为它在CouchDB主过程中进行评估,并且可以编制索引。 See options.selector in the PouchDB replication filtering options. 请参阅PouchDB复制筛选选项中的options.selector

Conclusion 结论

Which is better? 哪个更好? depends on your use case... In any case you should consider the scalability issues in both cases: 取决于您的用例...在任何情况下,您都应该考虑两种情况下的可伸缩性问题:

  • In the case of filtered replication, you will face some issues as the number of documents grow if you have to filter the complete dataset. 在过滤复制的情况下,如果必须过滤整个数据集,则随着文档数量的增加,您将面临一些问题。 This is reported to be 10x faster when using mango selectors. 据报道,使用芒果选择器时,速度提高了10倍。
  • In the case of db-per-user, you will have some issues if you need to consolidate the different user databases in a single one when the number of users grow. 对于每用户数据库,如果需要在用户数量增长时将不同的用户数据库整合到一个用户数据库中,则会出现一些问题。

Both pattern are valid. 两种模式都有效。 The only difference is that in order to use the filtered replication, you need to provide access to the main database. 唯一的区别是,为了使用过滤的复制,您需要提供对主数据库的访问。

Since it's in javascript, it's easy to get credentials and then access the main database. 因为它是在javascript中,所以很容易获得凭据然后访问主数据库。 This would give users the ability to see everyone's data. 这将使用户能够查看每个人的数据。

A more secure approach would be to use a database-per-user pattern. 更安全的方法是使用每用户数据库模式。 Each database will be protected by the user's credentials. 每个数据库都将受到用户凭据的保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM