Firebase for complex query. But no?

I switched from a parse server to firebase for my new project, but reached a point in the project where I begin to think that this is a bad idea.

Basically, I create an application where people can publish information about concerts in their city.

My first task was to filter events, so the user only receives events in his hometown. I did this by structuring the data after the cities:

{ concerts: { "New york": { ..., ... }, "Chicago": { ..., ... } } } 

Then I suggest that I need another filter for the type of concert, such as rock, pop, etc. Therefore, although I did another restructuring. However, probably there should be another 5-10 filters, and it will be very difficult to properly structure the database.

I have at least a few requests , but this was not allowed:

 firebase.database().ref("concerts") .orderByChild("type").equalTo("rock") .orderByChild("length").equalTo("2") .orderByChild("artist").equalTo("beatles") 

I thought about extracting everything from the server, and then filtered the result in the client. I see, however, two problems with this:

  • There may be a lot of unnecessary download data.
  • Some concerts will be blocked only for certain users (for example, users who have traveled to at least 10 other concerts), and there may be a security aspect by pulling these concerts home so that the user cannot see them.

I was thinking of combining filters to create query keys such as this , but with over 10 filters it will become complicated.

Is there a solution for this or should I forget about firebase for this use case?

Thank you in advance

+3
source share
1 answer

Incredibly complex queries can be created in Firebase. Data should be stored in a structure that can be verified and, most importantly, do not be afraid to duplicate data.

For example, suppose we have an application that allows the user to select a concert for a specific year and month, a specific city, and in a specific genre.

There are 3 options

year_month city genre

The user interface first asks the user to select a city

 Austin 

then the user interface asks for the year and month

 201704 

that genre

 Rock 

Firebase structure is as follows

 concerts concert_00 city: Memphis year_month: 201706 genre: Country city_date_genre: Memphis_201606_Country concert_01 city: Austin year_month: 201704 genre: Rock city_date_genre: Austin_201704_Rock concert_02 city: Seattle year_month: 201705 genre: Disco city_date_genre: Seattle_201705_Disco 

Your user interface has already polled the user for information about the request and with this built a query string

 Austin_201704_Rock 

and then query the 'city_date_genre' node for this row and you have the data.

What if the user wanted to know all the concerts in Austin in April 2017.

 queryStartingAt("Austin_201704").queryEndingAt("Austin_201704") 

You can easily expand it by adding another node request and reordering the data

 concerts concert_00 city: Memphis year_month: 201706 genre: Country city_date_genre: Memphis_201606_Country city_genre_date: Memphis_Country_201606 

And depending on which user selects their data, you may request a related node.

Adding additional nodes is a tiny amount of data and allows you to get very open queries for the data you need.

+2
source

Source: https://habr.com/ru/post/1265417/


All Articles