Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
How to Persist Data from Facebook15:08 with Jason Seifer
Once we have our application talking Facebook, we need to store the data somewhere. This video shows us how to get our application talking to a database to persist the data we get from Facebook.
[??] [CODE/RACER - Database Access] 0:00 In our last set of videos, we learned how to sign in and authenticate with Facebook. 0:05 In this video, we're going to save that information from Facebook 0:11 to our own local database. 0:15 In order to do this, we're going to use a project called Sequelize. 0:18 Sequelize is an object-relational-mapper for node.js. 0:23 In order to install it, we have to add it to our packages, 0:28 so let's open up our package.json and add it in. 0:33 Save the file and then run npm install to install it. 0:45 Great. Now we have Sequelize installed. 1:02 Let's tell our application how to use it. 1:05 First, we'll open up our app.coffee and import it. 1:08 Now let's take a look at the documentation and see what we do next. 1:23 It looks like we have to configure it. 1:27 Let's go ahead and open up our configuration file that we made earlier. 1:30 Now we'll add our database credentials into this configuration file. 1:36 Since we're going to be working in development, 1:45 I'm going to create a key for development right here. 1:49 This way, when we go to deploy it, we can specify a production environment as well. 1:53 We're going to use the MySQL database locally to work with it, 2:21 and we'll specify that we're logging. 2:26 This helps a lot when trying to debug errors. 2:30 Now let's go back and see how we initialize Sequelize. 2:36 When initializing it, we'll give it the keys that we just specified over in our configuration file. 2:46 In this part down here, the define key says timestamps are true. 3:30 What that will do is add a created_at and updated_at field to each of our models. 3:36 Every time a model is created or saved, those fields will be updated automatically. 3:42 Now that we've got Sequelize installed, let's create a User model 3:47 to save our Facebook data. 3:51 I'm going to do that back on the console and make a models directory. 3:53 Inside of the models directory is where I'll put the user file. 4:00 Now let's open up the User model. 4:09 Let's refer back to the Sequelize documentation to see how we create a model. 4:13 It looks like we use the define keyword. 4:19 We're not going to use any validations right now. 4:23 Since we're storing our model in a separate file, 4:28 it looks like we're going to have to use the import method. 4:31 Before we create our model, we need to create our database 4:34 and also add a table to it. 4:38 I'm going to use Sequel Pro in order to do that. 4:42 However, that's not completely necessary. 4:45 Now we're going to create our users table. 4:53 We're only going to save a few attributes that we get back from Facebook. 5:06 For right now I'll just save the Facebook ID, the first name, last name, and email. 5:10 In a real application, you would probably want to use an unsigned integer for the Facebook ID. 5:32 However, since this is just an example, using a varchar is going to be a lot easier. 5:38 Now that we've got our database set up, let's go ahead and create our model. 5:44 Since our User model is in a separate file, we're going to have to export that information. 5:49 Now let's refer back to the documentation. 6:04 It looks like we have to give it a model and then say the different data types. 6:07 Here we'll enter all of the information we put in the database. 6:24 We're going to want to save certain information that we get back from Facebook. 6:46 We'll do this using a custom method inside of our User model. 6:51 If we look back at the documentation in Sequelize, 6:55 we can see that we can define class methods and instance methods. 6:58 We'll go ahead and define a class method called findOrCreateFromMetadata. 7:03 What this will do is save the metadata from Facebook. 7:09 Or if a user with the Facebook ID already exists, 7:13 it will return that user. 7:17 This way we won't duplicate information inside of the database. 7:20 First, we'll look up to see if the Facebook ID exists. 7:35 If the query is successful, we'll store that information inside of a user object. 7:53 However, this might not actually exist inside of the database. 7:59 The success callback defines whether or not the query was successful. 8:05 So if the user doesn't exist, we'll build and save it. 8:11 At this point we're going to need access to the User model. 8:17 We can't call this.build in order to create our user 8:23 because this now refers to the success callback. 8:28 So what we'll do is define a variable called model up here 8:32 that will have the binding of the User model, 8:37 and then we change the reference over in our build function. 8:45 Now we'll go ahead and assign the metadata to our different database attributes. 8:48 Once we've built the model, we'll save it. 9:17 And if it's successful, we'll assign that again to a user object and run the callback. 9:22 If it fails, we'll go ahead and log the error 9:38 and we'll run the callback with an error and no object. 9:45 Now, that's what happens if we don't have a user in the database. 9:55 If we do, the user should be returned, 9:59 so we'll just run the callback and send in that user object. 10:02 And finally, we need to tell Node what to do 10:09 in case there's an error when querying the database. 10:11 That looks good for now. 10:22 Let's go ahead back to our application.coffee. 10:24 What we have to do is import the User model we just created. 10:29 Now let's scroll down to our authentication portion. 10:47 Instead of just rendering a template saying that we've authenticated, 10:51 what we'll do is call our findOrCreateFromMetadata 10:55 and send in the body of the Facebook response. 10:59 We also have to send in our error and object callbacks. 11:11 So let's see if the user exists. 11:23 In the case that the user does exist, we'll set a user ID session variable 11:34 and redirect to an endpoint called /me. 11:40 We haven't made that endpoint yet, but we will. 11:50 For now, if the user doesn't exist, we'll just redirect them to the homepage. 12:02 Finally, we'll create our /me endpoint. 12:11 First, we'll check to see if there's a user ID key inside of the session. 12:20 If there is a user, we'll go ahead and create a User.findById method. 12:30 You'll notice that we're rendering this me.eco template 12:58 in the event that we find the user. 13:02 Let's go ahead and create that. 13:05 It looks like we have an error on line 13 of our application file. 13:11 Let's go ahead and see what's going on. 13:16 It seems like I've messed up the indentation here when defining our Sequelize variables. 13:19 Let's go ahead and try and run it again. 13:34 So we load up our homepage and click Log In. 13:37 I'm already logged in to Facebook, so it won't ask me to authorize the application. 13:41 It seems our server died. Let's see what happened. 13:48 It says we have no method findOrCreateFromMetadata. 13:51 That's going to be in the User model. Let's open that up and see what's going on. 13:55 It looks like I forgot the classMethods argument. 14:01 Let's try it again. 14:19 It looks like we have another error saying that we have no method findById. 14:25 A quick look in the User model shows that I misspelled it. 14:30 Let's correct it and try again. 14:34 It would seem once more that we're indented just a bit too much. 14:40 These are some of the quirks of CoffeeScript that you have to get used to. 14:45 Here I have another typo. 14:51 And we finally got that to work. 14:57 Let's go ahead and look inside of our database. 15:00 All of our content is there. 15:05
You need to sign up for Treehouse in order to download course files.Sign up