Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn how to leverage ChatGPT to effortlessly transform raw SQL queries into Sequelize queries.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hey everyone, Travis here!
0:09
I'd like to share another cool
feature you can explore with ChatGPT,
0:11
converting raw SQL queries
to Sequelize queries.
0:15
Now, I wanna clarify that this
is not meant to replace learning
0:18
Sequelize properly or
skipping its documentation.
0:21
However, it can be really helpful if you
already have a raw SQL query written or
0:24
if you're still getting
familiar with Sequelize.
0:29
When I was learning Sequelize,
0:32
I always wish there was a conversion
feature like this in their documentation.
0:34
Thanks to ChatGPT, we can now explore
both approaches side by side,
0:38
which has actually helped me understand
Sequelize model querying better.
0:42
Now let's dive into the setup.
0:46
I have a simple Express API up and
running,
0:48
I've set up a SQLite3 database
with two tables, Users, and BlogPosts.
0:51
There's a one-to-many
relationship between the tables.
0:56
Meaning a user can have
multiple blog posts while
0:59
a blog post can only have one user.
1:02
I'm using the Sequelize ORM to
communicate with the database,
1:05
although I'm not fully
leveraging its power yet.
1:08
I've written a few endpoints, but
1:11
currently I'm using raw SQL queries
to interact with the database.
1:13
Our goal is to see if ChatGPT can assist
us in quickly modifying our routes to
1:17
utilize Sequelize's
model querying instead.
1:22
This first route is simply retrieving
all the blog posts along with their
1:24
associated user or author's username.
1:29
I'm gonna copy this query and
head over to ChatGPT.
1:32
I'm using the free version, and
at the time of recording this,
1:37
it's using version 3.5.
1:41
I'm gonna specify my model import, so
that it doesn't continue giving us
1:43
the database table names, and ask it
to convert this into a Sequelize query.
1:47
I'll go down to a new line,
paste it in, and see what it gives us.
1:52
Here's our response.
1:55
It's given us a detailed breakdown
of all the code it's given us,
1:57
which is very helpful.
2:01
We can see it's grabbing
the specific attributes I was
2:02
getting in the old query and
even added in our user's alias.
2:06
Let's copy this and
bring it into our code.
2:09
I have a new route to use set up already,
so I'm going to comment out this original
2:21
one, uncomment the new one, and paste our
new query right after this await here.
2:26
I'll save and head over to Postman.
2:31
I've created a workspace in
Postman to test my routes and
2:38
here I have a GET request all set up for
this route.
2:40
Let's see if it works.
2:43
Awesome, it's working perfectly, and
2:45
it even added our author's
data into its own object.
2:48
All right,
let's move on to my second endpoint.
2:54
Here I'm wanting to create a new entry, or
row, into the blog post table.
2:57
Again, let's copy and
paste this SQL query over to ChatGPT.
3:01
I'll ask it to convert this
one just as it did before, but
3:06
I'm gonna ask it to simplify it so
3:09
that it will automatically add any
properties that sent in the request.
3:10
It looks good, but I feel that this can
be simplified further by not saving
3:32
the req.body into a variable.
3:37
I'm just gonna copy this last line and
bring it over to the code.
3:40
I'll paste it into my new
route just as I did before,
3:51
I'll also change this blog post
data to just use req.body.
3:54
All right, now let's try it in Postman.
4:02
I have a post request set up to
create the greatest blog of all time.
4:05
Let's run it.
4:11
We got a successful status code returned.
4:16
Let's go back and
run the Get All Blog Posts request again.
4:18
Now we can see the new entry
added down here at the bottom.
4:25
Nice work, Rohald.
4:31
I noticed that there was a typo in
the title of one of our blog posts.
4:39
Ironically, it's a typo
on the word mistakes.
4:43
Luckily, I've set up a route
to handle editing or updating,
4:48
a current blog in the database.
4:51
I'm first making sure that the blog post
we are attempting to edit exists with
4:53
this top query.
4:58
Since there are two queries in here,
I'm going to copy the entire route and
4:59
see how ChatGPT handles this one.
5:03
I'm going to reuse my previous
question and modify it.
5:22
Again, we received a very
detailed breakdown of the code,
5:49
it's also much simpler than it was before.
5:52
Let's copy this over.
5:55
I'll paste in the new route, remove
the import that came along with it, and
6:09
comment out the original one.
6:13
I'll save this and head back into Postman.
6:21
Here I have a request set up to update
the title of the blog with the ID of 8.
6:26
Let's give it a shot.
6:31
We received a successful status code so
6:36
let's try running the Get All Blog Post
request again to make sure it worked.
6:38
And there we go.
6:44
No more mistakes in mistakes, nice.
6:44
To finish off the CRUD operations,
6:49
I now have a route set up to handle
deleting a blog post from the database.
6:51
Because I have two queries
in this one as well,
6:55
let's take the whole route
over to ChatGPT again.
6:58
I'm going to prompt it just
as I did last time but
7:16
without worrying about anything
coming in with the request.
7:18
Wow, this looks fantastic.
7:40
Let's paste it into our code.
7:41
Just as before I'll paste it in,
7:48
remove this import comment
out the original route.
7:50
I personally have only
written one blog post, and
8:17
it's very obvious I didn't
put much time into this one.
8:19
I don't think many people are gonna
wanna read this thing, so
8:22
I set up a request here
in Postman to delete it.
8:26
Let's run it.
8:28
It looks like it may have worked.
8:33
Let's run our get All Blog Post
request again to make sure.
8:34
Number 12 is gone, it worked.
8:48
I hope this video helps you out and
8:52
shows you another way that we can
utilize AI in our learning journey.
8:53
As I said earlier though, don't forget
about the Sequelize documentation.
8:57
That should always be your first stop
when trying to figure out a new topic.
9:02
Thanks for watching,
9:06
and until next time, happy coding!
9:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up