1

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', function(snap) {
   destRef.set(snap.exportVal());
 });

Obviously, it is quite expensive, so my question is: Why Firebase does not provide a simple API for that? like:

srcRef.moveTo(destRef);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
devmao
  • 670
  • 8
  • 18

1 Answers1

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.

function transfer(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' });
David East
  • 31,526
  • 6
  • 67
  • 82
  • Hi, probably I have poorly explained my issue. With the term server side, I mean firebase. Not my server app. In other words, I would like to have a "move" API like the "set" to avoid to fetch locally, from my client or server app, data just for moving it from one collection to another. – devmao Dec 13 '15 at 16:42
  • You can also run that code locally, but the Firebase CLI is best used from the command line for data transfers. Take a look at the updated answer. – David East Dec 13 '15 at 16:51
  • Sorry, but the data:get command moves the data from Firebase to my app, and the data:set from my app to Firebase. If so, this is my issue because the amount of the data that I have to move is quite big and doesn't make sense, just for moving, transfer data from your service to my client app. – devmao Dec 13 '15 at 17:01
  • There is no server side transfer that you're asking for, you can either use the CLI or firebase-import for large data sets. https://github.com/firebase/firebase-import – David East Dec 13 '15 at 17:11
  • Probably my question is not a real question, but more a feature request. This hypothetical "move" API could be very useful and powerful, so please think about it a bit. Thank you for your time. – devmao Dec 13 '15 at 17:18
  • Are there any other considerations for the answer? – David East Dec 14 '15 at 13:33
  • Cannot find the module firebase-tools when I try to deploy the script – MbaiMburu Oct 31 '17 at 07:47