Transcript from the "Virtuals, Hooks & Indexes Exercise" Lesson
>> Scott Moss: All right, so speaking of exercises, we're gonna jump into the next one. So if you look inside the hooks folder, you're going to have a org file, org.js, which is just a org schema, and you're gonna have a project schema. And they're both pretty much filled out, they've got some good stuff on them.
[00:00:20] But just like the rest of the exercises, you're gonna have some tests that you have to pass. So if we hop into the README and we,
>> Scott Moss: Preview that, and we go down to what's happening here, so again, you're gonna have a different test command. It's just gonna be exercises/hook/__test__, just so we could run those tests and not any other ones.
[00:00:41] And basically, in this exercise you're gonna learn how to use schema middleware and virtuals, which is exactly what we just talked about and what I walked you all through. Also, you'll dig into the indexes a little more in detail and talk about compound indexes. So that's what we just did, so now you're gonna have to showcase those skills.
[00:00:56] And just like before, check out to the start branch, reference the README for any links or resources or help that I might have talked about. And as far as the tasks go, there's more tasks here, but I promise you that they're not as difficult as the last one.
[00:01:09] So first thing is you're gonna add a compound index to the project schema so that the project names are unique per org. Very similar to what we just did, where I made the school names unique per district, you're gonna make the project names unique per org. Second thing you're gonna do is add a virtual getter to the project schema called bucketLeft, and that's gonna calculate how much budget is left versus how much budget has spent so far.
[00:01:34] I'm gonna walk you through that in a minute, cuz you haven't seen these models before, so you might not know what I'm talking about. And the next thing you're gonna do is add a post remove a hook to the org schema that removes all projects associated with the org.
[00:01:46] So again, if you have associations set up on a organization and you delete an organization, every party that's referencing that deleted organization needs to get deleted, too. So you need to write a middleware for that, or a hook. Add a virtual getter to the org schema called avatar that creates the full url to the org avatar by concatenating the cdnURL with the org id.
[00:02:08] That sounds very complicated, but in reality, you're just making a string that adds a string inside of a virtual. And I'll walk you through what that looks like. And these need to get all the tests to pass. So I'm gonna walk you through those right quick. So if we look at the org.js, the virtual, what I'm talking about is, and the test will tell you is, you need to take this cdnUrl, right here, and you need to add it to the ID of an org inside of a virtual.
[00:02:32] And that virtual needs to be called avatar. So you need a virtual called avatar that just returns the cdnUrl plus the ID, that's it. So that's one thing. You also need to add another middleware in here, that post remove will go find all of the projects related to the org that just got removed and delete those, too.
[00:02:55] So you gotta do that. So that's gonna be an asynchronous middleware hook that you have to do on post remove. And then inside the project, I believe what you had to do in here was you had to add a virtual called budgetLeft. And basically all it does is, it has a getter on it that when called will just give you the difference between a budget and what was spent.
[00:03:16] That's all it does. So you subtract the spent from the budget and it should return that value. And that should be a virtual called budgetLeft. And then the last one that you had to do was, and in the project,
>> Scott Moss: Where is it? Yeah, add a compound index to the project scheme.
[00:03:36] Yeah, so in the project scheme, this is where you also add a compound index where you want the name of the project to be unique but only scoped to an org. So you add a compound index for that. And those are the four things you had to do.
[00:03:53] If you do all that, all the tests should pass. So if we run these tests, we can do yarn test, or, yarn, yeah, test exercises/hooks/__test__/. If we run those,
>> Scott Moss: You see two test suits and only three tests that pass. So it's not that many tests, but you can see they are pretty descriptive.
[00:04:18] So project names are unique per org, like we said before. This one's expecting that virtual to be there. And then you have some other ones up here, that's expecting the org to move all the projects inside of a hook, so it's testing for those things.