Firestore - user favorites data structure suggestions

Viewed 1097

I have a project where I need a user to be able to add items to his favorites like this:

Items 
 - Item 1 
 - Item 2
 - Item 3
Users
 - User 1
   - Favorites
     - Item 1
     - Item 3
 - User 1
   - Favorites
     - Item 2

Can anyone help by suggesting a good way to structure my data?

EDIT 1:

Sorry I should have explained this in the initial question. So I have a main list which shows all the items and their data from the database. Then each user has the ability to save an item he likes. After that on that user's page he should be able to view all the items he has saved.

Okay so far so good, but the only way I can think of to save the item in the user Favorites list is to save its ID there. Now there is a problem. I want to show the data of the item not just it's ID.

1 Answers

the only way I can think of to save the item in the user Favorites list is to save its ID there.

Yes, that's the best solution, to keep a reference of the item in the favorites list which is the actual item id.

I want to show the data of the item not just it's ID.

In this case, you should query your database twice, once to get the the ids and then based on those ids to get the corresponding data. That's it. Don't be affraid about nested listeners, will work perfectly fine.

Related