Is there such a thing as “Multi-Path Push” in Datasbase in real time Firebase?

I watched this video talking about multi-path updates in Firebase. Multipath updates are great because they allow you to call two methods of accessing the firebaseRef.update () method twice, and they are 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 push attack method and let Firebase generate a unique key for my object.

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

Note. Another possible solution would be to use Firebase api to somehow create a unique key in the client, and then perform a standard multipath update using this generated key as the key for my object.

+5
source share
1 answer

There is no multi-position push, but since push IDs are generated on the client, you can use the update with multiple locations to do what you want.

You can create a push id by calling push with no arguments. Push identifiers are generated on the client, and the generation of one of them is not related to the interaction with the database:

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

You can also use AngularFire2 for this; although you need to pass the argument ( undefined ) to push to calm TypeScript:

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

Once you have generated the key, you can create an update with several locations:

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

The update is atomic, so either all paths will be updated or not.

+11
source

Source: https://habr.com/ru/post/1262376/


All Articles