0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You're talking about JSON and Realtime Database, but then use code snippets that mention Firestore. While both Realtime Database and Cloud Firestore are part of Firebase, they are completely separate and use a different data model and separate APIs. Please update your question and tags to only refer to one or the other. – Frank van Puffelen Jun 11 '20 at 13:28
  • Hello @FrankvanPuffelen, Sorry for the confusion here. I am using only the realtime database. The variable name was wrong. Updated the post – Anish S Pillai Jun 11 '20 at 13:35
  • Thanks for the update. Can you replace the schematic representation of the data model with the actual JSON (as text, no screenshots), as that might make a difference here? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 12 '20 at 03:01

0 Answers0