用于将树分支从集合移动到另一个集合的Firebase API [英] Firebase API for moving a tree branch from a collection to another one

查看:114
本文介绍了用于将树分支从集合移动到另一个集合的Firebase API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要将一个非常大的集合分支移动到另一个集合。目前,我正在使用类似的东西:

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());
 });

显然,它非常昂贵,所以我的问题是:为什么Firebase不提供简单的API那?喜欢:

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

srcRef.moveTo(destRef);


推荐答案

您可以使用Firebase CLI。

使用npm安装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>"

我有一个名为firebase-dot-files 的个人项目,它创建bash函数来执行常见操作。其中一个是传输数据。设置bash函数后,您可以执行以下命令:

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

您还可以阅读此博文以获取更多信息。

Firebase CLI作为npm模块

Firebase CLI也可以是使用节点模块。这意味着您可以调用常用的CLI方法,但作为函数。

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

这是一个简单的数据:get命令:

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);
  });

要传输数据,您可以将数据:get,与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' });

这篇关于用于将树分支从集合移动到另一个集合的Firebase API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆