Change structure bank firebase

0

I've done a question here in SOpt on how to search the firebase realtime database , and @ RosárioPereiraFernandes suggested changing the structure of the bank

Old structure:

{
  "-L4Wqs3YbAlUgTWElF4Q" : {
    "receita" : {
      "-L6m_C46-Mj1py6RtF8H" : {
        "imagem" : "default",
        "ingrediente" : [ "ovo", "leite" ],
        "nome" : "um nome",
        "preparo" : "um preparo",
        "tipo" : true
      },
      "-L6m_Finc29fAlqU0nqe" : {
        "imagem" : "default",
        "ingrediente" : [ "amendoim" ],
        "nome" : "teste",
        "preparo" : "teste",
        "tipo" : false
      }
    },
    "senha" : "123456789",
    "usuario" : "guilherme"
  },
  "-L4WrImJ05VhzBgKHX6S" : {
    "senha" : "123456789",
    "usuario" : "patricia"
  }
}

New structure:

{
  "usuarios":{
    "-L4WrImJ05VhzBgKHX6S" : {
      "senha" : "123456789",
      "usuario" : "patricia"
    },
    "-L4Wqs3YbAlUgTWElF4Q" : {
      "senha" : "123456789",
      "usuario" : "guilherme"
    }
  },
  "receitas":{
      "-L6m_C46-Mj1py6RtF8H" : {
        "imagem" : "default",
        "ingrediente" : [ "ovo", "leite" ],
        "nome" : "um nome",
        "preparo" : "um preparo",
        "tipo" : true,
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      },
      "-L6m_Finc29fAlqU0nqe" : {
        "imagem" : "default",
        "ingrediente" : [ "amendoim" ],
        "nome" : "teste",
        "preparo" : "teste",
        "tipo" : false,
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      }
  }
}

Not the case, but what if I wanted to change into a bank that is already in production and has millions of users? Something with JavaScript or right in the Firebase console

    
asked by anonymous 10.03.2018 / 00:38

1 answer

0

I was able to create a JavaScript code to change the bank structure, but due to the fact that the firebase was asynchronous, I needed several gambi ...

To start I've created a base with some users and recipes:

for(var i = 0; i < 100; i++) {
    var chave = firebase.database().ref('/usuarios').push().key

    firebase.database().ref('/usuarios/'+chave).set({
        usuario: 'teste',
        senha: 'teste'
    })

    for(var y = 0; y < 100; y++) {
        var _chave = firebase.database().ref('usuarios/'+chave+'/receitas').push().key

        firebase.database().ref('usuarios/'+chave+'/receitas/'+_chave).set({
            nome: 'teste',
            ingrediente: ['teste', 'teste', 'teste'],
            preparo: 'teste',
            imagem: 'teste.png'
        })
    }
}

And the code to change the structure + gambi:

//array com para adicionar as chaves do usuário
var json = []
firebase.database().ref('usuarios').once('value').then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
        var chave = childSnapshot.key

        json.push({chave: chave})
    })
})

//Função para ler uma receita e adiciona-la em outro array
function ler(i) {
    firebase.database().ref('usuarios/'+json[i].chave+'/receitas').once('value').then(function(snapshot) {
        snapshot.forEach(function(child) {
            var chave = child.key
            var obj = child.val()

            _json.push({chave: json[i].chave, _chave: chave, obj: obj})
        })
    })
}

//O array com as receitas
var _json = []
for(var i = 0; i < json.length; i++) {
    ler(i)
}

//Loop para cadastrar as receitas (no novo lugar) com os valores do segundo array
for(var i = 0; i < _json.length; i++) {
    firebase.database().ref('/receitas/'+_json[i]._chave).set({
        nome: _json[i].obj.nome,
        preparo: _json[i].obj.preparo,
        ingrediente: _json[i].obj.ingrediente,
        usuario: _json[i].chave
    })
}

//Loop para excluir as receitas, já cadastradas novo lugar
for(var i = 0; i < _json.length; i++) {
    firebase.database().ref('/usuarios/'+_json[i].chave+'/receitas/'+_json[i]._chave).remove()
}

Why does a ler() function? Since Firebase is asynchronous, the variable i does not arrive in a loop within the forEach() f of firebase with the correct value, when it arrives it will already have some other higher value, because the JavaScript loop is executed several times until be done 1 loop of forEach()

You can probably improve this code to make it even more performative, but it works

    
13.03.2018 / 01:43