I have designed a firebase realtime json structure for an online shop. The structure is as below
/Categories ( Root element )
/Health & Beauty
/Body Care
/Nivea Cream
/Johnson Cream
/XYZ Cream
/Hair Care
/Head & Shoulders Shampoo
/Pantene Shapoo
/Sunsilk shampoo
/Drinks
/Drinks
/Coca Cola
/Pepsi
/Fanta
/Chocolates
/Chocolates
/Ferrao Rocher
/Lindt
I always maintain a two level structure for all the categories. In the above case, though Chocolates and Drinks are single layered, I duplicate the node. It is because I am using a single general firebase query for fetching the categories.
The query is as below
dataBase.list('Categories/' + this.searchCategoryType).valueChanges()
In case of body care the query is
dataBase.list('Categories/' + 'Health & Beauty/Body Care').valueChanges()
In case of drinks the query is
dataBase.list('Categories/' + 'Drinks/Drinks').valueChanges()
My doubt is, Is the above structure correct?
If I have to implement the search functionality ( with the product name or type ) will the above structure help me? Because when I search with a product name (ex: Pantene ) I want all the child elements under the Catagories to be checked and give me the result. I was going through stack overflow and found an answer that, search will not be possible with the above structure. If so, what would be the best possible way to structure the above data?
Is it like
Categories
-- Nivea Cream
-- Johnson Cream
-- XYZ cream
-- Head & Shoulders Shampoo
-- Pantene Shapoo
-- Sunsilk shampoo
-- Ferrao Rocher
-- Lindt
-- Pepsi
If this is the structure, On the ui, if user clicks on Drinks link, I need to query through all the available products and fetch only the drinks items. It will downgrade the performance as I will have thousands of products under Categories. To avoid this performance issue only I designed the structure with layers.
Reference from stack overflow. Deep path query using wildcard as a path