API Design in Node.js, v4

Create & Delete Handlers Solution

Scott Moss

Scott Moss

Superfilter AI
API Design in Node.js, v4

Check out a free preview of the full API Design in Node.js, v4 course

The "Create & Delete Handlers Solution" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott continues the solution for the Update Handlers exercise. This lesson includes the solution to the createUpdate, updateUpdate, and DeleteUpdate route handlers.


Transcript from the "Create & Delete Handlers Solution" Lesson

>> createUpdate, let's do that one, so to create an update, what did we say we needed? We're gonna need a title and a body, I think we got to have one more, you're also gonna need, The product ID. IsSemVer, they've got SemVer in there, that's cool. So yeah, we need a productId, otherwise how do we know where to add this?

So we gotta have that. Okay, so first thing first, we need to check to make sure that the product ID that you're sending up actually belongs to you, before we associate an update to our product ID that you gave us. Otherwise, yeah, it's gonna be the wild, wild west, so we'll just say product.

And in fact, we can do all of that, I believe we can do all of that in one query with a nested write, maybe. Maybe not, let's find out, so we'll say await prisma.product.findUnique. id: of req.body.productId.findUnique. Why are you tripping out? Let's see, .findUnique where, or the output where, id, there we go, okay, req.body.id.

Okay, so we got the product, if this doesn't come back, so we can say, if there is no product, right? That means this product does not belong to you. And we're gonna handle errors later. Does not belong to user. So right now, we'll just send something back right now, it says like, no, you're not getting nothing back.

But if you do have a product, cool, you just send the right product ID. Okay, let's go ahead and create an update now. So now we can say, prisma.update.create, data, and we know that the productId is the one you sent us, or product.id, they're both the same. And the other stuff is going to be all in req.body.

Honestly, we can just probably just spread over req.body at this point. Because we trust it, so we could just say, Actually we need it, yeah, we can just spread over req.body, I believe, so we could just req.body. Because req.body will just have title, a body, and a productId, which are all the things that we need for an update.

A title, a body, and a productId. Everything else is optional or defaulted, so we don't have to put anything else there. So we're actually good, so we can just do req.body. And we can trust it because the validators already looked at it. If we didn't have validators, we probably would not wanna do that, just throw the body on there blindly, cuz who knows what's on that body.

But because it already got validated at this point we should be able to trust it, and it should just work. So we got our update, now we can say res.json{data:update}. As you can see, these start to get more complex when you throw in a relation. Relations are what makes database queries complex.

Without relations, everything's just like one query. As soon as you add relations, it's like, well, hold on, you have to do what's called a query plan. You literally have to do a query plan of planning on how you're gonna navigate to this data. And what's the optimal query, which usually leads to rewriting schemas and indexes.

But yeah, it gets complicated when you have tons of relations. That's why there's different types of databases, like graph databases and things like that, to help with problems like that. Do you have a social network that have everyone has a relationship with everyone? Okay, maybe a relational database is probably not the best, because imagine the query for that.

Trying to find a friend of a friend of a friend of a friend, that would be crazy, right? So something like a graph database will probably be better, so there's pros and cons. Okay, let's update an update. So in order to do that, we need, These are optional, pretty much, I guess this one's optional, too, I guess we could say that.

Optional as well. Pretty much all of these are optional, and we don't really need any of it, but we do have this ID here, so let's do that. But we need to make sure that you as a user own that ID. You as a user has a product that you own that has an update with that ID.

So we have to find a product, Who has that updated. And there's a lot of ways we can do that, we can go from the product side, so let's try that. So we can say product = await prisma.products.findUnique. Or actually we wanna do findFirst, I guess, find the first product where it belongsToId = req.user.id.

But then we also wanna look inside of its updates, To see, What's the word? There's a word to look for the array here. We could say updates. And then in, like that, then you can put an array here, I believe. But yeah, I don't think this is gonna work, because it's not a scalar, it wants the strings.

Okay, we'll just do this in JavaScript then, for now. So we'll just get the products where you have this. So these will be, Let's just say findMany now, and these will be products. And then we'll just say match. First let's get all the updates. And we can pretty much just copy what we had up here to get all the updates, which is basically like this.

I guess we gotta include updates. So we got the updates, and then we can say, match would be updates.find an update, where update.id === req.params.id. So then you can say, if there's a match in res.json. Or no, if there's a match, then we can actually do the update that we were trying to do in the first place.

So I was gonna say if not a match, we'll handle this. And you just do something like res.json, message, nope, and just return here. But if there is a match, now we can actually do the update, which will be updated [LAUGH] Update, which would be await prisma.update.update. Where id is req.params.id, and data is req.body.

Yeah, that was a lot. Because, Yeah, you definitely want the database doing this, and not having to do this in JavaScript. I don't want you guys sit here watching me trying to figure out the optimal way to do it in Prisma. But I can promise you there's a way to do it here in Prisma, the exact syntax is escaping me.

Then we could just say res.json ({data: updatedUpdate}). And a delete and update.
>> Question?
>> Yes.
>> Here, we will end up fetching a lot of updates, right?
>> Will we end up fetching a lot of updates? Yeah, you'll end up fetching every single, well, so you'll get the updates here.

You'll get every single, I'm sorry, where am I at? Yeah, you'll end up getting every single update for every single product that belongs to this user, yeah. Right, yeah, it's not optimal, you definitely want to filter, you wanna do that here. You wanna say only find the products who has an update that is part of req.params.id right here.

That's what we wanna do, that's what I was looking for in Prisma. But the syntax on exactly how to do it is us doing it in memory, which is usually never good, but we'll keep it moving. Okay. DeleteUpdate, I mean, it's very much the same, it's kinda like an update, you just need to make sure that they own that update before you delete it, otherwise they're deleting something else.

So the same problem we had here we would need there. So I'm actually just gonna copy this. Pretty much the same thing. And just put it here. So we gotta find the products, find the updates, if there's a match, cool, if there's not a match, great. And only then can we say deleted is await prisma.update.delete where id is req.params.id.

And then you can res.json ({data, deleted}). Okay, so a lot of stuff there. All right, let's just add them to the routes. So we're gonna go to our router, and we'll just add them. And we'll try to use them and see what happens. So for update, this is getUpdates, for this one, this is getOneUpdate.

This next one is updateUpdate, that one's still so funny. This next one is createUpdate. And this last one is deleteUpdate. There we go.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now