Firebase Functions Tutorial - Cloud Firestore Triggers | Diligent Dev

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everybody it is Rob aka the diligent dev and welcome to this cloud fire store triggers tutorial in this tutorial we're gonna cover things like automating the sending of push notifications and emails syncing your data within fire store and also how to clean up your data in fire store and in firebase storage so without any further ado let's jump over the computer and get right into it okay so the first thing we're going to do is head over to firebase google.com go ahead and sign up for an account if you don't already have one once you've signed up click go to console we're gonna create a new project we'll call this firebase function triggers it continue hit continue again choose the default account hit continue and once this is done creating the project I'll be right back okay so the project is setup successfully so the next thing we're going to do is go to database up here at the top of the screen we're gonna click create database we'll start it in test mode for now we'll pick US central and once this is done provisioning I'll be right back okay so our database has been provisioned the next thing we're gonna do is click on storage we're gonna click on get started click Next click done and once this is done creating our default bucket I'll be right back okay so our firebase storage bucket has been created successfully and the next thing we're going to do is install the firebase CLI so I'm going to go to their Doc's and I'll leave a link to this in the description and you can see we can install through NPM but before you install anything through NPM you're gonna need nodejs so if you don't already have that go ahead and install it and then we're gonna copy this command out of here I'm gonna open a new terminal I'm gonna paste it in and hit enter and one caveat is if you're on a Mac you'll have to put sudo in front of this command and type in your password so that it can be installed globally on your machine and once this is done I'll be right back alright so the firebase CLI has installed successfully the next thing we're ready need to do is run firebase login ok yes and then go ahead and login to your firebase account and what this is going to do is sync our CLI with our firebase account okay so now what I've done is created a blank folder on my desktop and opened it in Visual Studio code we'll go up top we'll go to new terminal and we're gonna run the following command firebase and it's it's gonna ask me if I'm in the root directory and I'm gonna say yes it's gonna ask me what features I want to use we're gonna use fire store functions and storage it's gonna ask me if I want to use an existing project I'm gonna say yes I'm gonna go down to the firebase functions triggers and hit enter it's gonna ask me where I want to store the fire store roles I'm just gonna hit enter for the default it's gonna ask you where you want to sort of the fire store indexes hit enter for default what type of language we want to use for this we're going to use JavaScript asks if we want to use es lint I'm gonna say yes and there's gonna ask if we want to install our dependencies with NPM now and I'm gonna say yes again and once this is finished I will be right back okay so after all the dependencies were installed it asked me what file I wanted to use for my storage roles and we'll just use storage rules the default you just hit enter there and after that it'll build out your whole projects let's go ahead and take a look at the structure here and our functions folder we have node modules so we can use any NPM packages we'd like in there we have our linter rules our get ignore our index J s where we'll be writing all of our triggers our package.json and this is where it stores all your dependencies that you'll need and scripts that you can run we have some fire based configuration folders another get ignore outside of the functions folder we have this firebase JSON which tells firebase where all the rules and everything are stored and if you ever wanted to change your file names such as your fire store roles you could change it and then reference it here we have our indexes for fire store our fire store rules and our storage rules okay so the only thing we're really gonna be concerned about in our project is this index J s file and you'll see it gives you an example firebase functions call so we'll go ahead and uncomment this and take a look at it and you'll see it exports a function called hello world it's an HTTP request and on the request it responds with hello from firebase for the purpose of this demo we're not going to be focusing any HTTP requests if you have any interest in that I'll link a video on the screen where I create a REST API using firebase functions but for now we're just gonna focus on triggers now the first cloud firestore trigger we're gonna cover is on create and this is a trigger that gets fired every single time a document is created in a specific collection inside of fire store now these are great for sending things like push notifications and maybe welcome emails when a user signs up but for this tutorial we're gonna keep it very basic and all we're gonna do is after a user signs up we're going to log it in a logging table where we would typically send something like an email so in order to do this we're gonna come up here to the top and I'm gonna say con Stedman equals require firebase admin and then we'll say admin that initialize app and then we'll set an instance of our fire store so we'll say cons DB equals admin dot fire store now down here we're going to go ahead and delete everything up to the exports and we're gonna say on user creates and we'll set that equal to functions dot fire store dot documents we will reference our users collection and then we'll put a forward slash and put user ID here and that would enable us to be able to grab the user ID if we needed it then we'll say on creates we'll pass in a snapshot and context and set that equal to an arrow function in order to grab the values that we've passed in on on our creation of our document will say Const values equals snap data and then this is typically where you'd send out like a push notification or an email so we'll just put a comment out here and say send email but like I said for this tutorial we're just gonna log this in a logging table so we'll say DB doc collection will reference the logging table and then we'll say add and we'll pass in an object with a description and we'll set that equal to some string and trip elation so we'll use a back tic here and we'll say email was sent to user with username will say dollar sign and wrap that in curly braces so that we can reference our values and say dot username now this needs to be an async function so that we can await this so here we'll say awaits and up top here we'll say async I'm going to go ahead and save that and then here at the bottom we'll run our command firebase deploy - - only functions and that will go ahead and deploy our functions to the cloud and I'll be right back after that's finished okay so as we can see here our function deployed successfully so what we're gonna do now is hop over to firebase I'm gonna start a collection I'm going to call it users and then we're gonna throw some fields in here we'll say username set that equal to the diligent dev will add a first-name equal to diligent and a lastname equal to dev we're gonna go ahead and save this and then I'm gonna give it a second for our cloud fire store trigger to fire and then we're going to reload the page and now we see a log in collection and in here it says a description of email was sent to user with username the diligent dev so you can see on our document create it went ahead and fired that trigger and that's where you would send something like an email or a push notification out so moving right along the next trigger we're gonna cover is the on update trigger now one great thing about firebase and no sequel databases in general is that they scale really well but the problem with them is that you'll have duplicate data in a lot of your different collections so let's say that we have a new collection and we want to call it reviews and we're gonna say that the username that left this specific review is the diligent dev and then we'll say stars he left five and contents is is great video and we'll go ahead and save this and this would be an example of something that where you would have a duplicate data because you reference the username in your reviews collection but you also have the username in your users collection so when someone would go in and update their profile and change their username in the users collection we would want that updated as well in the reviews collection and I'm going to show you how to do that right now so here we are back at our project and since we're referencing the exact same collection what I'm gonna do is just copy this function and take it down here we're gonna change the function name to on user update we're gonna change this from on create to on update we'll go ahead and delete everything that's inside of the function we're gonna grab the new values so we'll say Const new values equals snap dot after dot data we're gonna grab the previous values so say const previous values equals snap dots before dot data and then we're going to check to see if the username has been changed so we'll say if new values dot username does not equal previous values dot username then we're going to go ahead and update our reviews collection so in order to do this we're gonna say cons snapshots equals awaits DB dot collection or in a reference that reviews collection dot where username is equal to previous values dot u username and then dot gets so what this does is returns all the reviews that have the old username the next thing we're going to do say let's update promises equal an empty array because we do not want to do in a weight inside of a loop and we're gonna say snapshots dot for each pass in the documents and then I say update promises dot push DB collection and a reference the reviews collection then we're gonna say dot doc I want to pass doc dot ID number I say updates and we're going to pass in an object with username and set that equal to new values dot username and then outside of this for each we're gonna say awaits promise dot o and we're gonna pass in our update promises we're gonna go ahead and save that and then down here at the bottom we're gonna run our firebase deploy def - only functions and once this is complete I'll be right back ok so our function has been created successfully so let's head back over to firebase you'll see here in the reviews we have the username of the diligent dev let's say a user goes and updates their profile let's just change this to diligent dev and we'll hit update we'll give it one second here and then we'll go back to our reviews collection and as you can see the username has updated so if we go back to our users collection and we put the in front of it again and hit updates and go back to our reviews collection you'll see that the username has also updated here so as you can see this is a great way to sync your duplicate data inside of your database ok so the last firestore trigger we're going to cover is on delete and this fires when a document is deleted so what we're gonna do for this is we have a post collection here and you'll see that it's got some content a title and some images now when we delete this post out of this collection wouldn't it be nice if it would go to our storage account and into this post bucket and also delete these posts well this is what we're gonna cover right now okay so under our on user update function we're going to write exports dot on post delete and we're to set that equal to functions firestore dot documents and we're gonna reference the posts collection and post ID and then we're to say on delete and we're gonna pass in snapshots and context and set that equal to an arrow function at the top will say Const deleted post equals snap dot data and then we're gonna go ahead and delete our images now in order to do that we're going to declare an empty array called delete promises set that equal to an empty array and then where I say Const buckets equals admin dot storage dot bucket and then we're gonna say deleted post dot images for each pass in an image and now we're gonna push into our deleted promises array so say deleted promises dot push bucket dot file pass in the image and then we'll say delete and then underneath our for each we'll just say oh wait promise that all delete promises and in order to have this await this we will have to make this an async function so we're gonna go ahead and save everything and then we're gonna run our command of firebase deploy - nationally functions and once this is done running I will be right back so our firebase function has deployed successfully I'm back here at firebase I'm in the post collection I'm gonna go ahead and delete this document I'm gonna give it a second for the our store trigger to execute will go to our storage will go into posts and you'll see that there are no files here any longer so it's going to go ahead and wrap up our fire store triggers tutorial if you got any value out of this make sure you hit that like and subscribe button it really helps out my channel and until next time happy coding [Music]
Info
Channel: Diligent Dev
Views: 10,935
Rating: undefined out of 5
Keywords: Firebase Functions Tutorial, Cloud Firestore Triggers, Firebase, firebase cloud function tutorial, firebase functions database
Id: WrzvBulgBi0
Channel Id: undefined
Length: 16min 6sec (966 seconds)
Published: Wed May 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.