Check out a free preview of the full API Design in Node.js, v3 course:
The "Mongoose Schema Exercise" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to write a Schema that ensures that all tests in the suite pass.

Get Unlimited Access Now

Transcript from the "Mongoose Schema Exercise" Lesson

[00:00:00]
>> Scott Moss: I'm just gonna show you some more schema stuff, but not too much, and then you're gonna break off into an exercise where you're gonna be developing the schema and the model for the item resource, and like I said, it will be test associated with it so as long as you get those test to pass, you'll be good, and these tests are very descriptive.

[00:00:19] They literally tell you exactly what you need to do, so even if you had no clue how to do this, the test tells you exactly what it's looking for. So I'm confident that you'll be able to figure it out. So let's do that. So I'm going to check out to,

[00:00:37]
>> Scott Moss: Let's stash this.
>> Scott Moss: Start out with lesson three here.
>> Scott Moss: And basically, lesson three, if you've already check out to the lesson two solution branch, it's the same branch, there's just, if you go look at the item model, there's nothing here. There's a model that's being created, but the schema has no information on it.

[00:01:02] We have to fill some information into this schema here, because right now the item schema has absolutely nothing on it, and the only way to know what you need to put in here is if you execute the test. So if you execute the test, it'll tell you, it's expecting this field, this validation, this name, and you have to fulfill those tests, and this is where you're going to be putting it in.

[00:01:21] You'll be putting it here in this schema here.
>> Scott Moss: So just to give you some examples I'm gonna run the test, and I'm gonna do one for you. So if I say yarn and the test command for this one is going to be npm run test-models I believe, yes.

[00:01:42]
>> Scott Moss: Of course it is going to fail. You can see this looks like about six tests that's failing here, and it's testing for each field name that's supposed to be on the schema. So, for instance, it's saying item model, schema name. It expected it to have a max length of 50, required to be true, trim true, and a type that is string.

[00:02:06] So I can satisfy some of that by just looking at the test. So if I say, okay, there must be a name field here. Its type must be string.
>> Scott Moss: It is required. It has something called trim: true. I have no idea what that is, but that's what it's looking for.

[00:02:25] But basically what that is, that's just saying this is a string, if it has empty spaces before or after, trim it just like if you call trim in JavaScript. You call it .trim in JavaScript, it'll trim the spaces. So that's what it's saying there, and then it has another validation called maxlength of 50, so it's expecting that.

[00:02:44] So that right there might satisfy that test.
>> Scott Moss: So if we scroll up, it looks like we got one to pass, and it should have been that one. So yeah, that one passed. So that's the flow that you're gonna go through to get these fields in the right way that you need them to be.

[00:03:02] So item, like I said, is basically, it's like a to-do item. This is like a very basic API for a to-do app, and you're just filling out these fields for Mongo. So, like I said, check out to lesson three, if we look at the readme, just to go over it one more time.

[00:03:21] You're gonna be creating the schemas. It's on lesson three branch. The test command is gonna test models, where there's yarn or MPM, and the things you're gonna be doing is you're gonna be creating the schema for the item resource, you're gonna add the correct fields, and this says look at the test here, you're gonna add the correct validations, also look at the test, the test does exactly what we are expecting.

[00:03:42] For extra credit, if you know how to use Mongo, or if you just wanna take the extra credit and figure out how to do it, I'll give you a hint if you wanna do it, add a compound index to ensure all the tasks in a list have a unique names.

[00:03:55] You're looking at me like what the hell I'm talking about. That's because I've made it extra credit, so you don't have to do that. You should be looking at me like what am I talking about, cuz that's, you probably only how to do that if you've ever used Mongo before, but yeah, that's basically index.

[00:04:08] So we want all the items to have unique names that belong to a list, so we don't want it to be unique in the database, but we want them to be unique per list. So it's a compound index, it's a little little trickier to do, and then, of course, like always, get all the tests to pass, and if you fulfill all of these, except the extra credit, all the tests will pass.

[00:04:27] There's no test for the extra credit, so you don't have to worry about that one failing.