简体   繁体   中英

How to structure Firebase Database

I am using Firebase Database for my mobile and web application. I wanted to get advise for how to structure the JSON Tree. I have the following use case in mind:

Mobile app user logs in and gets all nearby restaurants in a list. User sets order on one restaurant. The restaurant owner uses web or mobile application to see incoming orders and accepts them. After accepting the order, the mobile app user gets response that his order has been accepted. Now my idea for the structure was the following:

在此处输入图片说明

SO we have one node at top level for each restaurant and each restaurant node contains a requests node which saves all the requests for this restaurants.

Is that structure ok or could this be structured better?

Consider a data structure like this, you don't want to retrieve all the request when you get a restaurant and this way, you can get all the requests for a restaurant and all the requests from a particular user.

{
    "requests": {
        "req1": {
            "status": 0,
            "time": 1473593287,
            "user": { "u2": true }
        },
        "req2": {
            "status": 0,
            "time": 1473593227,
            "user": { "u1": true }
        },
        "req3": {
            "status": 0,
            "time": 1473594287,
            "user": { "u1": true }
        },
        "req4": {
            "status": 0,
            "time": 1473594227,
            "user": { "u2": true }
        },
    },
    "restaurant-requests": {
        "resA": {
            "req1": true,
            "req2": true
        },
        "resB": {
            "req3": true,
            "req4": true
        }
    },
    "restaurants": {
        "resA": {
            "name": "Example Restaurant A",
            "address": "1 Example Street"
        },
        "resB": {
            "name": "Example Restaurant B",
            "address": "2 Example Street"
        }
    },
    "user-requests": {
        "u1": {
            "req2": true,
            "req3": true
        },
        "u2": {
            "req1": true,
            "req4": true
        }
    },
    "users": {
        "u1": {
            "address": "123 Example Street"
        },
        "u2": {
            "address": "124 Example Street"
        },
    },
}

http://i66.tinypic.com/67o0tj.png

That's what I would do.. good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM