Creating Entries in a Table5:04 with Andrew Chalkley
In this video we'll use Sequelize to add rows of date in to a table.
First and foremost, 0:00 we need to include the article model in our articles JS routes writes file. 0:02 Each model that we define is a property loaded in from the code 0:29 in the index.js file that's in our models folder. 0:33 This will let us use the article model and all the associated ORM methods, 0:38 like finding, updating, creating and deleting database records. 0:42 Let's work on getting data into a database. 1:01 Make sure your database is running. 1:05 And let's go to the Create New Article form. 1:13 Let's review the server code for this page. 1:17 This is an empty object. 1:28 ORMs can generate instances of objects with empty properties 1:31 that still retain the methods and functionality of an ORM object. 1:35 The way we generate an empty instance of the model, you can use the Build method. 1:40 And it's not just the ORM functionality that's included when 1:50 you build an instance, but any functionality that you define. 1:55 For example, we'll be adding a published out method for date formatting. 2:00 And a short description that shows a brief excerpt of our post. 2:04 We'll be adding this functionality to our model in the form of instance 2:08 methods later. 2:12 Next we want to work on the creating of rows. 2:26 We're looking at the HTTP post routes, and 2:32 all this code up here is my code for creating objects in memory. 2:35 The key part of this is the rec.body. 2:39 This is the data from the form. 2:42 This maps one to one to the properties from the form inputs and 2:45 the properties on the article models. 2:49 These two lines add instance methods to the article. 2:56 To display the published at time and short descriptions on each article created. 2:59 This is a crude approach and 3:05 would refine this later when we look at instance methods. 3:07 To create a model, we can use the Create method. 3:10 It requires an object with properties that map to the properties of the object. 3:22 Remember the request body is such an object. 3:28 Then when the database is finished saving the new article record, 3:52 the database will redirect to the new article. 3:57 The article with the new id that's been also incremented. 4:03 I accidentally did the plural articles, it should be just be article. 4:07 We don't want to make a mistake there. 4:12 When we restart the server. 4:15 And type in some sample data into the Create form and Submit. 4:20 We see that we are redirected to the static article with the ID of one, 4:29 since we're not retrieving it from the database yet. 4:33 Before we look at the ORM code for finding articles, 4:37 we should look at including the instance methods in our sequelize objects. 4:40 If we don't include them, all of the routes that show articles or 4:45 individual entries will break since the views require instance methods on each of 4:49 these article instances. 4:53 In the next video, we'll see how to do this. 5:01
You need to sign up for Treehouse in order to download course files.Sign up