Mysql structure in firebase/firestore

Asked

Viewed 301 times

1

How to create a collection 1:1 and 1:n in firebase, as in the images, where I have company tables, city and categories, as would the relationship between them in the firestore, mainly the relation 1 companies has several categories.

inserir a descrição da imagem aqui

Obs: both company and category have multiple data elements, this image is just one example.

1 answer

1


The structure of both (Realtime Database and Firestore) is more or less the same, so I will respond from one but that can be used for the other. Read the documentation for more details

If you want to maintain the same SQL structure, basically you will use a root-level structure, for example:

{
    empresa: {
        id_empresa_1: {
            nome: 'fulano',
            categoria: {
                id_categoria_1: true,
                id_categoria_2: true
            },
            cidade: 'id_cidade_1'
        }
    },
    categoria: {
        id_categoria_1: {
            nome: 'beltrano'
        }
    },
    cidade: {
        id_cidade_1: {
            nome: 'ciclano'
        }
    }
}

"Translating" pro SQL, the objects at the root level (emrpesa, city and categories) are tables, each object within them is a row of the respective table and the keys of this object are the columns

But you can simplify company objects using a city object within the company object:

{
    empresa: {
        id_empresa_1: {
            nome: 'fulano',
            categoria: {
                id_categoria_1: true,
                id_categoria_2: true
            },
            cidade: {
                nome: 'ciclano'
            }
        }
    },
    //...
}

But this causes a nesting of the data and this should be used with caution, although simplifying the structure, it can hinder (and even disable) more complex queries

Browser other questions tagged

You are not signed in. Login or sign up in order to post.