1 00:00:01,050 --> 00:00:05,150 Our Express API is now aware of some information that's going to be sent from. 2 00:00:05,160 --> 00:00:09,280 In theory our phone even though right now we're just testing the API from postmen. 3 00:00:09,420 --> 00:00:14,520 So now that our Express API has that email and password that user is trying to sign up with the express 4 00:00:14,520 --> 00:00:15,010 API. 5 00:00:15,020 --> 00:00:19,590 Now it needs to reach out to Mongo DB and see if anyone has ever signed up with an email of test at 6 00:00:19,590 --> 00:00:21,300 test dot com before. 7 00:00:21,300 --> 00:00:25,710 So in this video we're going to get a better idea of how our Express API is going to work with Mongo 8 00:00:25,710 --> 00:00:26,150 DB. 9 00:00:26,970 --> 00:00:27,200 Okay. 10 00:00:27,210 --> 00:00:28,420 So quick diagram. 11 00:00:28,690 --> 00:00:33,780 So inside of Mongo DB We're going to eventually store some information related to all the users who 12 00:00:33,780 --> 00:00:35,510 create an account with our app. 13 00:00:35,610 --> 00:00:39,960 We're going to store this information inside of something called the user's collection. 14 00:00:39,960 --> 00:00:45,660 You can essentially imagine users collection as being like a array with a ton of objects inside of it 15 00:00:45,960 --> 00:00:49,750 where each object represents one user was created an account. 16 00:00:49,890 --> 00:00:52,760 Now behind the scenes there's not actually an array of objects. 17 00:00:52,890 --> 00:00:56,670 A very different storage mechanism is used in Mongo DB. 18 00:00:56,670 --> 00:00:59,600 Nonetheless we can kind of imagine that is what is happening. 19 00:01:00,000 --> 00:01:04,290 So again inside of our users collection we're gonna have a bunch of different objects that all have 20 00:01:04,350 --> 00:01:11,040 an email and password and those two properties are both going to be strings each of those objects represents 21 00:01:11,100 --> 00:01:15,830 one single user account so that's what's going on inside of Mongo. 22 00:01:15,830 --> 00:01:18,150 That's where our actual information lives. 23 00:01:18,290 --> 00:01:22,360 But in order to work with that data we're making use the library called mongoose. 24 00:01:22,670 --> 00:01:27,200 When we make use of Mongoose we have to instruct it about the different data or the different collections 25 00:01:27,380 --> 00:01:29,120 that we've stored inside of Mongo DB. 26 00:01:29,840 --> 00:01:34,440 So we have to do a little bit of configuration with mongoose before we ever work with Mongo. 27 00:01:34,730 --> 00:01:39,740 Specifically we need to tell Mongoose that there's something called a user's collection and we're going 28 00:01:39,740 --> 00:01:43,120 to do that by creating something called a user's model. 29 00:01:43,160 --> 00:01:48,740 This user's model is going to essentially represent kind of a tie to that collection of data inside 30 00:01:48,740 --> 00:01:49,520 of Mongo DB. 31 00:01:50,270 --> 00:01:55,100 So when we create this user model we're going to tell Mongoose that every user that is stored inside 32 00:01:55,100 --> 00:01:59,330 of our collection up here is going to have an email that's a string and a password that's a string as 33 00:01:59,330 --> 00:01:59,970 well. 34 00:02:00,940 --> 00:02:05,680 Once we create this user model we can then use it to manipulate all the different users we've got stored 35 00:02:05,710 --> 00:02:10,720 inside of Mongo DB So let's flip back over to our code editor. 36 00:02:10,730 --> 00:02:15,920 We're going to write out a little bit of code to tell Mongoose that we want to have this user collection 37 00:02:15,920 --> 00:02:22,730 thing or this mechanism of storing user's so going to flip back over to my editor inside my source directory. 38 00:02:22,740 --> 00:02:32,050 I'll make a new folder called models then inside there I'll make a new file called user dot J.S. then 39 00:02:32,050 --> 00:02:36,180 inside this file where we will first begin by requiring in the Mongoose library. 40 00:02:36,330 --> 00:02:39,490 So I'll say const mongoose is require Mongoose 41 00:02:42,240 --> 00:02:47,040 or then use the Mongoose object to create the idea of a user model. 42 00:02:47,090 --> 00:02:55,460 So going to write out const user schema is new Mongoose start schema and take notice of the capitalization 43 00:02:55,460 --> 00:03:01,580 I have inside of here so lowercase im on Mongoose capital S schema the schema object is where we tell 44 00:03:01,580 --> 00:03:06,900 Mongoose about the different properties that every user inside the user's collection is going to have. 45 00:03:06,980 --> 00:03:11,030 So for you and I like we were just discussing a user is always going to have an email. 46 00:03:11,720 --> 00:03:17,210 Well then assign that property a object to configure some information about what this email property 47 00:03:17,210 --> 00:03:18,860 is supposed to be like. 48 00:03:18,890 --> 00:03:24,600 So again right now we are attempting to tell Mongoose about what different properties every user inside 49 00:03:24,600 --> 00:03:26,330 of our users collection is going to have. 50 00:03:27,580 --> 00:03:32,110 So inside this object we're going to specify the type of data that email is going to reference. 51 00:03:32,210 --> 00:03:39,910 So email is always going to be of type String will then pass on in other configuration flag of unique 52 00:03:41,060 --> 00:03:47,840 is true by adding in this flag mongoose is going to understand that we want every email that is tied 53 00:03:47,840 --> 00:03:51,030 to a user inside of our users collection to always be unique. 54 00:03:51,350 --> 00:03:56,600 And if someone ever tries to save two users with duplicate emails we're gonna get an error message back 55 00:03:58,520 --> 00:04:03,760 and then finally we'll also add in one last flag here of required is true. 56 00:04:03,760 --> 00:04:08,090 So that just means that any time that we tried to save a new user to our users collection inside of 57 00:04:08,090 --> 00:04:13,460 Mongo D.B. if the user does not have an email then they are invalid and we should not be allowed to 58 00:04:13,460 --> 00:04:14,380 save them. 59 00:04:14,390 --> 00:04:20,530 So every user we create has to have an email property now we can go through the same process with a 60 00:04:20,530 --> 00:04:21,830 password property as well. 61 00:04:22,380 --> 00:04:26,620 So I to say that the password must be of type String. 62 00:04:26,670 --> 00:04:32,610 It doesn't have to be unique so to users can have identical password or so identical passwords and then 63 00:04:32,760 --> 00:04:34,530 required is true. 64 00:04:34,530 --> 00:04:38,880 So we want to make sure that every user is created with a password. 65 00:04:38,990 --> 00:04:43,790 So now that we've created this object that essentially defines what a user is we're going to associate 66 00:04:43,790 --> 00:04:44,950 it with mongoose. 67 00:04:45,030 --> 00:04:46,610 It's down to about in the file. 68 00:04:46,760 --> 00:04:59,350 I'll say Mongoose dot model will pass in a string of user and then pass in the user schema. 69 00:04:59,410 --> 00:05:04,360 So again these schema right here tells Mongoose about the kind of structure or data that every user 70 00:05:04,360 --> 00:05:05,500 is going to have. 71 00:05:05,500 --> 00:05:10,350 And this call right here actually associates that with our Mongoose library OK. 72 00:05:10,380 --> 00:05:16,360 So we can now save this file and then going to go back into my index dot J.S. file and at the very top 73 00:05:16,360 --> 00:05:19,920 of this file I'm going to requiring that user file we've just created. 74 00:05:19,990 --> 00:05:27,980 So going to say require we're going gonna go into the models directory and then require in user. 75 00:05:28,000 --> 00:05:30,980 You'll notice that we did not actually assign this required to anything. 76 00:05:31,060 --> 00:05:34,750 We also did not really export anything from user dot J us as well. 77 00:05:34,840 --> 00:05:40,150 The reason for that is that Mongoose really expects to only see that line of code right there executed 78 00:05:40,210 --> 00:05:47,230 exactly one time if we import require this file user dot J.S. into multiple other files inside of our 79 00:05:47,230 --> 00:05:49,320 project every time we require it. 80 00:05:49,420 --> 00:05:54,670 That line right there is going to be executed a second or third or fourth time and you're going to very 81 00:05:54,670 --> 00:05:59,880 quickly see an error message where Mongoose says something like hey you already defined a model called 82 00:05:59,890 --> 00:06:01,970 user you can't define it again. 83 00:06:02,050 --> 00:06:08,630 So to get around that we only require in user dot J.S. into index dot J.S. Right here if we ever then 84 00:06:08,640 --> 00:06:13,530 need to get a reference to our user collection or user model that we just created which we definitely 85 00:06:13,530 --> 00:06:15,400 need inside of auth routes. 86 00:06:15,420 --> 00:06:16,240 Let's go over to all throughout. 87 00:06:16,240 --> 00:06:17,890 Right now I'll show you how to do that. 88 00:06:17,940 --> 00:06:23,910 So inside of here if we need to get access to that user model tied to Mongoose and somehow manipulate 89 00:06:23,940 --> 00:06:28,110 our collection of users we can then import or require in Mongoose at the top 90 00:06:31,320 --> 00:06:37,740 and then to get access to the user model will say const user with the capital you is Mongoose dot model 91 00:06:38,280 --> 00:06:44,040 user like so it's now anywhere inside this file we can get access to that user model which allows us 92 00:06:44,040 --> 00:06:49,940 to access the underlying collection of data that's stored inside of Mongo DB Okay. 93 00:06:50,000 --> 00:06:50,750 So that's pretty much it. 94 00:06:50,750 --> 00:06:56,270 So we've now got our user setup so we can now use this object right here to somehow manipulate that 95 00:06:56,270 --> 00:07:01,850 collection of data so we can use it to see if a given email is already in use we can also use it to 96 00:07:01,850 --> 00:07:04,700 create a new instance of a user as well. 97 00:07:04,710 --> 00:07:06,950 So let's do both those things in the next video. 98 00:07:06,950 --> 00:07:09,110 So a quick pause and I'll see you in just a minute.