Getting Started with Cloud Firestore with Go – Firecasts


JEN PERSON: Hey all you
Firebase developers. Let’s talk about two
of my favorite things: Cloud Firestore
and the Admin SDK. [FUNK MUSIC PLAYING] So I love that Firebase has
such robust client-side SDKs for iOS, Android, and web. But, did you know that Firebase
also has a server-side option, the Admin SDK? The admin SDK lets you access
Firebase features server-side, so you can, for
example, read and write to Cloud Firestore
outside of the client. This is especially useful for
offloading CPU-intensive tasks, and keeping secrets
away from the client. Now, if you’ve
seen Todd’s videos on getting started with Cloud
Firestore for iOS or Android, you’ll already be
familiar with the sample app I’m going to use, one that
stores an inspiring quote. If you haven’t
seen these videos, I highly recommend
you check them out, Todd gives a great overview
of the Firestore structure. I’m going to use the iOS
version of the Inspiring Quotes app, which looks like this. You can see the quote and
author, as well as text fields to write a new quote
and a new author. In this video, I’ll show you
how to use the Admin SDK to make changes to Cloud
Firestore, which can then be seen here in the client. So you may be wondering, how
can I keep my quotes fresh, with new inspiration every day. I could rely solely on users
to write quotes, or just try to remember
something interesting, and type it into the
console manually. After all, there’s no shortage
of misinterpreted quotes on the internet. Didn’t George
Washington say that? Anyway, one neat way I can
handle this is server-side. I could use one of the
many quote-generating REST APIs out there to
get my quotes for me, and then write them to the
database using the Admin SDK. I can also use server-side
code to send a notification to users, or something
like that, if I wanted to. So that’s what I want to show
you today: how to you get started using the Admin
SDK to read and write data to Cloud Firestore. Today, I’m going to show you
the Firebase Admin SDK for Go. But Java, Python, and
Node.js are also supported. OK. Let’s get started. First, I need service
account credentials, which I can download from
the Firebase console. Under the Firebase
Admin SDK tab, I’ll select Generate
New Private Key. This downloads a JSON file
containing the service account credentials. I’ll add this to my
project’s directory. Now, this service
account JSON file contains sensitive
information, including your service account’s
private encryption key. Remember to keep
it confidential. Never add it to version control,
never put it in a client app, and never store it in
a public repository. Now, I’ll install the
Firebase Admin SDK to my Go app using go get. I’ll navigate to where I
want to put my new Go app, and then create it. When initializing the
Admin SDK on my own server, I need to use the
service account key in my Options, which I do,
using the With Credentials file method, as shown here. Then, I use the new
app function to create a new app from the provided
Context, Config, and Client options. In my case, the Client options
contain the service account file, so the app
will be authenticated using that credential. Now, if you’re running your code
from within the Google Cloud Platform, you use the
Google application default credentials. That way, the Admin SDK itself
fetches a service account on your behalf, like this. Since I’m simply running
my project locally for now, I’m going to use the
service account credentials. Now that I’ve initialized
the Admin SDK, I’m ready to get a random quote
and write it to the database. But before I get coding, let’s
briefly examine the structure of the Quotes app. At the top level, I start with
a collection called Sample Data. This collection contains
just one document, called Inspiration. This document will itself
have two key value pairs– or we can call them
fields –one called Quote, and another called Author. Now, as an aside,
keep in mind I’m not storing one quote per user. Everybody in the world will
be altering this one document. If I were looking to save
a different quote per user, I’d probably set up
a Users collection, and create a different
document for each user. And again, if you’d like to see
how to build this app on iOS or Android, check
out Todd’s Firecast on Getting Started
with Cloud Firestore. Now, I’m going to
call a function I’m calling Get Quote,
the result of which contains a JSON object
with a random quote. The result of the function
looks something like this. All right, let’s
generate a quote, and then add it to
Cloud Firestore. OK, I’m going to call the
Get Quote function, which returns a struct called
Quote, containing the quote and the author. I’ll log the quote in
terminal, so we can see it. I’ll write the quote
to the inspiration doc in the sample data collection
using the Set function, which sets the document’s
data to whatever data I pass to the function. If the document does not yet
exist, it will be created now. Keep in mind this
replaces all data in the document with
the data passed. If I wanted to update some
fields, or add to the document, I’d use Update instead of Set. I’ll log the error
if one is found. Then, I’ll log the
results so we can see when the quote
was successfully written to the database. All right, let’s see
this code in action. In the terminal, I’ll build and
then run my Inspiration app. The console logs tell
me that the new quote was written successfully. And we can see that quote
here, and we can also see it in the Firebase console. But I think it’s cooler to watch
the quote change in the app. So here is the iOS
version of the Quotes app, with the current
randomly-generated quote. When I run my Go app
again, I can see the quote changed in the iOS app as well. See? Pretty cool. My server-side code can
now generate quotes. Great, that’s sure to
keep users motivated. But I may also want to
be able to read data from cloud Firestore,
server-side, say, if I wanted to sanitize
bad words from quotes, or send notifications when
new quotes are written. So let’s look at getting
data from Firestore. I’ll get data from
the Inspiration doc in the sample data
collection using Get. I’ll also include a catch block
to handle if an error occurs. I’ll just log it for now. Then, after I get
data, I’m going to check if a document exists. If not, I’ll log that
no document exists. Then, I can use the document
data for whatever my app needs, such as sending a notification. And there you have it. Now you know how to use
Cloud Firestore for Go on your server. I hope this inspires
you to incorporate Cloud Firestore
and the Admin SDK into your Firebase projects. Be sure to subscribe to be the
first to know about new videos from Firebase, including
Firecasts like this one. Thanks for watching, and I’ll
see you on a future episode. [FUNK MUSIC PLAYING]

Leave a Reply

Your email address will not be published. Required fields are marked *