Reputation: 3712
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?
Upvotes: 4
Views: 1667
Reputation: 40582
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);
Upvotes: 2
Reputation: 1176
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
Upvotes: 0