Ronen
Ronen

Reputation: 837

Firebase Database Design Recommendation

I am writing an application that handles organization members and payments. I have the following entities:

Organization---+
               |
               +---Members-----+
               |               |
               |               +---Children
               +---Accounts
               |
               +---Payments

There can be >100,000 payment per organization, but only few members and Accounts. When a member is logged-in he can see only his payments. When admin is logged in he should be able to access all data.

The questions are:

  1. Should I keep hierarchical structure of the organization or should I flatten each entity?
  2. In case I would like to keep the structure hierarchical, is it possible to get an Organization with only partial sub-collections (not all members and not all payments)

Thanks.

Upvotes: 0

Views: 1084

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

One thing to always keep in mind with Firebase's real time database is the amount of data you're retrieving and the amount of data you're making the database consider.

If you make the database consider 100K payments to show <10 payments for the current user, you've wasted a lot of resources. I wrote more about this here: Firebase Scalability Limit and Firebase Performance: How many children per node?.

If you model the payments for each user separately, you'll drastically reduce the amount of data the server has to consider. This improves scalability, although of course not linearly.

If the admin user needs to see all data, I'd recommend that you first make them select a user. After that they'll follow the same access pattern as regular users.

Upvotes: 6

alltej
alltej

Reputation: 7285

You should keep your structure flat and as denormalized as possible. Querying the parent node will load all the child nodes and data.

Yes, you can still use query/filter using the SDK available.

Upvotes: 1

Related Questions