farahm
farahm

Reputation: 1326

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:

enter image description here

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?

Upvotes: 0

Views: 1820

Answers (2)

Nirel
Nirel

Reputation: 1925

enter image description here

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

Upvotes: 1

Callam
Callam

Reputation: 11539

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

Upvotes: 3

Related Questions