00:00
(dramatic music)
00:00
Now, most times you're not gonna want your query
00:04
to actually return back every single record
00:06
that you have for a model.
00:07
So today we're gonna take a look
00:08
at how we can limit those records.
00:09
So let's take this back a little bit
00:11
to where we would get back to results.
00:13
So let's give what we have here a save
00:15
and jump back into Insomnia, send this off.
00:17
And you can see our fine minis returning back
00:18
both JavaScript and TypeScript at this point.
00:20
If we wanted to limit this to just the first matching result,
00:23
what we can do is call just first here.
00:25
So let's give that a save.
00:27
And now JavaScript comes first in our array.
00:29
So we're going to get that back as our result.
00:32
An additional thing to note here
00:33
is that we're no longer getting back an array
00:35
as our response.
00:36
Instead, we're just getting back the model itself
00:38
because we're expecting just that single result.
00:41
Now, if we switch this to, let's add an additional where,
00:44
ID, and let's use an ID that doesn't exist.
00:46
So let's do three.
00:47
If we run this again, since we're just using first,
00:50
if it doesn't match a result, it's going to return back null.
00:52
This null response matches within our type as well.
00:55
So if we take a look at the type of our fine mini,
00:56
it's going to be topic or null.
00:58
If we need this query to return back a response,
01:01
otherwise fail out, we can add or fail to our first method.
01:05
And now if we send this off,
01:07
if we don't get back a response,
01:08
we're going to get it back a row not found,
01:10
four or four error.
01:11
So in addition to just using first, first, or fail,
01:14
if you need to limit your results,
01:15
but not necessarily to just the first record,
01:17
there's also a limit method.
01:19
This limit method allows you to specify
01:21
any number that you need,
01:22
and then it will cut it off at that response number.
01:24
So for example, we have two results here.
01:26
If we cut it off at one and we get rid of our ID there,
01:29
and we run this again,
01:31
we're going to get back just that one result.
01:33
If we switch this to two,
01:34
let's get rid of our where statement there.
01:36
Now we're just limiting it to two as a whole, send this off.
01:39
Now we're getting back just two,
01:40
but instead of limiting it to just those with script,
01:43
we're now also getting back AdonisJS.
01:45
So TypeScript is getting cut off from our response,
01:47
which then allows us to set this to three,
01:49
which would then return back all three.
01:51
Now, in most cases, if you're going to limit your results,
01:53
you probably also want to sort your results as well.
01:56
We can do that easily by chaining off
01:57
of our query builder order by.
01:59
First argument is going to be the column,
02:00
so let's order by the name.
02:01
And then the second argument is optional,
02:03
but it's going to be the direction.
02:04
So it can either be ascending or descending.
02:07
Let's leave it off for now,
02:08
and we'll see that it will default to doing ascending.
02:10
So we get back our results in alphabetical order,
02:12
which just happens to match the default sort.
02:14
But if we switch this to descending,
02:17
you'll see that the order is then flipped.
02:19
So we get TypeScript, JavaScript, and then Adonis.
02:21
And now if we limit it to two,
02:23
instead of TypeScript being cut off,
02:25
AdonisJS will now be cut off.
02:26
You can also order by something like a date/time.
02:29
So let's do createdAt here, descending.
02:32
And now the results here will just happen
02:33
to match what we had previously
02:34
because that's the order that we inserted them in.
02:36
Let's go ahead and get rid of our limit
02:38
so that we can take a look at all three.
02:39
And you'll see that the createdAt is indeed,
02:41
we created these all on the 28th,
02:43
but the time here is 11/28 for that one,
02:46
11/23 for that one, and 11/21 for this one.
02:49
So it's ordering them from newest to oldest
02:51
whenever we do descending there.
02:53
If we switch this to ascending, send this off,
02:56
you'll see that it's now doing oldest to newest.
02:58
Now, in addition to first and limiting,
03:00
you can also limit your orders via pagination.
03:03
And to help you out with that,
03:04
there is a pagination helper.
03:05
For that, we can do .paginate,
03:08
specify the current page number that we're on.
03:10
So this would be one
03:11
since we don't have any pages currently.
03:13
Specify the number that we want per page.
03:15
Let's say two so that we actually have two pages
03:17
to paginate here.
03:19
And let's send that out.
03:20
So it's going to order them by the createdAt date
03:22
in ascending order,
03:23
and then it's going to paginate them.
03:25
It will get back the first page of results,
03:26
and it will limit the results to two.
03:29
In addition to that, whenever we send this off,
03:30
you'll see that it also comes
03:31
with a number of different properties
03:33
that help us with the actual pagination rendering
03:35
whenever it comes time to do that.
03:37
So we get back meta information
03:38
containing the total per page, current page,
03:41
last page, first page, as well as the URLs for this.
03:44
Then we have our data array,
03:46
which contains the actual response data for the page.
03:49
If we come back into our paginate method,
03:51
switch this to page two,
03:52
give this a save.
03:53
Where we have AdonisJS and JavaScript,
03:55
we should now expect to just see TypeScript.
03:57
And you'll see our meta information updates
03:59
to reflect that we're now on page two,
04:01
and our data is now reflected
04:02
to just the second page of data.
04:05
Now, if we were actually going to render out
04:06
the meta information here,
04:07
this first page URL and last page URL,
04:10
you can see is going slash with a query string of page,
04:13
and then the number for the actual page action.
04:16
We can alter what this page is by doing findMany.
04:21
And then we can set the base URL for the paginated records
04:24
by specifying that URL in.
04:25
So for example, we can do slash topics,
04:28
give this a save, run this again.
04:30
And now you can see our first page URL is slash topics
04:33
with the query string of the applicable page.
04:35
So that's updated accordingly.
04:37
If you happen to have a route defined,
04:39
in our case, we have topics.index here that we can use.
04:42
We can go ahead and specify that route
04:44
using the route module.
04:45
So we can do route.makeURL,
04:49
and then specify topics.index there.
04:52
And then we'll use that accordingly.
04:54
Give this a send, and we get back the exact same result.
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!