如何在pouchdb中使用数据库

时间:2021-05-05 22:50:59

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?

我的问题是如何将每个用户的信息存储在数据库中。我应该为每个用户创建一个包含任务的数据库吗?或者有没有办法将所有用户的所有任务放入名为“Tasks”的数据库中,并以某种方式过滤同步,以便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文档而且我无法定义这个,如果有文档,请告诉我在哪里。)

2 个解决方案

#1


4  

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 can handle the database creation/deletion each time a user is created/deleted in CouchDB. In this case each PouchDB client will replicate the complete user database.
  • 每个用户的数据库是CouchDB中的每用户数据库模式。每次在CouchDB中创建/删除用户时,CouchDB都可以处理数据库的创建/删除。在这种情况下,每个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. See this post.
  • 如果用户数据被隔离且您不需要在用户之间共享信息,这是一种正确的方法。在这种情况下,如果您需要在CouchDB中同步许多用户数据库和另一个用户数据库,则可能会出现一些可伸缩性问题。看这篇文章。

Single database for every user

每个用户的单个数据库

  • You need to use the filtered-replication feature in CouchDB/PouchDB. This post explains how to use it.
  • 您需要在CouchDB / PouchDB中使用过滤复制功能。这篇文章解释了如何使用它。

  • 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. See options.selector in the PouchDB replication filtering options.
  • 如果您需要使用过滤方法,最好为此目的使用芒果选择器,因为它在CouchDB主过程中进行评估,并且可以对其进行索引。请参阅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.
  • 对于每用户数据库,如果需要在用户数量增长时将不同的用户数据库整合到一个用户数据库中,则会出现一些问题。

#2


2  

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. This would give users the ability to see everyone's data.

由于它是在javascript中,因此很容易获得凭据然后访问主数据库。这将使用户能够查看每个人的数据。

A more secure approach would be to use a database-per-user pattern. Each database will be protected by the user's credentials.

更安全的方法是使用每用户数据库模式。每个数据库都将受到用户凭据的保护。

#1


4  

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 can handle the database creation/deletion each time a user is created/deleted in CouchDB. In this case each PouchDB client will replicate the complete user database.
  • 每个用户的数据库是CouchDB中的每用户数据库模式。每次在CouchDB中创建/删除用户时,CouchDB都可以处理数据库的创建/删除。在这种情况下,每个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. See this post.
  • 如果用户数据被隔离且您不需要在用户之间共享信息,这是一种正确的方法。在这种情况下,如果您需要在CouchDB中同步许多用户数据库和另一个用户数据库,则可能会出现一些可伸缩性问题。看这篇文章。

Single database for every user

每个用户的单个数据库

  • You need to use the filtered-replication feature in CouchDB/PouchDB. This post explains how to use it.
  • 您需要在CouchDB / PouchDB中使用过滤复制功能。这篇文章解释了如何使用它。

  • 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. See options.selector in the PouchDB replication filtering options.
  • 如果您需要使用过滤方法,最好为此目的使用芒果选择器,因为它在CouchDB主过程中进行评估,并且可以对其进行索引。请参阅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.
  • 对于每用户数据库,如果需要在用户数量增长时将不同的用户数据库整合到一个用户数据库中,则会出现一些问题。

#2


2  

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. This would give users the ability to see everyone's data.

由于它是在javascript中,因此很容易获得凭据然后访问主数据库。这将使用户能够查看每个人的数据。

A more secure approach would be to use a database-per-user pattern. Each database will be protected by the user's credentials.

更安全的方法是使用每用户数据库模式。每个数据库都将受到用户凭据的保护。