1 00:00:00,240 --> 00:00:04,916 At the top of the articles.js routes file, there's the instance methods that 2 00:00:04,916 --> 00:00:09,755 required to format dates and produce short descriptions on the article's page. 3 00:00:12,866 --> 00:00:16,343 An instance method is a method that provides functionality for 4 00:00:16,343 --> 00:00:18,220 an instance of a model. 5 00:00:18,220 --> 00:00:22,880 For example, we want each instance to generate a published update and 6 00:00:22,880 --> 00:00:24,710 a short description. 7 00:00:24,710 --> 00:00:29,260 These functions should be invoked in the context of each model hence the name, 8 00:00:29,260 --> 00:00:30,330 Instance Methods. 9 00:00:33,450 --> 00:00:40,843 Let's cut these two functions and the date format dependency from the routes file. 10 00:00:45,926 --> 00:00:48,760 And put them in the article model file. 11 00:00:49,810 --> 00:00:53,100 Having these functions in the routes file isn't encapsulating 12 00:00:53,100 --> 00:00:55,090 the functionality where it should be. 13 00:00:55,090 --> 00:00:56,780 It's better suited here. 14 00:00:56,780 --> 00:01:00,869 When you define a sequelize model, you define its attributes, 15 00:01:02,800 --> 00:01:07,660 class methods, and instance methods. 16 00:01:07,660 --> 00:01:11,520 A class method is a helper method that doesn't require an instance. 17 00:01:11,520 --> 00:01:15,410 You've already used some examples of the class methods with the create and 18 00:01:15,410 --> 00:01:16,250 build methods. 19 00:01:17,590 --> 00:01:19,247 These are provided by sequelize. 20 00:01:19,247 --> 00:01:22,360 They on an instance of an object but 21 00:01:22,360 --> 00:01:27,420 they're related to the articles in general, not a given instance. 22 00:01:27,420 --> 00:01:31,680 If you want to add functionality that doesn't require a particular instance but 23 00:01:31,680 --> 00:01:37,250 is related to the class of objects, you can define custom class methods here. 24 00:01:37,250 --> 00:01:39,987 Where I wanted to define instance methods though, 25 00:01:39,987 --> 00:01:42,798 you can do that after the class method definitions. 26 00:02:14,398 --> 00:02:19,390 We can literally move these functions into the instance methods objects. 27 00:02:19,390 --> 00:02:24,017 And these methods will be applied to all instances of articles. 28 00:02:35,265 --> 00:02:36,660 And that's it. 29 00:02:36,660 --> 00:02:40,050 Before we work on the code for retrieving database records, 30 00:02:40,050 --> 00:02:44,580 I wanted to point out this, the createdAt attribute. 31 00:02:44,580 --> 00:02:49,118 Most ORMs create two fields in a table, createdAt and updatedAt, 32 00:02:49,118 --> 00:02:54,850 that gets automatically updated when the model is created or updated. 33 00:02:54,850 --> 00:03:00,508 I used createdAt to generate a publishedAt date in a nice human readable format. 34 00:03:00,508 --> 00:03:04,417 Most ORM libraries handle these time stamps out of the box. 35 00:03:04,417 --> 00:03:08,920 You don't need to worry about them, but they are really useful. 36 00:03:08,920 --> 00:03:14,310 For example, when you list articles, we can order by the createdAt attribute. 37 00:03:14,310 --> 00:03:19,190 We want them all recent articles first and that's what we'll do in the next video.