Rodrigo Mello
Rodrigo Mello

Reputation: 172

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.

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?

(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.)

Upvotes: 3

Views: 669

Answers (2)

Juanjo Rodriguez
Juanjo Rodriguez

Reputation: 2121

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.
  • 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.

Single database for every user

  • You need to use the filtered-replication feature in CouchDB/PouchDB. This post explains how to use it.
  • With this approach you can replicate a subset of the CouchDB database in PouchDB
  • 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.
  • 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.

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.
  • 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.

Upvotes: 4

Alexis Côté
Alexis Côté

Reputation: 3690

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.

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

Upvotes: 2

Related Questions