Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase: admin vs root lookups

Getting the hang of Firebase and JavaScript for coding cloud functions; but I guess I'm looking if anyone can explain the pros and cons of using the following for look-ups and/or writes in cloud functions?!

  1. the use of admin.database().ref()
    vs
  2. root.child()

I myself have been using admin.database.ref(), but do I need to? Is it just as good to use the root.child() instead?

like image 625
Learn2Code Avatar asked Apr 25 '17 17:04

Learn2Code


1 Answers

I see that you've also asked this question in the comments for my Firecast on YouTube. That's a good question. If you've been following along with the Cloud Functions for Firebase samples, you might have seen admin.database().ref().

admin.database().ref() uses the Firebase admin SDK to access data in the Database. As an admin reference, it has unrestricted access to any part of the Database.

In the video, root = event.data.ref.root, a reference to the root of the database where the .onWrite event occurred. ref has the same read and write access as the user who triggered the event. Unlike admin.database().ref(), it does not grant unrestricted access. root.child therefore access that specific path in the database, as long as the user has permission to access it.

Now event.data.adminRef.root is a Database reference with unrestricted access to any part of the Database. If this sounds like admin.database.ref(), it's because they're exactly alike. In my case, I chose event.data.ref.root to limit the amount of new topics introduced in one video. If you're more comfortable using the admin SDK, that is totally fine.

like image 84
Jen Person Avatar answered Sep 23 '22 02:09

Jen Person