00:00
(upbeat music)
00:02
So we've been taking a look so far
00:06
at the Model Query Builder.
00:07
The Model Query Builder runs directly off of our models.
00:10
We can use the actual column names
00:12
that we've defined on the models within our queries itself
00:14
to make things a little bit more developer friendly.
00:16
And you'll also notice that all of the responses
00:19
have been of type our model itself.
00:22
So whenever you're working with the Model Query Builder,
00:23
it's always going to return back a type
00:26
of your model itself.
00:26
So it's always going to be the same structure
00:29
as your models defined.
00:30
And that's regardless of whether or not
00:32
you're specifying any select statements
00:34
or anything of the sort.
00:35
So for example, if we specify that we just want back
00:38
the ID for our topics here and we give this a save,
00:41
we're still getting back a type of our topic array.
00:44
And whenever we actually send out a request
00:46
to this endpoint to get back our results,
00:48
you'll see that it's not just an array
00:49
of the underlying IDs.
00:50
Instead, it's an object with the ID inside of it,
00:53
still keeping true that it's returning back
00:55
some reference to our model itself.
00:58
Now, if we did truly just want this to be an array of IDs,
01:01
we could wrap our await statement here in parentheses,
01:04
map over the results, do topic, topic.id
01:08
to just return back the ID and send this out.
01:11
And now we just get back an array of the underlying IDs,
01:14
not wrapped in any sort of object.
01:17
Now, in addition to the Model Query Builder,
01:19
which is what we've been taking a look at here so far,
01:21
we also have access to the Database Query Builder.
01:24
And this is more of a raw ConnectJS Query Builder.
01:26
Let's take a look at const db equals await.
01:30
We can import the Database Query Builder
01:32
via IOC Adonis Lucid Database.
01:34
We can specify the table that we want to run this query
01:37
against by doing dot from.
01:38
This will be our topics table.
01:40
And you'll notice that we're using the underlying table name
01:43
from the database itself.
01:45
None of the names that we have defined inside our model
01:47
will work whenever we're working
01:48
with the Database Query Builder.
01:50
The key difference here between these two
01:52
is that the Model Query Builder runs through our models
01:54
and we can use our model names, relationships, et cetera,
01:57
with the Model Query Builder.
01:59
And it will always return back a reference to our model,
02:02
regardless of what we're selecting or joining in.
02:05
The Database Query Builder
02:06
runs directly through our database.
02:08
It doesn't touch any of our models.
02:10
It doesn't run any hooks.
02:11
It just goes directly to the database.
02:14
Because of that, we don't have any actual types
02:15
running through this database.
02:17
So if we take a look at the type of a response,
02:19
currently it's just an array of any.
02:21
So if we go ahead and add DB into our JSON response here,
02:25
I'm gonna get rid of the select
02:26
on our Model Query Builder here temporarily.
02:28
Now our two queries here match
02:30
in terms of what we would expect.
02:32
We're just getting back all of our topics
02:33
from our Topic Model Query Builder.
02:35
And we're just getting back all of our topics
02:36
from our Database Query Builder.
02:38
So let's give this a save, give this a run.
02:41
Here is our Topics Query Builder result.
02:43
You'll see that we get back exactly
02:44
what we've been working with here so far.
02:46
For our Database Query Builder,
02:47
we get back the exact same result.
02:49
And that's because we're looking
02:50
at the serialized response of our topics
02:53
and the serialized result of our database,
02:56
which at the end of the day will match.
02:58
Now let's take a second to take a look
03:00
at the code implementation here.
03:01
So it's console.log, our topics and our DB.
03:04
And then let's open up our terminal here.
03:07
I'm gonna open this up all the way
03:08
and let's send the request out.
03:10
Okay, same serialized results that we got before.
03:12
But if we jump back into our console,
03:15
our database results match that,
03:17
that we're getting with our serialized results
03:19
for the Database Query Builder.
03:21
But our Model Query Builder you'll see
03:22
is turning back actual instances of our model itself.
03:26
So through the Model Query Builder,
03:27
we can still access all the information
03:30
in terms of our model itself,
03:32
including the underlying data that we would then get back.
03:35
So the key difference between the Model Query Builder
03:37
that we're taking a look at the results here for
03:39
and the Database Query Builder
03:40
is what it's referencing and going through.
03:43
The Model Query Builder will always go through the model.
03:45
The Database Query Builder just goes directly
03:47
to the database and will return back
03:49
whatever the database returns.
03:51
Let's go ahead and collapse this down here for a second
03:53
and let's add these selects back.
03:55
For our Database Query Builder,
03:57
we can do the exact same thing, select.
03:59
You'll also notice that the where statements
04:01
and everything that we've worked with
04:02
in the prior two lessons is applicable here.
04:04
We'll take a look at the main difference here
04:05
between that next.
04:06
Okay, let's give this a save.
04:08
I'm gonna scroll all the way down on our terminal here,
04:10
jump back into Insomnia, send this off again.
04:12
Again, the underlying serialized results match here.
04:15
But if we take a look at the console,
04:17
we're still getting back a reference of our topic.
04:19
It looks the exact same as the results before.
04:22
The only difference is going to be the underlying attributes
04:24
that we have populated.
04:25
In terms of our database results, again,
04:28
it matches exactly what we have for the serialized results.
04:31
I'm gonna go ahead and collapse our terminal away,
04:33
get rid of our console log.
04:35
And now one last thing to take a look at
04:36
with the Database Query Builder
04:38
is the difference between our column references.
04:40
So previously, whenever we sorted our Model Query Builder,
04:42
we did an order by, and the only one that we have
04:45
that's camel-cased is our createdAt and updatedAt.
04:47
So we'll go ahead and do that here,
04:48
and we'll do it in descending order.
04:50
So for the order by here, we're using the column name
04:52
as it's defined in our model,
04:54
which is camel-cased createdAt.
04:56
For our Database Query Builder,
04:57
whenever we do an order by here,
04:59
we're going to want to reference the column name
05:01
as it's defined in our database,
05:03
which is created_at.
05:06
And again, we can do descending here as well.
05:08
Give this a save, jump back into Insomnia, send this off.
05:11
And again, you'll see the underlying
05:12
serialized results match.
05:14
The main difference again is the types of the response.
05:17
For the Model Query Builder,
05:18
it's going to be a type topic array.
05:19
And for the Database Query Builder,
05:20
it's going to be an any array.
05:22
How we reference the table name,
05:23
the Model Query Builder will use the table name
05:25
as it's defined in the model.
05:26
And the Database Query Builder,
05:27
we need to actually specify the table name
05:29
that we want to use as it's defined in the database.
05:31
And the same goes for the columns.
05:33
We need to use the column as it's defined in the model
05:35
for the Model Query Builder,
05:36
and the column as it's defined in the database
05:38
for the Database Query Builder.
05:40
One last notation is that the Model Query Builder
05:42
is forgiving, so you can use the column names
05:45
as they're defined in the database as well.
05:47
So let's switch that to created_at,
05:49
send this off, and we get back the exact same thing.
05:51
It will work just fine because at the end of the day,
05:53
it will double check against the database itself
05:55
if it cannot find a model reference.
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!