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?

3
  • See firebase-multi-write and be sure to read "Do You Need This". Hint: you probably don't.
    – Kato
    Apr 30, 2015 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, 2015 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, 2015 at 22:47

2 Answers 2

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);
3
  • 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 6, 2016 at 2: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 and creating an mcve.
    – Kato
    Dec 6, 2016 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 stackoverflow.com/questions/45508007
    – bibscy
    Aug 6, 2017 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

2
  • 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, 2015 at 1: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, 2015 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.