Skip to content Skip to sidebar Skip to footer

Firebase Api For Moving A Tree Branch From A Collection To Another One

In my application, I need to move a quite big collection branch to another collection. Currently, I'm using something like that: srcRef .startAt(start) .endAt(end) .once('value'

Solution 1:

You can use the Firebase CLI.

The Firebase CLI is installed with npm

sudo npm install -g firebase-tools

Then you can execute commands to get and set data:

firebase data:get / -f "<my-firebase-app>"

I have a personal project called firebase-dot-files that creates bash function to do common operations. One of them is transferring data. After you setup the bash functions you can do the following command:

transfer_to dev-firebase staging-firebase

You can also read this blog post for more information.

Firebase CLI as an npm module

The Firebase CLI can also be used a node module. This means you can call your usual CLI methods, but as functions.

Here is a simple data:get command:

var client = require('firebase-tools');
client.data.get('/', { firebase: '<my-firebase-db>', output: 'output.json'})
  .then(function(data) {
    console.log(data);
    process.exit(1);
  })
  .catch(function(error) {
    console.log(error);
    process.exit(2);
  });

To transfer data, you can combine a data:get, with a data:set.

functiontransfer(path, options) {
  var fromDb = options.fromDb;
  var toDb = options.toDb;
  var output = options.output;
  client.data.get(path, { firebase: fromDb, output: output })
    .then(function(data) {
      return client.data.set(path, output, { firebase: toDb, confirm: true });
    })
    .then(function(data) {
      console.log('transferred!');
      process.exit(1);
    })
    .catch(function(error) {
      console.log(error);
      process.exit(2);
    });
}

transfer('/', { fromDb: '<from>', toDb: 'to',  output: 'data.json' });

Post a Comment for "Firebase Api For Moving A Tree Branch From A Collection To Another One"