如何构建Firebase数据库

时间:2016-09-11 10:59:17

标签: android firebase firebase-realtime-database

我正在为我的移动和网络应用程序使用Firebase数据库。我想建议如何构建JSON树。我有以下用例:

移动应用用户登录并获取列表中的所有附近餐馆。用户在一家餐厅订购。餐馆老板使用网络或移动应用程序查看收到的订单并接受订单。接受订单后,移动应用程序用户会收到他的订单已被接受的回复。现在我对结构的想法如下:

enter image description here

因此,我们在每个restaurant的顶层都有一个节点,每个restaurant节点都包含一个requests节点,该节点会保存此餐馆的所有请求。

这种结构是否正常或者可以更好地构建?

2 个答案:

答案 0 :(得分:3)

考虑这样的数据结构,当您到达餐馆时,您不想要检索所有请求,这样,您就可以获得餐馆的所有请求以及来自特定用户的所有请求。 / p>

{
    "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"
        },
    },
}

答案 1 :(得分:1)

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

这就是我要做的...... 祝你好运!