Hey there, everyone.
They share.
And in this video we are going to work through with the really, really long one.
In case you remember, we worked and we actually talked a lot.
Almost half an hour long video about how to deal with the queries which comes into the you are a long
query parameter list in this video.
We're going to go ahead and simplify this because this is a thing you are going to do with a lot of
products.
So it makes sense to actually extract the functionality, put that into utils, do it.
Do you really need to do that?
Probably no, because in a lot of cases, especially when I also do that in the teaching part, I've
seen that in the demo test.
We receive this search.
So anybody who is searching that, we actually handle that into a separate function.
When somebody actually sends out these pagination, we handle that into a separate function.
And when somebody sees us this ratings and all of that greater than equal to, we handle that into a
separate function.
But at the end of the day, the goal is whatever this user dot find is or what are the models to find
is we simply want to go ahead and inject these things inside this.
So inside these parenthesis, a curly brace is going to come in and inside that this is all what we
care.
We have to inject this one.
So this is the whole idea about doing anything.
This is a really powerful method and all the things which you inject inside is kind of a rare clause.
So we are going to define a kind of a var clause into this one.
So let's go ahead and name this one as simply a word clause and let's add a simple utility up here.
So let's go ahead and we're going to call this one as simply let's call this one as we're close, because
that's what precisely it is where clause that J is.
Now what this where clause is going to be, it's going to be a simple file or basically rather a class
which accepts that on which model you want to run this because it's going to be a user defined product,
fine category, fine or something else like that.
So first thing is that going to be parameter, and the second parameter is going to be this one.
The long string that we had is so much of the discussion.
So everything after the question mark is going to come to us and that is pretty easy request query.
So from search equals to all of that, this is going to what is going to come to us.
So let me copy this and first paste this one.
So this is the entirety of the big thing that is going to come in, and the first one is going to be
simple base itself.
So let's go ahead and call this one as base.
So whatever the base is, this is going to be simple product, dot find product.
Fine.
So this is what you are passing me up here.
I'm going to comment this one out and I'm going to call this one as simply the big query.
So let's go ahead and say a big queue.
Yeah, that's kind of a sound name.
Okay, so this is what we are having.
So these are two parameters that I'll be receiving.
Okay, now let's go ahead and create this class based on this one.
So let's go ahead and call this one class.
And this one is going to be simple.
Let's capitalize it because it's a class obviously where clause.
There we go.
So in this very close, we need to create a constructor because we are accepting two parameters.
So let's go ahead and call this as a little bit of a short because this is too big to call this one.
So this is going to be constructor.
Toure.
There we go.
And inside the constructor, we expect that whoever uses this class actually gives me two parameter.
He gives me base and he gives me the big Q.
So we're going to go ahead and say, hey, give me a big cube.
Ah, the big query this big Q again is just a request or query dot request dot query that you are sending
me up here.
Let's go ahead and create available so that we can reference them again.
So we are going to go ahead and call this one as this dot base is going to be base itself and further
down the road.
And again, this shouldn't be there.
There we go.
And further down the road, we're going to call this one as this big cue is also going to be big queue.
And again, this should be a smaller one, this big queue.
There we go.
Okay.
Now this is all done.
Now what we want to do further down the road is this class is going to have a few method which can actually
handle all of our situation.
The three precisely one are the search here first.
The second one is going to be the pagination that is handling for us.
And the third one, which is having this ratings and all of that.
So this category equals whatever that is or if there is something that comes up as ratings equals to
dollars.
So that kind of a situation that we can handle shortly, we can inject more methods as we like.
Now the first one and the easiest one that we are going to handle is going to be the search functionality.
So let's go ahead and say that is going to be a simple search method inside this one.
This goes like this and the search method, all I have to do is inside this big query.
I have to find this keyword search and I have to find whatever the value of this search keyword is.
And that is pretty easy because we have seen that earlier, that whenever you send a request or query,
it comes up as an object.
An object has a lot of key value properties there.
So let's go ahead and see that that I want to store this one as a search word.
So that is going to be the exact word that you are giving it to me.
So this is going to be like this.
So first I will select, I will search that whether you are having any search keyword or not.
So if we are going to say that this dot big query, so inside this big query, whatever the big string
that we have got, does this have a keyword search?
If this is having search keyword, then we are going to do something.
Otherwise we are going to go ahead and do something or nothing basically.
So if this dot search keyword exist, then we want to extract this value.
So where do we want to search here?
Because end of the day you have to write something like this.
So again, remember inside this, if I just go ahead and copy this one here, all you got to do here
is say something like this.
Hey, email is going to be something like this at the rate ae def.
So this is all what we do in the search category.
So let me just go ahead and add this one.
So we are searching for this a string, so let's go ahead and search for this.
So we're going to say that if this search would exist, I would look for in the name, you can look
for it in the name, in the description, whatever suits best to you.
So in the name further down the road, remember, this is exactly what we do, email and then an object
and just like this.
So I would like to inject this search keyword.
Now I can go ahead and simply say that, hey, just add this search keyword and that's it.
That will be absolutely fine to handle this case, but I don't want to do that.
I want to search not for precise word, but actually for a regex based search.
So I'm going to go ahead and remove this one and expand this object.
And I will say, hey, just craft this one as simple regex.
Yes, this is given to us by Mongoose.
In fact, MongoDB also gives that.
So this dot inside this big query, we will be filling it up with the.
Search keyword.
So this will be holding up as a regex.
So instead of passing it as a literal string, we are now passing it as a regex match.
So anything that closely related to that actually comes in.
And we are also going to go ahead and pass on an option in case you want to wish to, you can go ahead
and pass on.
It's not really compulsory.
I'm going to go ahead and pass on an option.
IE there is also an option of g for global search.
The IE is for the case in sensitivity.
So this is all what we got up here.
Okay.
Once this is all done, this entirety of the thing is being done so that I can inject something inside
in here.
This is product fine.
This product dot find is actually the base.
So we need to inject that into a base.
So let's go ahead and work on with that.
So we will say that this dot base.
So inside this base, let's go ahead and modify this one.
So this one is going to be this dot base and a dot find.
Let's add this add a find on to this query itself.
And then further down the road, I will just expand this object and I will say, hey, just add this
search word here.
So whatever existing values were there, we were not changing that, we're just adding it.
Now surely you might say, Hey, this was already product find and you are adding a dot find onto this.
Yes, we are actually doing this a little bit.
This can be actually modified a little bit more onto that.
But this is all what we got.
Now, once this is done, let's go ahead and simply go ahead and say, hey, I want to return this.
Why you are returning the entire this because I also want to return the big query itself.
Maybe somebody needs it.
That's why.
Why to bother it.
Why to return just this dot base.
Let's return then to this.
So it returns the entire context of it.
Okay, so I hope you got that.
How this where clause is working onto the search.
I guess this is all for this video.
Let's go ahead and move on to the next video and talk about how pagination is going to work.
Let's go ahead and catch up with the next one.