1 00:00:00,810 --> 00:00:04,560 At the end of last video I mentioned that getting TypeScript and Mongoose to work together is sometimes 2 00:00:04,560 --> 00:00:06,020 just a little bit awkward. 3 00:00:06,030 --> 00:00:10,440 So in this video I want to outline two big issues that we're going to run into just so you are aware 4 00:00:10,440 --> 00:00:14,550 of why we're going to write out some slightly complicated code in just a moment. 5 00:00:14,970 --> 00:00:18,210 So here's the big issues that are going to start to run into. 6 00:00:18,270 --> 00:00:23,160 First I want to give you a quick review on some terminology around Mongoose and Mongo DV inside of Mongo 7 00:00:23,160 --> 00:00:23,750 DV. 8 00:00:23,760 --> 00:00:26,750 We organize information into different collections. 9 00:00:26,940 --> 00:00:32,550 We might have a user collection that will store all the different users who have signed up for our application. 10 00:00:32,550 --> 00:00:38,370 So in this case users on through for each of these can be thought of as little objects that store some 11 00:00:38,370 --> 00:00:43,390 information about a very particular user to access all this data. 12 00:00:43,390 --> 00:00:49,720 We're going to use Mongoose and in particular we are going to create two different things inside a mongoose. 13 00:00:49,720 --> 00:00:53,720 We are first going to create something called a mongoose user model. 14 00:00:53,740 --> 00:00:58,450 This is a class that is going to represent the entire collection of users. 15 00:00:58,630 --> 00:01:05,950 So we will use this user model class to run queries to create new users or to do anything that modifies 16 00:01:05,980 --> 00:01:09,700 or queries the overall collection. 17 00:01:09,700 --> 00:01:15,300 We are also gonna have something called a mongoose user document a user document is also a class. 18 00:01:15,430 --> 00:01:21,610 It is meant to represent one singular user so we might create an instance of a user document to represent 19 00:01:21,820 --> 00:01:28,630 user number 2 or create a second instance or another instance to represent just user number for. 20 00:01:28,670 --> 00:01:34,190 So again we've got a user model that represents all of our data and user document that represents just 21 00:01:34,190 --> 00:01:35,290 one little thing. 22 00:01:37,210 --> 00:01:39,080 So that quick refresher on terminology. 23 00:01:39,100 --> 00:01:42,910 Here are the two issues that are going to start to run into when we are getting Mongoose and typescript 24 00:01:42,940 --> 00:01:50,320 to interact with each other so issue number one is going to come up when we start to create a new user 25 00:01:50,350 --> 00:01:51,520 document. 26 00:01:51,520 --> 00:01:56,020 So this is going to be how we eventually try to create a new user document or a new instance of a user 27 00:01:56,450 --> 00:02:03,280 will say new user and then pass in some different properties such as email and password and whenever 28 00:02:03,280 --> 00:02:09,370 we tried to pass in properties to anything typescript wants to understand the properties we are passing 29 00:02:09,370 --> 00:02:14,740 in and somehow make sure that you're passing in the correct type of data the typescript wants to understand 30 00:02:14,890 --> 00:02:19,120 the different arguments that calling new user is going to accept. 31 00:02:19,450 --> 00:02:25,720 However Mongoose does not make this easy behind the scenes or more specifically with the Mongoose type 32 00:02:25,720 --> 00:02:28,330 definition file that we just installed a little bit ago. 33 00:02:28,510 --> 00:02:33,430 Whenever we try to create an instance of a user as you're doing right here it is not going to kind of 34 00:02:33,430 --> 00:02:37,290 tell typescript about the different properties that we are trying to pass in. 35 00:02:37,420 --> 00:02:42,880 And so for example we could make a huge typo inside of here we could accidentally write in hard instead 36 00:02:42,880 --> 00:02:47,920 of password and typescript is not going to complain because typescript is not going to be given enough 37 00:02:47,920 --> 00:02:53,740 information by Mongoose by default to understand the different types of arguments or the names of these 38 00:02:53,740 --> 00:02:59,060 properties we are supposed to pass in but this is issue number one that we are going to Ryan too. 39 00:02:59,330 --> 00:03:01,160 We have a workaround for this. 40 00:03:01,160 --> 00:03:06,110 We're going to essentially somehow teach typescript about the different properties that this constructor 41 00:03:06,140 --> 00:03:06,790 expects. 42 00:03:06,800 --> 00:03:07,850 So we are going to solve this. 43 00:03:07,850 --> 00:03:10,670 I just want you to be aware that this is something we need to be aware of. 44 00:03:12,660 --> 00:03:13,010 OK. 45 00:03:13,050 --> 00:03:14,450 Issue number two. 46 00:03:14,650 --> 00:03:18,540 So with issue number two at some point time we are going to create a new user. 47 00:03:18,870 --> 00:03:25,180 And we might assign it some properties like say email and password that's been going to give us back 48 00:03:25,210 --> 00:03:26,410 a user document. 49 00:03:26,440 --> 00:03:31,840 So that's let's imagine right here a user variable or something like that at some point time later. 50 00:03:31,980 --> 00:03:35,490 We're going to want to access some of the data within this user. 51 00:03:35,560 --> 00:03:41,140 Let's imagine for a second that we add in a console log of user if we did that we might end up seeing 52 00:03:41,320 --> 00:03:47,160 that this user document has more properties than what we provided to the constructor. 53 00:03:47,230 --> 00:03:52,880 So for example we might take that email and password and assign them as properties to user. 54 00:03:52,990 --> 00:03:57,880 But behind the scenes Mongoose might decide to add in a couple of additional properties as well. 55 00:03:57,880 --> 00:04:04,200 Properties like let's say a created at timestamp or in updated that timestamp so the properties that 56 00:04:04,200 --> 00:04:09,240 we pass into the constructor don't necessarily match up with all the properties that are created on 57 00:04:09,240 --> 00:04:10,790 the user documents. 58 00:04:10,800 --> 00:04:15,960 This is also an issue with typescript because typescript wants to know about all the properties that 59 00:04:15,960 --> 00:04:17,830 exist on the user. 60 00:04:17,880 --> 00:04:23,250 So we need to somehow tell typescript hey look we're going to pass some properties into the constructor 61 00:04:23,290 --> 00:04:28,710 typescript but don't worry there are going to be some other properties that get created behind the scenes 62 00:04:28,770 --> 00:04:30,930 and assigned to the user as well. 63 00:04:30,960 --> 00:04:34,920 So we really need to teach typescript that there are kind of two different sets of properties. 64 00:04:34,920 --> 00:04:38,880 There's one set of properties that will be passed to the constructor and there's gonna be a second set 65 00:04:38,880 --> 00:04:44,530 of properties that we should be able to access on the user document that gets created. 66 00:04:44,610 --> 00:04:46,760 That is issues number one and number two. 67 00:04:46,770 --> 00:04:51,340 And again this is some stuff that we're going to have to fight with around Mongoose and typescript. 68 00:04:51,390 --> 00:04:55,800 Now of course if we were just writing javascript yeah we would not really have to worry about this but 69 00:04:55,830 --> 00:04:57,370 there are some very good reason. 70 00:04:57,370 --> 00:05:00,210 So you're using typescript on this micro services project. 71 00:05:00,210 --> 00:05:05,550 We haven't really dove into them just yet but trust me you're going to start to see why we're using 72 00:05:05,550 --> 00:05:10,540 typescript very quickly as soon as we start to create our second service. 73 00:05:10,670 --> 00:05:10,910 All right. 74 00:05:10,910 --> 00:05:12,730 Now we understand these two big challenges. 75 00:05:12,770 --> 00:05:13,640 Another quick pause. 76 00:05:13,660 --> 00:05:17,030 We're gonna start to write out some code that's going to solve both these challenges. 77 00:05:17,090 --> 00:05:19,730 We're going to create our user model and all that good stuff. 78 00:05:19,760 --> 00:05:21,080 So I'll see you in just a minute.