JEN PERSON: Hey, all you Firebase developers.
Let's talk about two of my favorite things--
Cloud Firestore and the admin SDK.
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 will 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 in 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 could 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 could also use server side code
to send a notification to users, or something
like that if I wanted it to.
So that's what I want to show you today--
how to get started using the admin SDK to read and write
data to Cloud Firestore.
I'm going to show you how to use the Firebase admin
SDK for Java, but know JS, Python, and Go 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.
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.
I'll create a new project in IntelliJ,
and I'll add the server's account credentials.
In my Gradle build, I'll add the dependency for the admin SDK.
I'll import the service account.
Firebaseoptions.builder constructs an empty builder.
I'll use this to set the Firebase credential to "use,"
to authenticate the SDK, passing the service account credential.
Then I'll call "build" to build the Firebase options instance
using these credentials.
Finally, I'll initialize the default app instance
using FirebaseApp.initializeApp, passing the options
from Firebaseoptions.builder.
Note that if I were using the admin SDK on the Google Cloud
Platform, I can initialize the app using the application
default credentials like this.
No matter how I initialize the app,
I'll then initialize an instance of Cloud Firestore
the same way, calling admin.Firestore.
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 getQuote,
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 getQuote function, which
returns a hash map containing the quote and author.
I'll write the quote to the inspiration doc
in the sample data collection using the set function, which
sets documents data to whatever data I pass to the function.
If the document does not yet exist, it will be created.
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.
Once that work is complete, I'll log that the quote
was written to the database.
All right, let's see this code in action.
The console logs tell me that the new quote
was written successfully.
We can see the quote here.
Then in the Firebase console, I can see the new quote.
But personally, I think it's cooler to watch the quote
change in the app.
So here's the iOS version of the quotes
app with the current, randomly generated quote.
When I run my Java app again, I can
see that 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.
First, I'll create a reference to the document holding
the quote.
Then I'll get data from the inspiration doc
in the sample data collection using get.
OK, after I get the data, I'm going
to check if the 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 Java
on your server.
I hope this inspires you to incorporate
the 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.