Home > OS >  Best way to store user-specific data in Firestore
Best way to store user-specific data in Firestore

Time:05-06

I have an app that helps store owners manage their inventory through a simple API-driven interface.

My app stores all data on Firestore. My simplified database looks like this:

-users
    -name
    -email
    -uid

-products
    -atts
    ...
    -ownerId

-someOtherThing
    -atts
    ...
    -ownerId

The idea is that only documents with ownerId that matches the current user ID will be accessible to the user. User with ID=5 will only have access to items that match ownerId=5.

Is this a good way of storing this data? I am worried that I will eventually end up with thousands of documents in that collection and querying them by "ownerId" might not be the best way to tackle this. On the other hand, I might end up with hundreds of users too, which probably makes it bad design to introduce several new collections for each of them?

What would be a better approach design-wise?

CodePudding user response:

While "a good way" is subjective and purely dependent on the use-cases of your app, what you're proposing is quite a common way to store data in Firestore.

Your concern about the number of users and other documents is unwarranted, as Firestore guarantees that the performance of returning the (say) products for a specific user depends solely on the number of products returns, not on the total number of products in the database.

So if you have 10 products that you're the ownerId for, then no matter how many other users/products there are, the amount of time it takes to retrieve your 10 products will always be the same.

  • Related