Using Firebase functions to update list array

95 views
Skip to first unread message

Harry S

unread,
May 8, 2017, 1:19:11 PM5/8/17
to Firebase Google Group
Just read Kato's blog (https://firebase.googleblog.com/2014/05/handling-synchronized-arrays-with-real.html) and have a question to resolve my one case. Kato explained that in some situations where it makes sense to store arrays in Firebase and we did that initially for one of our apps (we regret it though). Structure looks like this:

-Categories
- CategoryName
- 0: Product_Id
- 1: Product_Id
- 2: Product_Id
- CategoryName 
- 0: Product_Id
- 1: Product_Id 
 
Every time we add a product to DB, we need to assign a category by adding product Id under CategoryName array.

Question, using Kato's guidelines, is it possible to create a Firebase function for child-added event on Products node and add Product-Id to CategoryName? We also need to consider the scenario where multiple users can add Product at the same time so function needs to capture all inserts without overwriting CategoryName array.

Thanks!

Kato Richardson

unread,
May 8, 2017, 3:23:32 PM5/8/17
to Firebase Google Group
Hi Harry,

So one of the exclusions I mentioned is that arrays will fall down any time you do concurrent edits. So if multiple users will be updating the lists, then you'll definitely want to move to a different model. A good alternative is to turn the product ids into the keys and use a truthy value for them:

Categories
      CategoryName
              ProductId: true
              ProductId: true
              ProductId: true

To make this work for the short term, you want to write the entire array, not just the changed element, and you'll want to use a transaction (so concurrent edits don't blow away other users' changes).

You don't necessarily gain anything by using a Function to do this--the requirements are the same there as doing it directly from the client. But it's certainly possible if that makes your job easier.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/bce5a5a2-2e44-4a3d-b643-b0e63106e439%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages