Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. JEN PERSON: Hey, all you Firebase developers.
  2. Let's talk about two of my favorite things--
  3. Cloud Firestore and the Admin SDK.
  4. [MUSIC PLAYING]
  5. So I love that Firebase has such robust client side
  6. SDKs for iOS, Android, and web.
  7. But did you know that Firebase also has a server side option?
  8. The Admin SDK.
  9. The Admin SDK lets you access Firebase features server side.
  10. So you can, for example, read and write
  11. to Cloud Firestore outside of the client.
  12. This is especially useful for offloading CPU intensive tasks
  13. and keeping secrets away from the client.
  14. Now, if you've seen Todd's videos
  15. on getting started with Cloud Firestore for iOS or Android,
  16. you'll already be familiar with the sample
  17. app I'm going to use--
  18. one that stores an inspiring quote.
  19. If you haven't seen these videos,
  20. I highly recommend you check them out.
  21. Todd gives a great overview of the Firestore structure.
  22. I'm going to use the iOS version of the inspiring quotes
  23. app, which looks like this.
  24. You can see the quote and author, as well as text fields
  25. to write a new quote and a new author.
  26. In this video, I'll show you how to use the Admin SDK to make
  27. changes to Cloud Firestore, which can then
  28. be seen here in the client.
  29. So you may be wondering, how can I
  30. keep my quotes fresh with new inspiration every day?
  31. I could rely solely on users to write quotes, or just
  32. try to remember something interesting and type
  33. it in the console manually.
  34. After all, there's no shortage of misinterpreted quotes
  35. on the internet.
  36. Didn't George Washington say that?
  37. Anyway, one neat way I can handle this is server side.
  38. I could use one of the many quote
  39. generating REST APIs out there to get my quotes for me,
  40. and then write them to the database using the Admin SDK.
  41. I can also use server side code to send a notification
  42. to users, or something like that, if I wanted to.
  43. So that's what I want to show you today--
  44. how to get started using the Admin SDK to read and write
  45. data to Cloud Firestore.
  46. I'm going to show you the Firebase Admin SDK for Node.js.
  47. But Java, Python, and Go are also supported.
  48. OK, let's get started.
  49. First, I need service account credentials,
  50. which I can download from the Firebase console.
  51. Under the Firebase Admin SDK tab,
  52. I'll select Generate New Private Key.
  53. This downloads a JSON file containing the service account
  54. credentials.
  55. I'll add this to my project's directory.
  56. Now this service account JSON file
  57. contains sensitive information, including
  58. your service account's private encryption key.
  59. Remember to keep it confidential.
  60. Never add it to version control.
  61. Never put it in a client app.
  62. And never store it in a public repository.
  63. Now I'll install the Firebase Admin package
  64. in my project directory.
  65. I'll make a new JavaScript file for my code.
  66. I'll be running my code locally, but you can add yours
  67. to the server of your choice.
  68. I'll require the Firebase Admin package.
  69. I'll also require the config file containing the service
  70. account key.
  71. Then, I can initialize the SDK using my downloaded service
  72. account key file.
  73. Note that if I were using the Node.js Admin SDK in a Cloud
  74. Function, I could automatically initialize the SDK
  75. through the function's config variable.
  76. I wouldn't need to download the service account.
  77. If using the Admin SDK on the Google Cloud Platform,
  78. you can initialize the app using the application default
  79. credentials, like this.
  80. But no matter how I initialize the app,
  81. I'll then initialize an instance of Cloud Firestore
  82. the same way, calling admin.firestore.
  83. Now that I've initialized the Admin SDK,
  84. I'm ready to get a random quote and write it to the database.
  85. But before I get coding, let's briefly examine the structure
  86. of the quotes app.
  87. At the top level, I start with a collection called sampleData.
  88. This collection contains just one document
  89. called inspiration.
  90. This document will itself have two key value pairs,
  91. or we can call them fields--
  92. one called quote, and another called author.
  93. Now, as an aside, keep in mind, I'm
  94. not storing one quote per user.
  95. Everybody in the world will be altering this one document.
  96. If I were looking to save a different quote per user,
  97. I'd probably set up a users collection
  98. and create a different document for each user.
  99. And again, if you'd like to see how to build this app on iOS
  100. or Android, check out Todd's Firecast
  101. on getting started with the Cloud Firestore.
  102. Now, I'm going to call a function I'm calling getQuote,
  103. the result of which contains a JSON
  104. object with a random quote.
  105. The result of the function looks something like this.
  106. All right, let's generate a quote,
  107. and then add it to Cloud Firestore.
  108. OK, I'm going to call the getQuote function, which
  109. returns a promise with the response from the HTTP
  110. endpoint.
  111. I'll parse the results and create an object containing
  112. two key value pairs--
  113. a quote and an author.
  114. I'll write the quote to the inspiration doc
  115. in the sample data collection using the set function, which
  116. sets the document's data to whatever data
  117. I pass to the function.
  118. If the document does not yet exist, it will be created.
  119. So keep in mind that this replaces
  120. all the data in the document with the data pass.
  121. If I wanted to update some fields or add to the document,
  122. I'd use the update instead of set function.
  123. Once that work is complete, I'll log that the quote
  124. was written to the database.
  125. All right, let's see this code in action.
  126. In the terminal, I'll run node index.js
  127. to run my node app locally.
  128. The console logs tell me that the new quote
  129. was written successfully.
  130. We can see the quote here.
  131. And we can also see it in the Firebase console.
  132. But I think it's cooler to watch the quote change in the app.
  133. Here's the iOS version of the quotes app,
  134. with a current randomly generated quote.
  135. When I run my node up again, I can
  136. see that the quote changed in the iOS app as well.
  137. See?
  138. Pretty cool.
  139. My server side code can now generate quotes, great.
  140. That's sure to keep users motivated.
  141. But I may also want to be able to read data from Cloud
  142. Firestore server side--
  143. say, if I wanted to sanitize bad words from quotes
  144. or send notifications when new quotes are written.
  145. So let's look at getting data from Firestore.
  146. I'll get data from the inspiration doc
  147. in the sample data collection using get.
  148. I'll also include a catch block to handle if an error occurs.
  149. I'll just log it for now.
  150. OK, after I get the data, I'm going
  151. to check if the document exists.
  152. If not, I'll log that no document exists.
  153. Then, I can use the document data
  154. to do whatever my app needs, such as sending a notification.
  155. And there you have it.
  156. Now you know how to use Cloud Firestore for Node.js
  157. on your server.
  158. I hope this inspires you to incorporate the Cloud Firestore
  159. and Admin SDK into your Firebase projects.
  160. Be sure to subscribe to be the first to know
  161. about new videos for Firebase, including
  162. Firecasts like this one.
  163. Thanks for watching, and I'll see you on a future episode.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement