使用 Firebase 实时数据库批量 create/remove

Batch create/remove with Firebase Real time db

我像这样使用 Firebase 实时数据库:

 createSoldLead(soldLead: SoldLeadModel): void {
    const soldLeadsReference = this.angularFireDatabase.list<SoldLeadModel>(
      `groups/${this.groupId}/soldLeads`
    );

    const leadsReference = this.angularFireDatabase
        .list<SoldLeadModel>(
      `groups/${this.groupId}/leads`
    );

    soldLeadsReference.set(soldLead.id.toString(),soldLead);

    leadsReference.remove(soldLead.id.toString());
  }

这工作正常。但是我怎样才能批量执行此操作 create/remove?即确保他们都成功

我看到了this blog。但不知道如何将它应用到我的用例中?

您可以使用单个多路径更新在不同路径写入多个节点。

你的两个调用的等价物是这样的:

let updates = {};
updates[`groups/${this.groupId}/soldLeads/${soldLead.id}`] = soldLead;
updates[`groups/${this.groupId}/leads/${soldLead.id}`] = null;

firebase.database().ref().update(updates);

将节点值设置为 null 会删除该节点。

在这里你可以看到AngularFire版本。

createSoldLead(soldLead: SoldLeadModel): void {
    const updates = {};

    updates[`groups/${this.groupId}/soldLeads/${soldLead.id.toString()}`] = {
      ...soldLead,
      createdBy: this.createdBy,
      createdDate: this.createdDate,
    };

    updates[`groups/${this.groupId}/leads/${soldLead.id.toString()}`] = null;

    this.angularFireDatabase.object('/').update(updates);
  }