1 00:00:00,318 --> 00:00:04,890 You've configured sequelize, initialized a connection to the SQLite database, and 2 00:00:04,890 --> 00:00:07,620 synced the article model with the database. 3 00:00:07,620 --> 00:00:10,920 In this video, we'll work on the routes for data creation. 4 00:00:10,920 --> 00:00:15,880 We'll start by including the article model at the top of the articles.js routes file. 5 00:00:15,880 --> 00:00:21,190 Require the index file of the models folder with const 6 00:00:21,190 --> 00:00:26,739 Article = require, passing it the path to the folder. 7 00:00:28,615 --> 00:00:33,448 We can access each model that we define via a property that gets imported from 8 00:00:33,448 --> 00:00:35,960 the code in the models index.js file. 9 00:00:35,960 --> 00:00:41,200 We want to import the article model, so we'll access it with .Article. 10 00:00:42,560 --> 00:00:47,540 Now we're able to use the article model and all the associated ORM methods you've 11 00:00:47,540 --> 00:00:51,240 worked with before, like find all, create, update, destroy, and so on. 12 00:00:52,280 --> 00:00:57,200 The articles.js file contains several routes to handle HTTP requests, 13 00:00:57,200 --> 00:00:59,050 like POST and GET. 14 00:00:59,050 --> 00:01:03,226 The handler functions execute when a route matches the path defined. 15 00:01:03,226 --> 00:01:05,170 Since sequelize is promise-based, 16 00:01:05,170 --> 00:01:08,180 most of the handlers are going to perform asynchronous actions to 17 00:01:08,180 --> 00:01:11,625 make calls to the database and execute all the CRUD operations. 18 00:01:11,625 --> 00:01:14,411 We're going to use the async/await syntax for 19 00:01:14,411 --> 00:01:17,155 all asynchronous calls made with sequelize. 20 00:01:17,155 --> 00:01:20,905 And we want to use a try/catch statement in each handler 21 00:01:20,905 --> 00:01:25,107 to run the code that needs to be executed and catch any exceptions that are thrown. 22 00:01:25,107 --> 00:01:28,201 So because of the necessity of the try/catch block for 23 00:01:28,201 --> 00:01:32,417 error handling, our code can get quite verbose and unwieldy. 24 00:01:32,417 --> 00:01:37,927 So I'm abstracting away the try/catch block here in the asyncHandler function, 25 00:01:37,927 --> 00:01:42,447 which acts as a middleware that wraps each of our wrap handlers in a try/catch block. 26 00:01:42,447 --> 00:01:45,447 That way we won't have to explicitly write try/catch over and 27 00:01:45,447 --> 00:01:48,090 over again when making our sequelize calls. 28 00:01:48,090 --> 00:01:53,720 The catch statement will send a 500 error or internal server error to the user 29 00:01:53,720 --> 00:01:59,300 if an exception is thrown in the try block, or in any of the handler functions. 30 00:01:59,300 --> 00:02:03,429 You can learn more about the asyncHandler function in the teacher's notes with 31 00:02:03,429 --> 00:02:04,113 this video. 32 00:02:04,113 --> 00:02:07,550 All right, we're ready to work on getting data into the database. 33 00:02:07,550 --> 00:02:11,950 You create a new article by clicking the Create Article button on the main page. 34 00:02:11,950 --> 00:02:13,290 You can see the server side code for 35 00:02:13,290 --> 00:02:18,090 this page in articles.js under Create a new article form. 36 00:02:18,090 --> 00:02:24,100 When the path is articles/new, the app renders the new view where the article or 37 00:02:24,100 --> 00:02:26,714 locals object is an empty object. 38 00:02:26,714 --> 00:02:31,480 The post route under POST create article is the route responsible for creating and 39 00:02:31,480 --> 00:02:33,050 posting a new article. 40 00:02:33,050 --> 00:02:36,260 Currently it just redirects to the main articles page. 41 00:02:36,260 --> 00:02:39,690 So we'll begin here by working on the creation of rows. 42 00:02:39,690 --> 00:02:44,710 You've learned that the sequelize create method builds a new model instance, 43 00:02:44,710 --> 00:02:47,020 which represents a database row and 44 00:02:47,020 --> 00:02:49,960 automatically stores its data in the database. 45 00:02:49,960 --> 00:02:52,830 Create is an asynchronous call that returns a promise. 46 00:02:52,830 --> 00:02:57,770 And inside an async function, you use the await keyword to wait for a promise. 47 00:02:57,770 --> 00:03:02,963 So inside this handler function, initialize a constant 48 00:03:02,963 --> 00:03:07,731 variable named article to await Article.create. 49 00:03:10,626 --> 00:03:16,718 Create requires an object with properties that map to the model attributes, 50 00:03:16,718 --> 00:03:21,330 or the ones defined here in Article.init, for example. 51 00:03:21,330 --> 00:03:27,050 The request body property returns an object containing the key value 52 00:03:27,050 --> 00:03:30,660 pairs of data submitted in the request body, in other words, the form data. 53 00:03:30,660 --> 00:03:34,150 For example, I'll comment out the article variable. 54 00:03:34,150 --> 00:03:37,530 And if I log req.body to the console, 55 00:03:39,050 --> 00:03:44,170 notice that it's an object which maps to the properties of the form inputs and 56 00:03:44,170 --> 00:03:48,810 the properties associated with the article model, title, author, and body. 57 00:03:48,810 --> 00:03:55,072 So we'll pass create the request body with req.body. 58 00:03:56,464 --> 00:03:59,796 When the database builds and saves the new article record, 59 00:03:59,796 --> 00:04:03,335 the app should redirect to the newly created article. 60 00:04:03,335 --> 00:04:08,141 Sequelize generates an auto-incrementing ID for each model instance or 61 00:04:08,141 --> 00:04:09,710 entry created. 62 00:04:09,710 --> 00:04:14,548 So in the res.redirect method, I'll add the article id to 63 00:04:14,548 --> 00:04:19,888 the URL path by concatenating article.id to the articles path. 64 00:04:21,675 --> 00:04:25,115 Over in the browser, I'll click Create Article, 65 00:04:25,115 --> 00:04:29,036 then type some sample data in the Create New Article form. 66 00:04:38,131 --> 00:04:44,540 When I click Submit, I'm redirected to the URL path articles/1. 67 00:04:44,540 --> 00:04:47,530 The article data does not render to the view since we're not yet 68 00:04:47,530 --> 00:04:49,420 retrieving the data from the database. 69 00:04:49,420 --> 00:04:51,390 We'll do that in the next video. 70 00:04:51,390 --> 00:04:55,120 But I'll quickly check if the new entry was added to the articles table, 71 00:04:55,120 --> 00:05:00,470 using the DB Browser for SQLite app, which you may have used in a previous course. 72 00:05:00,470 --> 00:05:04,850 I'll open the development.db file in the app. 73 00:05:04,850 --> 00:05:10,700 And clicking Browse Data shows the new article entry with an id of 1. 74 00:05:10,700 --> 00:05:14,370 We see the title, author, and body data, 75 00:05:14,370 --> 00:05:18,565 as well as the timestamps generated by sequelize, it worked! 76 00:05:18,565 --> 00:05:21,698 In the next video, we'll start reading from the database. 77 00:05:21,698 --> 00:05:25,240 That way we'll be able to view all the articles on the main page and 78 00:05:25,240 --> 00:05:26,500 individually by their ID.