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?!
admin.database().ref()
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With