5

I was watching this video which talks about multi-path updates in Firebase. Multi-path updates are great because they allow you to call the firebaseRef.update() method two or more times while having them be one atomic, all-or-nothing operation.

This is great, but in my current situation I don't want to use the update() method. Instead, I want to use the FirebaseRef's push method and allow Firebase to generate a unique key for my object.

The tricky part is that I have denormalized my data so that it's in two places. What I would really like to do is have an atomic, all-or-nothing operation that uses Firebase's push() operation to create a unique key and then save the object with that key in 2 or more different places inside of my database data. However, the syntax for push() already uses an object so is what I want to do even possible?

Note: Another possible solution may be to use Firebase api to somehow generate a unique key in the client and then do a standard multipath update using that generated key as the key for my object being inserted.

Community
  • 1
  • 1
Jim
  • 3,821
  • 1
  • 28
  • 60
  • 1
    You note at the end is the answer to your question. var key = firebase.database().ref('somestring').push(); will return you a key without writing anything in firebase database. Use that key to make your object and then call update() – Himanshu Chaudhary Jan 05 '17 at 21:05
  • But suppose a user leaves the app after the first goes through and before the second goes through. Then my denormalized data is in an inconsistent state. I want to do it all in one atomic operation. – Jim Jan 06 '17 at 03:05
  • then use transaction() it is built for that purpose. But as my personal viewpoint only use transactions if it is really necessary. – Himanshu Chaudhary Jan 06 '17 at 10:44
  • transaction will not work in this situation. If you believe it does please post an answer. – Jim Jan 07 '17 at 17:49

1 Answers1

13

There is no multi-location push, but because push IDs are generated on the client, you can use a multi-location update to do what you want.

You can generate a push ID by calling push with no arguments. Push IDs are generated on the client and generating one involves no interaction with the database:

let key = firebase.database().ref().push().key;

You can use AngularFire2 to do this, too; although, you have to pass an argument (undefined) to push to appease TypeScript:

let key = angularFire.database.list('').push(undefined).key;

Once you've generated the key, you can create a multi-location update:

let obj = { some: 'object' };
angularFire.database.object('').update({
    [`a/path/${key}`]: obj,
    [`another/path${key}`]: obj
});

The update is atomic, so either all of the paths will be updated or none will.

cartant
  • 57,105
  • 17
  • 163
  • 197