-1

How can I use the database efficiently (economically) when fetching data after Firebase has saved new data?

There is a table of cars with models and characteristics of cars. The user will create a list in the My cars table and add the cars he has.

Should all properties be added to the my cars table or should they be added only by adding the ids of the cars and placing a request to those ids with the help of a loop?

cars.json

{
    "-MxOq5xYjYpZjOYviQQa": {
        "color": "red",
        "hp": "150",
        "title": "Auidi A1",
        "type": "DSG"
    },
    "-MxOqAAtRU06ZE04djoB": {
        "color": "black",
        "hp": "200",
        "title": "Auidi A2",
        "type": "DSG"
    },
    "-MxOqN-1BpR2DLlucZ1J": {
        "color": "white",
        "hp": "250",
        "title": "Auidi A3",
        "type": "DSG"
    }
}

1.Usage (list.json)

{
    "title": "My Car",
    "desc": "Cars I own.",
    "userId" : "1213",
    "cars": {
        "-MxOq5xYjYpZjOYviQQa": {
            "title": "Auidi A1",
            "color": "red",
            "hp": "150",
            "type": "DSG"
        },
        "-MxOqAAtRU06ZE04djoB": {
            "title": "Auidi A2",
            "color": "black",
            "hp": "200",
            "type": "DSG"
        }
    }
}

2.Usage (list.json)

{
    "title": "My Car",
    "desc": "Cars I own.",
    "userId" : "1213",
    "cars" : ["-MxOq5xYjYpZjOYviQQa","-MxOqAAtRU06ZE04djoB"]
}

Note: There is more attribute data in the Cars table.

Gurkan T
  • 198
  • 2
  • 14

1 Answers1

0

There is no singular correct answer here, it all depends on the need of your application and your comfort level with various solutions.

Duplicating the data is a common approach in Firebase (and other NoSQL databases), since it means you get the necessary data with the minimum number of API calls, and thus is often the most scalable solution.

On the other hand, performing additional load operations is quite fast on Firebase since it pipelines the requests over a single connection, so the data model with just IDs is quite feasible too.

In the end, only you can choose. Since you mention cost, that'd be something you can quickly measure/estimate between both models and then use the pricing table/calculator to determine what fits those needs best.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807