Change structure firebase bank

Asked

Viewed 421 times

0

I made a question here at Sopt on how to do a search on the firebase Realtime database, and @Rosáriopereirafernandes suggested changing the structure of the database

Ancient 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"
      }
  }
}

This is not the case, but what if I wanted to change in a bank that is already in production and has millions of users? Something with Javascript or directly in the Firebase console

1 answer

0


I was able to create a code with Javascript to change the structure of the bank, but because the firebase is asynchronous I needed several Gambi...

To start I 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()
}

Because a function ler()? How Firebase is asynchronous the variable i does not arrive in a loop within the forEach() firebase with the correct value, when it arrives will already be with some other higher value, because the Javascript loop runs several times until it is done 1 loop forEach()

Probably has to improve this code to make it more performative, but it works

Browser other questions tagged

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