1 00:00:00,019 --> 00:00:09,152 [MUSIC] 2 00:00:09,152 --> 00:00:11,459 Hey everyone, Travis here! 3 00:00:11,459 --> 00:00:15,679 I'd like to share another cool feature you can explore with ChatGPT, 4 00:00:15,679 --> 00:00:18,549 converting raw SQL queries to Sequelize queries. 5 00:00:18,549 --> 00:00:21,610 Now, I wanna clarify that this is not meant to replace learning 6 00:00:21,610 --> 00:00:24,226 Sequelize properly or skipping its documentation. 7 00:00:24,226 --> 00:00:29,291 However, it can be really helpful if you already have a raw SQL query written or 8 00:00:29,291 --> 00:00:32,692 if you're still getting familiar with Sequelize. 9 00:00:32,692 --> 00:00:34,217 When I was learning Sequelize, 10 00:00:34,217 --> 00:00:38,014 I always wish there was a conversion feature like this in their documentation. 11 00:00:38,014 --> 00:00:42,046 Thanks to ChatGPT, we can now explore both approaches side by side, 12 00:00:42,046 --> 00:00:46,508 which has actually helped me understand Sequelize model querying better. 13 00:00:46,508 --> 00:00:48,363 Now let's dive into the setup. 14 00:00:48,363 --> 00:00:51,135 I have a simple Express API up and running, 15 00:00:51,135 --> 00:00:56,456 I've set up a SQLite3 database with two tables, Users, and BlogPosts. 16 00:00:56,456 --> 00:00:59,296 There's a one-to-many relationship between the tables. 17 00:00:59,296 --> 00:01:02,932 Meaning a user can have multiple blog posts while 18 00:01:02,932 --> 00:01:05,510 a blog post can only have one user. 19 00:01:05,510 --> 00:01:08,966 I'm using the Sequelize ORM to communicate with the database, 20 00:01:08,966 --> 00:01:11,722 although I'm not fully leveraging its power yet. 21 00:01:11,722 --> 00:01:13,744 I've written a few endpoints, but 22 00:01:13,744 --> 00:01:17,664 currently I'm using raw SQL queries to interact with the database. 23 00:01:17,664 --> 00:01:22,011 Our goal is to see if ChatGPT can assist us in quickly modifying our routes to 24 00:01:22,011 --> 00:01:24,842 utilize Sequelize's model querying instead. 25 00:01:24,842 --> 00:01:29,396 This first route is simply retrieving all the blog posts along with their 26 00:01:29,396 --> 00:01:32,018 associated user or author's username. 27 00:01:32,018 --> 00:01:35,070 I'm gonna copy this query and head over to ChatGPT. 28 00:01:37,285 --> 00:01:41,389 I'm using the free version, and at the time of recording this, 29 00:01:41,389 --> 00:01:43,292 it's using version 3.5. 30 00:01:43,292 --> 00:01:47,828 I'm gonna specify my model import, so that it doesn't continue giving us 31 00:01:47,828 --> 00:01:52,514 the database table names, and ask it to convert this into a Sequelize query. 32 00:01:52,514 --> 00:01:55,977 I'll go down to a new line, paste it in, and see what it gives us. 33 00:01:55,977 --> 00:01:57,729 Here's our response. 34 00:01:57,729 --> 00:02:01,577 It's given us a detailed breakdown of all the code it's given us, 35 00:02:01,577 --> 00:02:02,962 which is very helpful. 36 00:02:02,962 --> 00:02:06,277 We can see it's grabbing the specific attributes I was 37 00:02:06,277 --> 00:02:09,963 getting in the old query and even added in our user's alias. 38 00:02:09,963 --> 00:02:11,849 Let's copy this and bring it into our code. 39 00:02:21,499 --> 00:02:26,209 I have a new route to use set up already, so I'm going to comment out this original 40 00:02:26,209 --> 00:02:31,008 one, uncomment the new one, and paste our new query right after this await here. 41 00:02:31,008 --> 00:02:38,150 I'll save and head over to Postman. 42 00:02:38,150 --> 00:02:40,824 I've created a workspace in Postman to test my routes and 43 00:02:40,824 --> 00:02:43,447 here I have a GET request all set up for this route. 44 00:02:43,447 --> 00:02:45,990 Let's see if it works. 45 00:02:45,990 --> 00:02:48,363 Awesome, it's working perfectly, and 46 00:02:48,363 --> 00:02:51,424 it even added our author's data into its own object. 47 00:02:54,343 --> 00:02:57,085 All right, let's move on to my second endpoint. 48 00:02:57,085 --> 00:03:01,460 Here I'm wanting to create a new entry, or row, into the blog post table. 49 00:03:01,460 --> 00:03:06,318 Again, let's copy and paste this SQL query over to ChatGPT. 50 00:03:06,318 --> 00:03:09,251 I'll ask it to convert this one just as it did before, but 51 00:03:09,251 --> 00:03:10,963 I'm gonna ask it to simplify it so 52 00:03:10,963 --> 00:03:14,713 that it will automatically add any properties that sent in the request. 53 00:03:32,941 --> 00:03:37,688 It looks good, but I feel that this can be simplified further by not saving 54 00:03:37,688 --> 00:03:40,071 the req.body into a variable. 55 00:03:40,071 --> 00:03:43,202 I'm just gonna copy this last line and bring it over to the code. 56 00:03:51,516 --> 00:03:54,728 I'll paste it into my new route just as I did before, 57 00:03:54,728 --> 00:03:58,748 I'll also change this blog post data to just use req.body. 58 00:04:02,259 --> 00:04:05,740 All right, now let's try it in Postman. 59 00:04:05,740 --> 00:04:08,911 I have a post request set up to create the greatest blog of all time. 60 00:04:11,145 --> 00:04:11,801 Let's run it. 61 00:04:16,345 --> 00:04:18,186 We got a successful status code returned. 62 00:04:18,186 --> 00:04:21,551 Let's go back and run the Get All Blog Posts request again. 63 00:04:25,141 --> 00:04:27,551 Now we can see the new entry added down here at the bottom. 64 00:04:31,329 --> 00:04:32,443 Nice work, Rohald. 65 00:04:39,256 --> 00:04:43,927 I noticed that there was a typo in the title of one of our blog posts. 66 00:04:43,927 --> 00:04:48,020 Ironically, it's a typo on the word mistakes. 67 00:04:48,020 --> 00:04:51,787 Luckily, I've set up a route to handle editing or updating, 68 00:04:51,787 --> 00:04:53,752 a current blog in the database. 69 00:04:53,752 --> 00:04:58,672 I'm first making sure that the blog post we are attempting to edit exists with 70 00:04:58,672 --> 00:04:59,739 this top query. 71 00:04:59,739 --> 00:05:03,053 Since there are two queries in here, I'm going to copy the entire route and 72 00:05:03,053 --> 00:05:04,590 see how ChatGPT handles this one. 73 00:05:22,178 --> 00:05:25,360 I'm going to reuse my previous question and modify it. 74 00:05:49,034 --> 00:05:52,638 Again, we received a very detailed breakdown of the code, 75 00:05:52,638 --> 00:05:55,317 it's also much simpler than it was before. 76 00:05:55,317 --> 00:05:56,339 Let's copy this over. 77 00:06:09,188 --> 00:06:13,037 I'll paste in the new route, remove the import that came along with it, and 78 00:06:13,037 --> 00:06:14,612 comment out the original one. 79 00:06:21,362 --> 00:06:23,727 I'll save this and head back into Postman. 80 00:06:26,607 --> 00:06:31,550 Here I have a request set up to update the title of the blog with the ID of 8. 81 00:06:31,550 --> 00:06:32,611 Let's give it a shot. 82 00:06:36,289 --> 00:06:38,887 We received a successful status code so 83 00:06:38,887 --> 00:06:44,016 let's try running the Get All Blog Post request again to make sure it worked. 84 00:06:44,016 --> 00:06:44,802 And there we go. 85 00:06:44,802 --> 00:06:47,450 No more mistakes in mistakes, nice. 86 00:06:49,462 --> 00:06:51,601 To finish off the CRUD operations, 87 00:06:51,601 --> 00:06:55,968 I now have a route set up to handle deleting a blog post from the database. 88 00:06:55,968 --> 00:06:58,349 Because I have two queries in this one as well, 89 00:06:58,349 --> 00:07:00,984 let's take the whole route over to ChatGPT again. 90 00:07:16,682 --> 00:07:18,990 I'm going to prompt it just as I did last time but 91 00:07:18,990 --> 00:07:22,014 without worrying about anything coming in with the request. 92 00:07:40,173 --> 00:07:41,897 Wow, this looks fantastic. 93 00:07:41,897 --> 00:07:43,302 Let's paste it into our code. 94 00:07:48,986 --> 00:07:50,807 Just as before I'll paste it in, 95 00:07:50,807 --> 00:07:53,678 remove this import comment out the original route. 96 00:08:17,250 --> 00:08:19,676 I personally have only written one blog post, and 97 00:08:19,676 --> 00:08:22,464 it's very obvious I didn't put much time into this one. 98 00:08:22,464 --> 00:08:26,068 I don't think many people are gonna wanna read this thing, so 99 00:08:26,068 --> 00:08:28,781 I set up a request here in Postman to delete it. 100 00:08:28,781 --> 00:08:29,400 Let's run it. 101 00:08:33,090 --> 00:08:34,903 It looks like it may have worked. 102 00:08:34,903 --> 00:08:38,139 Let's run our get All Blog Post request again to make sure. 103 00:08:48,282 --> 00:08:52,004 Number 12 is gone, it worked. 104 00:08:52,004 --> 00:08:53,803 I hope this video helps you out and 105 00:08:53,803 --> 00:08:57,539 shows you another way that we can utilize AI in our learning journey. 106 00:08:57,539 --> 00:09:02,136 As I said earlier though, don't forget about the Sequelize documentation. 107 00:09:02,136 --> 00:09:06,384 That should always be your first stop when trying to figure out a new topic. 108 00:09:06,384 --> 00:09:07,308 Thanks for watching, 109 00:09:07,308 --> 00:09:09,220 and until next time, happy coding!