00:10
relationship information like our access level, status, module, and even the module courses? For that, if we jump back into our text editor within our lesson validator, what we're going
00:20
to want to do is add in an additional option, I call this includes, or what I'm going to
00:26
do here is call this relationships, and we can set this to a type of vine array. For this array, we can accept in an enum.
00:33
Now rather than actually passing in a literal enum type as we have previously, we can also just pass in an array of strings that would serve as those enum values.
00:43
So for example, if we wanted to allow status to be defined as a relationship, we just define status as a string within this enum rule.
00:50
We could also do access level and our module, and then to allow our users to also preload
00:56
in the modules course, we could do module.course like so. So if they pass in module.course, we're automatically just going to preload the module there without
01:06
them needing to specifically require the module here within the relationships array. Okay, so with that set, let's go implement it.
01:13
So within our search lessons, underneath our apply filters, let's go ahead and do static, make this private, and call this apply preloads.
01:20
And we'll accept in our query of type query, and we can go ahead and pluck out the relationships
01:25
from our filters just like so by using pick again, just as we did with our publish at. And the first thing that we need to do here is determine how we want this to behave if
01:35
they don't provide any relationships to be included. Do we want to automatically include all of them or do we want to include none of them by default?
01:43
I think for us, it probably makes the most sense to include none of them by default. Since the module.courses can get a little bit terse, providing none might be the cleanest option for this application.
01:53
So we can check if not relationships. Since this is an array, we can also just go ahead and do a nullish check to see if the
02:00
relationship doesn't have a length, then we go ahead and return. From there, we need to check for the specific relationships that we're allowing within our enum.
02:09
So why don't we go ahead and loop over the relationships? So for const relationship of relationships, and we can use a switch statement inside of
02:18
here for the relationship to apply them as needed. So we do a case checking for, say, the status.
02:24
And if we have a status, then we'll do query dot and we'll add our preload in for the status. From there, we want to break to allow this switch to move on to the next case if the
02:34
first one did not match. Now, since we are checking to see whether or not the string is specifically status and status is one of our valid preload options, rather than hard coding that in here, we could
02:44
also just pass the relationship in directly. Since we verified that the string is already a valid preload option, TypeScript will be happy with that.
02:54
That rings true also for our access level. That too is a direct preload option, and so is our module.
03:00
So case module there, and that takes care of three out of our four preload options right there. Now, we can add in one more case statement for our module courses.
03:09
Query dot preload, and we need to do module and then reach off of there with a module query builder.
03:15
So our module dot and then preload off of the module, the course to allow that nestedness, which is being specified within the name here via the period right there.
03:25
All right, let's add in our break and we need to apply these preloads into our query. So this apply preloads query and pass the filters in.
03:34
I'm going to put that before our publish at filter since we've put in the actual method before it. Give that a save. Let's jump back over into Hopscotch then.
03:43
Send this off with an empty body and nope, did I forget to make that optional? Let's jump back into our validator. I sure did.
03:50
Yeah, we definitely want this to be optional because we're just defaulting to none if they don't provide it in. So with that set, let's try this one more time. There we go.
03:58
OK, so we get back our data and it's still adding the access level ID in because we forgot to remove it from the base query.
04:05
So if we come back up to the base query, yeah, we also need to remove these preloads there. All right, let's try this one more time. Send it off. Scroll through our data. There we go.
04:14
Now it's just the base lesson without any additional preloaded information on top of it. So that's looking good.
04:20
If we come up to our body and add in relationships as an array and add status into it, send this off.
04:28
We should just get preloaded our status information, which we do indeed. Let's switch that for access level. Send that off. Now we should just get our access level, which we do.
04:38
Let's try module.course. Send this off. And there's our module and there's our course.
04:45
If we just try module, that should just do the module without the course. And if we do module and module.course, we should get back the module and the course information there too.
04:55
Now we're almost done here, but we do have one thing left to check. So right now we get back our courses because our module.course is last in our array.
05:04
For example, if we move this, so if I cut it out and move it to the front of our array and we send this off, you'll see that what we get back is just the module now without
05:14
the course information. The reason for this is because AdonisJS is adding our preload for our module twice. Once for where we have our module.course.
05:22
When it adds this, it would actually have our course in there, but then it's overwriting it whenever we actually apply the module in our second time.
05:30
Since within our loop, we are merely looping over it and applying it in the same order that's provided in that array. So in our first index, we're querying and preloading in the module and the course, and
05:40
then it's looping again and preloading just the module. So to fix this, what we can do is move our module into its own case statement at our
05:49
break there for our status and access level. And we can duplicate that preload, add it in here, and then just do a simple if check.
05:56
So if relationships includes module.course, then what we want to do is just go ahead and
06:03
continue on to the next loop, skipping the module as a whole, and then applying the module preload just once here with our module.course.
06:12
So if we give that a save, you'd also wrap the entire query preload there in an if as well, if that's your preferred structure.
06:19
But with that saved, if we jump back over to Hopscotch, send this off one more time, take a look at our results. Now we're getting our module along with its course, despite the module preload being added
06:29
here at the first index of our relationship array after our module.course.