DAVID EAST: Firebase console tips and tricks.
SUMIT CHANDEL: Setting up quotas and billing alerts.
DAVID EAST: Exporting Google Sheet data to Firestore.
SUMIT CHANDEL: Awesome tools for prototyping web apps.
DAVID EAST: It's time to get productive on this episode of--
ALL: #AskFirebase.
[MUSIC PLAYING]
DAVID EAST: Hey, everyone.
Welcome to a brand new episode of #AskFirebase.
My name is David East, and my favorite CSS color
is Papaya Whip.
Serious.
Just go try it.
Background color Papaya Whip.
SUMIT CHANDEL: Papaya Whip.
That sounds like a really good dessert.
And I'm Sumit Chandel.
And when I was a kid, I used to go to the pool fairly often.
But I didn't realize I couldn't swim until I was 25.
DAVID EAST: Today we're going to be doing things just a bit
differently.
SUMIT CHANDEL: Normally, we answer questions that you ask.
DAVID EAST: But today we're covering
some of our favorite Firebase productivity tips.
So if you like this episode, make
sure to like and subscribe, because we can always
find more productivity tips to talk about.
SUMIT CHANDEL: Let's get started.
All right, David.
What are your favorite Firebase console tips and tricks?
DAVID EAST: So the Firebase console
is full of awesome tips and shortcuts
to keep you productive.
And I'm going to cover just a few of my favorite ones,
starting with Firestore.
So one of the cool things about Firestore
is that you've probably gone and tried
to delete a document before.
And to do that, you click this little delete button,
and you get prompted to delete.
But you can skip that by just hitting Shift-click.
And you can just rapidly delete everything and have no regard
to that delete prompt.
But if you want to delete the whole document,
you do need a delete prompt for that one.
You can also do the same Shift-Delete
with the Real Time Database.
So in the Real Time Database console,
if I try to delete something, it's going to prompt me.
So instead of doing that, I'm just
going to hold down Shift and click, and it's gone.
And another fun tip is that I don't
have to just pass in a string.
I can also pass in JSON into the values.
And so after I put in this JSON object and hit Enter,
it formats it out to me as a full object.
And that is a really awesome time saver.
Another time saver is the one-click rollbacks
in Version Management with Firebase Hosting.
So here in the Hosting console, I
have two versions, a current version
and a previously deployed version.
So let's say something went wrong on my current.
So if I want to go back, I can just
go to the previously deployed version and click rollback.
And then automatically we see that we are deployed
to the rollback version.
And if I want to go back to the other one,
I can just roll back to that one as well.
And all those versions get maintained in the console.
Another thing I can do is keep track of how many versions
I want Firebase Hosting to store.
So I can say just only keep one version at a time, maybe 10,
or even a hundred versions.
And you can see that Firebase Hosting tells us
how much storage that's going to take up.
So I'll just select five.
And then from now on, I will just
have the last five versions of my site saved.
This last tip is all about saving time
debugging your problems and monitoring the health
of your Cloud functions.
So here in the function console, I can take a look at the usage.
So how many invocations by day.
And so I get a good feel for what's
going on for an overall usage.
But I also can go into logs.
And what logs is going to do, it's
going to show me successful function invocations but also
some errors.
So I can see I have a type error here.
And it even gives me the call stack.
But if I go to health, I can start
seeing what are the trend of these errors over time.
So 22, 16, 24, 20.
So day by day I can see when these errors were occurring.
And I see the call stack in here.
And I can even see when it was first seen
and when it was the last seen to see how
long-lasting this bug has been.
And to get even more insight, I can get
performance of this function.
And it will tell me things like how many invocations a second.
And so I can see that across the span of this whole day.
And then I can see really important things like latency.
And so how long did it take for this function to run?
And then also what memory pressure was happening?
And then also outbound networking.
So is it taking up a lot of data, how much is it consuming?
Using all that information from the Cloud functions
console I can find bugs, I can see
if there's any performance problems,
and track those down really easily.
And that was just a few tips.
And there are many, many more in the console.
So if you're curious about a specific product,
just leave us a comment and we'll cover it.
You took my computer.
SUMIT CHANDEL: Yeah, well, I needed to answer the--
ALL: Next question.
DAVID EAST: So this next productivity tip
is for you, Sumit.
How do I monitor and manage costs with the Blaze Plan?
SUMIT CHANDEL: Very good question.
The Blaze Plan is a pay-as-you-go plan.
If you're using a product like Cloud Firestorm,
you still get the benefit of the free tier.
But once you go over, you start to pay for your usage.
For the most part, you're probably
wanting to see more usage, because that
can translate to more users, traffic,
and all that awesome stuff.
However, sometimes usage can mean
that you've made a mistake, or you may not
be interested in paying more than a certain amount.
In those cases, you have two types
of tools at your disposal.
The first tool is billing alerts.
Billing alerts are set up in the Google Cloud console.
Let's check it out.
This is the billing section in the Google Cloud console.
Here we can see our cost trend.
And on the left-hand side, we go to Budget and Alerts
and create a new budget.
So we'll set a name for the budget.
We'll select the project that this budget applies to and then
whatever products that we want this budget to apply
to as well.
That can be both Firebase products or Cloud
products, whichever ones that your project is using.
Next, we'll go and set a specified amount
for our budget.
That can be any value that is important to you.
Here we can set something like $20 or $50.
So we can go ahead and set that.
We can also decide if we want to include credits in our cost.
And now we can set what thresholds at which
we want to be alerted for when we're
reaching close to our budget.
So we can set the actual amount or the forecasted amount.
So you can set any combination like for different percentages
and whether that's the actual or forecasted spend
for your account.
And after you've set those up, you'll
be alerted whenever you've reached those thresholds.
Now, one important thing to know about budgets
is that they won't actually limit your usage.
They'll just send you alerts when you're
close to reaching the budget.
However, there is another way to control cost through quotas.
Now, only certain products like Firestore
allow for quota limits.
But it's a really good way to make
sure you don't spend more than you need to.
I'd cover that in depth, but Todd Kupferman
did a fantastic job in his Get To Know Cloud Firestore series.
Check that video out for more details,
and the link is in the description.
DAVID EAST: I need my laptop back.
SUMIT CHANDEL: I need it.
DAVID EAST: I need it for the--
BOTH: Next question.
SUMIT CHANDEL: Is there a way to export data
from Google Sheet to Firestore?
DAVID EAST: So this is one of my favorite productivity tips.
Google Apps has a tool called Apps Script.
And that lets you write JavaScript snippets
that automate tedious tasks and does way more.
And we can use Apps Script to get
the contents of a spreadsheet and export it out
to a Firestore database.
So right here I have this spreadsheet.
And the first row is a bunch of properties.
And then below is the data.
And so I can open up the Google Apps Script editor
to write some code to send this data over to Firestore.
So the first thing I want to do is I want to create a menu item
and have it click on a button to say export to Firestore.
So I'll create a function called on open,
which is a magic Apps Script function,
and call a spreadsheet app object.
Get the UI, create a menu, and I'll give it
a top-level name of Firebase.
Then I'll add an item.
And this right here is my export to Firestore item.
And that will call a function, which we've placed as a string.
And then we add it to the UI.
So now we'll create this function called Main.
And we're going to do a couple of things.
We're going to get the active spreadsheet.
And we're also going to get its name,
because its name is going to match up
for the collection in Firestore.
So we'll create a sheet variable called
SpreadsheetApp.getActiveSheet.
And then from there we can get the sheet name
by just going sheet.getName.
So now what I need to do is I need to get the first row,
because those are the object properties.
So things like the author, the ISBN, the category,
and the title.
And then I want to get the next 100 rows,
because those are the records.
And that's going to be whatever the author's name is.
So Alice, author.
ISBNs-- I don't know what those even look like--
and category, and the title of the book.
And this comes back as a two-dimensional array.
And then from there we'll take that data
and send it out to Firestore.
So to get the first row properties,
I'm going to do that as a function called get properties.
That'll take in a sheet as a parameter.
And then we'll call sheet.getRange.
We'll start at the first row, the first column.
We only need to get the first row.
And then we'll go over four columns.
Then we're going to call getValues,
which actually returns it as a two-dimensional array.
So the inner array is author, ISBN, category, and title.
And so I'm going to just grab that zeroth index
inside of the array so it only comes back
as one array, not two-dimensional.
So I'll grab these properties in the main method
by passing in the sheet.
And then now I need to get the next 100 rows.
So I'll create a method called getRecords,
which also takes in the sheet, and then I'll
return sheet.getRange.
We'll start at the second row and then the first column,
go 100 rows, and then across four columns,
and call get values.
And this right here returns us a 2D array,
but that is what we want.
So we'll get these records by calling get records
and passing in the sheet.
And then now we just need to export to Firestore.
So I'm going to get a Firestore app .getFirestore.
And this needs to take in a service account.
So this library right here is called Google Apps Scripts
Firestore.
And we paste in this ID.
And so that's how we get it added in.
And then from here we need to configure it
with a service account.
So I'm just going to blank these out
for now, because I don't want you stealing my service
account.
But to get a service account, I go to the console.
I'll download the key, and then I just go through
and I find those properties.
And then I would just paste them in right here.
But I'm going to do that behind the scenes.
So now I'll create my method export to Firestore.
And that's going to take in the Firestore instance,
the collection name, the properties from the first row,
and then all of the records.
So I'm going to map over all the records
and convert them from an array into an object,
because we first get a record that's just an array of values.
And then we also have the properties,
which is the actual objects, the properties themselves.
So I'll create an empty object called data.
And what I really want to do is say data of author
is the first index in the record.
So I can loop through the properties.
And then from here, I can take the data object
and put in the property name and then use
the index in the for each method to grab
that value from the record.
So I'll return that data, and it's
gone from an array of values to an object of values.
So for each one of these, I can just
call Firestore.createdocument, pass in the collection name,
and then put the data as the value.
So now all I have to do is just call
that method in the main with all the proper arguments.
And on open calls, it's going to call our main method
when we click Add to Firestore.
We grab the properties, grab the records, grab the Firestore.
And then I will export it out to Firestore.
All right, so we have this great Firebase button.
We click it, and we can see that all of our data
is in the database.
And this will work every time I just go and click that button.
This is really helpful if your team manages
data in a spreadsheet and you commonly
need to get that data into Firestore.
And the use cases for Apps Script in Firestore
really are endless.
You can do the inverse, and you can take data from Firestore
and you can put it into a spreadsheet
where you can safely slice and dice your data not
in a production environment.
So Apps Script in Firestore, there's a lot of amazing things
you can do.
So that's all the productivity tips
we have for you in this episode.
SUMIT CHANDEL: If you have a specific product
that you'd like to know more about,
or if you have your own productivity tips
to share with us, let us know.
DAVID EAST: So make sure to like and subscribe,
because we're going to be doing more of these kinds of videos.
And we will see you in the very next episode of #AskFirebase.
[UPBEAT MUSIC PLAYING]