1 00:00:00,820 --> 00:00:02,330 Enough lecturing and talking. 2 00:00:02,350 --> 00:00:05,290 Let's go increase our actual user model. 3 00:00:05,290 --> 00:00:10,270 So back inside my code editor I'm going to find my off service directory. 4 00:00:10,340 --> 00:00:12,900 It is right here inside the NRC folder. 5 00:00:13,030 --> 00:00:15,730 I'm going to create another directory called models. 6 00:00:16,090 --> 00:00:19,680 And inside there I'll create a file called user dot. 7 00:00:19,710 --> 00:00:21,000 Yes. 8 00:00:21,130 --> 00:00:24,850 So this is where we are going to define our Mongoose user model. 9 00:00:24,850 --> 00:00:28,300 We're also going to write out some code inside of here to solve those two issues that we just discussed 10 00:00:29,260 --> 00:00:30,670 to get started at the very top. 11 00:00:30,670 --> 00:00:36,240 I'm going to import Mongoose from Mongoose after that. 12 00:00:36,250 --> 00:00:42,310 I'm going to define something called a schema a schema is how we are going to tell Mongoose specifically 13 00:00:42,520 --> 00:00:46,360 about all the different properties that a user is going to have. 14 00:00:46,520 --> 00:00:54,320 I'm gonna write out to user schema is new Mongoose dot schema and then sort of here again we're going 15 00:00:54,320 --> 00:00:58,180 to list out the different properties that a user is going to have. 16 00:00:58,190 --> 00:01:05,190 First off they're going to have an email I'm going to markets type as being a string and I'm going to 17 00:01:05,190 --> 00:01:07,760 say that this property is required required. 18 00:01:07,810 --> 00:01:13,290 True that quick little note in those we put a type right here and we said that its type was going to 19 00:01:13,290 --> 00:01:14,250 be string. 20 00:01:14,310 --> 00:01:17,020 This is not in any way shape or form. 21 00:01:17,100 --> 00:01:23,310 Not whatsoever tied to typescript the type that we are listing right here is 100 percent specific to 22 00:01:23,310 --> 00:01:24,180 mongoose. 23 00:01:24,180 --> 00:01:28,240 It does not tell typescript anything whatsoever. 24 00:01:28,260 --> 00:01:33,030 The other thing I want to mention is that when you put a type of string right here it is capital S string 25 00:01:33,090 --> 00:01:38,130 because we are referring to the built in string constructor that is available inside the javascript 26 00:01:38,130 --> 00:01:42,420 language whenever we write out a type of string using typescript. 27 00:01:42,420 --> 00:01:45,560 So for example if we wrote out an interface you're really quickly. 28 00:01:45,610 --> 00:01:51,540 And when it had some imaginary property that was of type String we'd write out lowercase string. 29 00:01:51,830 --> 00:01:57,210 So in typescript whenever we want to indicate a type of string it's lowercase with Mongoose when we 30 00:01:57,210 --> 00:02:06,320 indicate a type of a property it is a capital because we are referring to an actual constructor. 31 00:02:06,400 --> 00:02:12,170 So after that the other property we're going to eventually store on all these different users is a password. 32 00:02:12,200 --> 00:02:16,940 This will also be of type String and it will be also required 33 00:02:20,480 --> 00:02:20,720 all right. 34 00:02:20,720 --> 00:02:22,470 So that looks good. 35 00:02:22,500 --> 00:02:28,020 So now after creating that schema we can see the schema into Mongoose and mongoose is going to create 36 00:02:28,050 --> 00:02:29,510 a new model out of it. 37 00:02:29,520 --> 00:02:34,830 And remember this whole model thing is how we actually access a whole big set of data inside of our 38 00:02:34,830 --> 00:02:37,160 Mongo DB database so I can stay down here. 39 00:02:37,170 --> 00:02:45,910 The bottom line user is Mongoose dot model I'm gonna put in a string of user and then feed in the user 40 00:02:45,910 --> 00:02:50,780 schema like so then finally at the very bottom I'm going to export user 41 00:02:53,630 --> 00:02:53,850 okay. 42 00:02:53,880 --> 00:02:59,220 So this time well we wrote out all this code and everything appears to be working no without any issue 43 00:02:59,220 --> 00:03:03,830 whatsoever I don't see any typescript errors or anything like that. 44 00:03:03,840 --> 00:03:08,160 However if we start to really play around with this user model that we've now created you can really 45 00:03:08,160 --> 00:03:12,740 start to see where these two issues are going to start to cause us some problems. 46 00:03:12,750 --> 00:03:18,440 For example let's just try to create a new user so after creating the user model down here I'm going 47 00:03:18,440 --> 00:03:24,250 to say new user and I'll pass on some properties I want to assign to this user. 48 00:03:24,300 --> 00:03:29,100 Well naturally we probably want to have an email and a password so I could put in something like an 49 00:03:29,100 --> 00:03:36,470 email of test at test dot com and a password of whatever well that looks fine. 50 00:03:36,470 --> 00:03:39,700 We still don't see any errors from typescript or anything like that. 51 00:03:39,710 --> 00:03:42,380 However here's where stuff starts to go wrong. 52 00:03:42,500 --> 00:03:47,270 There is no information about the arguments that are going to be provided to this constructor being 53 00:03:47,270 --> 00:03:49,340 fed into typescript right now. 54 00:03:49,370 --> 00:03:53,810 So even though you and I know that we want to give that constructor an email and a password there are 55 00:03:53,810 --> 00:03:58,700 both strings because that's what we told Mongoose typescript has no idea that that is the case. 56 00:03:59,480 --> 00:04:05,180 So if I start to mangle these properties if I accidentally put in like just yes typescript says hey 57 00:04:05,210 --> 00:04:06,660 who cares. 58 00:04:06,680 --> 00:04:07,610 No issue whatsoever. 59 00:04:07,610 --> 00:04:10,110 We don't get any warnings going to get any errors. 60 00:04:10,280 --> 00:04:12,890 I can add in extra properties. 61 00:04:12,890 --> 00:04:13,800 No problem. 62 00:04:14,030 --> 00:04:18,220 I can even change this from a string to a number. 63 00:04:18,530 --> 00:04:24,850 Also no issue whatsoever so you can start to see it right away that typescript has no idea about what 64 00:04:24,850 --> 00:04:29,390 is going on with the arguments you're passing to this constructor the whole reason that we are using 65 00:04:29,390 --> 00:04:34,250 typescript is so that it can check stuff like this and make sure that we are not making silly typos 66 00:04:34,280 --> 00:04:35,750 or anything like that. 67 00:04:35,930 --> 00:04:40,430 So it would be great if we could somehow teach typescript about the different properties that were going 68 00:04:40,430 --> 00:04:41,620 to pass into this thing. 69 00:04:41,720 --> 00:04:45,440 So it can do some validation or something like that for us. 70 00:04:45,470 --> 00:04:49,370 All right so now that we understand the issue here let's take a quick pause and make the next video 71 00:04:49,460 --> 00:04:53,690 and start to discuss how we're going to get TypeScript and Mongoose to actually work together.