4

According to the firebase docs data is flattened and indices are used to link different nodes in the tree:

users
  $userId
    widgets
      $widgetId
widgets
  $widgetId

In the above example, if a user creates a widget, that widgetid is also stored under the user node.

My question is whether there is a way to guarantee the consistency of the operation, considering there is now the more than one write required.

Assuming the first operation is:

var newKey = fb.child('widgets').push({ name: 'widge' }).key();

I can then write it to:

fb.child('users').child(auth.id).child('widgets').child(newKey).set(true);

But what if there was a failure or other problem between the two writes? Or if I have multiple places I need to store that key and a failure occurs between those writes?

Is there currently a way to handle this in firebase?

If not, are there plans to support this in the future?

And if so, can someone provide a specific example of how that would be done?

martypdx
  • 3,702
  • 14
  • 22
  • See [firebase-multi-write](https://github.com/katowulf/firebase-multi-write) and **be sure to read "Do You Need This"**. Hint: you probably don't. – Kato Apr 30 '15 at 17:41
  • @Kato thx! this looks spot on - I'll have a look. In regards to "Do You Need This", it isn't always about needing it, also being able to explain and answer questions. – martypdx Apr 30 '15 at 22:44
  • And great questions they are. To counter, lots of others will read this and it's best to lay that on the table up front. – Kato Apr 30 '15 at 22:47

2 Answers2

2

Note that this is now part of the core Firebase API. See this blog post for details.

var mergedUpdate = {};
mergedUpdate[ 'users/' + userId + '/widgets/' + widgetId ] = true;
mergedUpdate[ 'widgets/' + widgetId ] = widgetData;

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.update(mergedUpdate);
Kato
  • 40,352
  • 6
  • 119
  • 149
  • could you provide some insight into a validation rule that would update the widgetId in the users/ node only when the new data contains both paths? – Felipe Dec 06 '16 at 02:25
  • I'd be happy to help. That sounds like a separate question and would need specific details to be of any help. See [how to ask](http://stackoverflow.com/help/how-to-ask) and [creating an mcve](http://stackoverflow.com/help/mcve). – Kato Dec 06 '16 at 17:57
  • @Kato Is it currently possible to write to two separate nodes in a firebase transaction? I assume your answer would not apply if multiple users would attempt to write to the same resource and only one user should be allowed to do that. Please advise. My question is https://stackoverflow.com/questions/45508007 – bibscy Aug 06 '17 at 20:08
0

Take a look at transaction operation and utilize onComplete callback to make your second write. You can chain the transactions.
The examples provided on Firebase site are for incrementing counters safely.

I have provided a sample code for another question that may be of help to you. How to store users and groups for a chat using Firebase

Community
  • 1
  • 1
Saeed D.
  • 1,125
  • 1
  • 11
  • 23
  • Thanks for the links, but IMO that's a hack if the client is manually reverting the data. It may be the best hack, but I'd like to hear it stated that firebase does or doesn't (yet) support this. – martypdx Apr 30 '15 at 01:53
  • As far as I know, the traditional database transaction (multiple updates grouped together) that would automatically roll back your changes is not supported. The current .transaction only protects concurrent updating (more like the locking feature of dbms). – Saeed D. Apr 30 '15 at 14:30