Ad

Firebase: Is The Structure I Am Using Correct?

- 1 answer

I am developing an application that uses Firebase as a database.

I have always designed relational databases (MySQL) and I am not sure if I am designing the database I need well

In terms of MySQL, I have 3 tables that are related to each other. The first table is called "Login", the second "Usuario" and the third "Fichada"

As for relationships, a "Login" is associated with a single "Usuario" (1: 1) and a "Login" can have many "Fichada" (0: *)

Diagram

What I need is to design that same structure but in the paradigm of non-relational databases.

Now i have something like that:

"Login":
    "atribute1" : value1
    "atribute2" : value2

    "Usuario": {
         "atribute3" : value3
         "atribute4" : value4
    }

    "Collection<Fichada>": {
         "atribute5" : atribute5
    }

I have a document called "Login", which inside has another document called "Usuario" and a collection of documents called "Fichada"

What I need is to know if this is correct. I am in doubt because when I enter the application, the user must enter their login information and at that time I do not need the information contained in "Fichada" at all. The question is that I don't know any way to get only the information I need at that time (When I get the Login document, I automatically get the "Usuario" and all of the "Fichada" documents). In an alleged case of having 15.000.000 "Fichada", I do not want to bring them all, but as I bring from the Firebase is the Login object and not "Fichada" so, I can not filter or bring them later.

I am retrieving the Firebase information as follows:

Login login = new Login();
CollectionReference referenciaLogin = db.collection("login");

Query query = referenciaLogin.whereEqualTo("email", email);

List<QueryDocumentSnapshot> documentos = query.get().get().getDocuments();
login = documentos.get(0).toObject(Login.class);
login.setIdLogin(documentos.get(0).getId());

In this way, whenever I get the "Login" entity, I also get all the "Fichada", and I don't think it's optimal

Ad

Answer

While the above structure is indeed correct, if you are highly concerned about the efficiency of retrieval, you can use one more level of indirection and design it like this:

"Login":
    "atribute1" : value1
    "atribute2" : value2
    "mapToUserIO" : value_3
    "mapToFichada" : value_4

and then have two more documents as follows:

"UserIO":
        "key": value_3
        "value":{
              "atribute3" : value3
              "atribute4" : value4
         }

That is, each value is a pair.

"Fichada":
        "key": value_4
        "value":"Collection<Fichada>": {
         "atribute5" : atribute5
    }

That is, the value of Fichada document will be a collection of Fichadas.

The only problem will be maintaining the unique key for the map, but you can use the username/id/unique identifier for the login table as a key for both. Hope that helps :)

Ad
source: stackoverflow.com
Ad