best practice data design in firebase

做~自己de王妃 提交于 2019-12-21 21:39:33

问题


How could i structure data for following scenario?

There is an app called Food Ordering System. It contains

Username(Who is registering this restaurant menus for online order)

Restaurant_name

Description

Location

Estimated Delivery

Menu(Has multiple menus for one restaurant)

My design

  "restaurant":{
    "username":{
        "restaurant_name":"KFC Restaurant",
        "description":"short description on restaurant",
        "estimated delivery":"1hour/km",
        "distance":"20km away",
        "location":"Kathmandu",
        "rating":"rating star up to 5",
        "menus":{
            "menu":{
                "item":"buff momo",
                "price":"$5",
                "rating":"rating star up to 5"
                },
            "menu":{
                "item":"Fried Chicken",
                "price":"$10",
                "rating":"rating star up to 5"
            },
            "menu":{
                "item":"BBQ",
                "price":"$20",
                "rating":"rating star up to 5"
            }
        }
    }

}

Is my design the best practice design?


回答1:


It actually depends on what you want to do with this data.

If in your website you want to show a list of all restaurant and when you click on restaurant show the list of menu they are offering, storing the menu inside the restaurant might not be a good idea.

Because according to the doc :

When we read a data node in our Firebase database, we also retrieve all of its children!

(the old one) https://www.firebase.com/docs/rest/guide/structuring-data.html#section-denormalizing-data

Also, imagine if you want to do a search bar and look for a menu (like chicken nuggets) and return the list of restaurant offering this I would design your database this way:

"restaurant":{
"$username":{
    "restaurant_name":"KFC Restaurant",
    "description":"short description on restaurant",
    "estimated delivery":"1hour/km",
    "distance":"20km away",
    "location":"Kathmandu",
    "rating":"rating star up to 5",
    ...
  }
}

"menus":{
  "$username" : {
        "menu":{
            "item":"buff momo",
            "price":"$5",
            "rating":"rating star up to 5"
            },
        "menu":{
            "item":"Fried Chicken",
            "price":"$10",
            "rating":"rating star up to 5"
        },
        "menu":{
            "item":"BBQ",
            "price":"$20",
            "rating":"rating star up to 5"
        }
    }
}


来源:https://stackoverflow.com/questions/37345365/best-practice-data-design-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!