Introduction to MongoDB Schema & CRUD Exercise
Transcript from the "Schema & CRUD Exercise" Lesson
>> Scott Moss: I've been talking too much. I want to get you all into the exercise. So, if you look at the read me, here's what you're gonna be doing.
>> Scott Moss: If you go down to the section that's called models underneath the exercises, the location for that is gonna be in the exercises/models folder.
[00:00:22] And here's some important commands that you'll have to use to actually work with this exercise. There's really only one and it's a test command. And if you have Yarn, you could run this command. If you have npm, you can run this command. Either one's gonna run test for this exercise, and it's only gonna run test for this exercise and not test in other exercises, which was a big problem.
[00:00:40] So it's only gonna run tests for this one and you don't have to worry about any of your other ones. So once you have that, and you understand what's going on here, basically this exercise is going to have you create connection logic. Which is what we talked about how to connect to MongoDB.
[00:00:53] And then you're going to create some schemas. And then you're going to use those schemas, you're going to create some functionality. That's it. So on this check list, first you need to do is check out to the start branch. If you haven't already, check out to that branch, install the node models with npm or yarn.
[00:01:08] If you haven't already, install those. Check the readme, which we're looking at right now. But refer to this readme if you need to figure out what the test command was, if you have a problem with installation, any links or resources I talked about is gonna be in this readme.
[00:01:22] And then, getting to the code, the first thing you're actually gonna do is create connection logic inside connect.js. So if you go to connect.js, right now, it's just returning promise.reject. It's doing that on purpose because I need it to fail on a test. If I didn't put anything here, the test would just hang, and you wouldn't know what to do.
[00:01:39] So right now, it's literally just throwing out a promise.reject and hope that it just breaks. So you need to fix that to where, given that URL that it's seeing, it connects to a Mongo database. So you should just take a URL and connect to a database. That's what you should do.
[00:01:52] So that's the first thing you need to do. The second thing you need to do is finish the user schema so that the user model test passes. So if you go inside of Models, User, there's a schema in here and it's got some fields on it. It looks pretty complete.
[00:02:09] There's some stuff in here, but if you run a for it, it's gonna fail. So you gotta read the test, the test is very descriptive about what it's asking for. You need to fulfil those needs for the test. It's gonna say, we expected this, this should have been this.
[00:02:20] Just make the test pass for the schema. And the test, like I said, are pretty. But you can even go look at the test in the __test__ folder inside of user spec. All the tests you will see inside of here. So if you still can't figure it out, read the text, and if not I'll help you out.
[00:02:36] But they're pretty descriptive. So once you got that, the last thing you need to do is complete the crud functions with the user model, and get all the crud tests to pass. So inside that same folder there is a crud.js, and there's just a whole bunch of empty functions.
[00:02:53] There's a getUserById, getAllUsers, createUser, removeUserById, and updateUserById. And these paths and these arguments, there's test for all of these so all those test have to pass as well. And you're just going to be using the user model based on the schema that you already fixed to fufill these, and they're mostly just generic crud.
[00:03:14] And again, you'd know if you passed if all the test pass and, again, the test are very specific. You can look at the test. You can even look at a test to help you out with some of the answers for some of the other ones. Because I have to mock a lot of these out in the test so you can look at how I'm creating stuff to kind of get you tips on what you should be doing.
[00:03:28] So if you run the test, so if you go to the package.json, I'm sorry, the readme, so many files. I use yarn, so I'm going to use the yarn command. And then I'm just going to say this. So what you're going to do is, one, it's only going to run the two tests inside of models.
[00:03:51] So we've got a crowd spec and then we have a user spec. And you're using Jes, so Jes runs the test in parallel. And later on, I'm gonna talk about how I set all this up cuz this is actually very advanced how to set up Mongo with jess and to get everything to work in parallel.
[00:04:04] But I don't want you to get distracted by that, but yes, the tests are running in parallel. If you try to do a console.log in a test and you don't see it, it's because it's running in parallel in different environments and it's not gonna spit out the logs.
[00:04:15] So I just want you to all be aware of that. So yeah, once you run the test, everything is just going to fail. You're going to see user crud. This expected something it failed, failed, failed. And you can see that it's literally pointing to where it's failing. And this is going to be for the user crud, which is the crud file.
[00:04:32] And then if you scroll down, depending on what order your test executed in, you're gonna see one for the user model. And again, there's a few tests here that have to pass and they're very descriptive as well. First name must be required, last name must be required. Email must be required, email must be unique.
[00:04:46] They're very descriptive on what they're asking, and it's gonna run all the tests for that. All you have to do is just fill out the code, you don't have to connect it back to the test. The test is already connected, it's gonna run everything. You just got to listen to what it is saying.
[00:04:58] And once this is all good, you are going to get all green here. And you should have 11 passed tests with 2 passed sweets.