Loading
Current section: Updating Data 6 exercises
lesson

Intro to Updating Data

Transcript

00:00 we're gonna make it so that you can delete records and you can update records and do all sorts of things to all these records when it's working with Prisma. And that is definitely something that we want to do. We don't want to continue working with our in-memory database thing.

00:16 So in databases with SQL, you have a couple mechanisms for making mutations. So you have insert into table name and then the columns you want to update and here are the values. There's a little level of indirection here that I never really loved. It's like, you know, the column one and here's our value for that column.

00:35 And I don't like that, but it is what it is. And SQL rocks, it's pretty powerful. But yeah, that level of indirection never sat well with me. But of course you never actually, most people don't actually write SQL. They'll use a query builder and ORM. So that's fine. Okay, then we have update. So we're gonna update in that table name.

00:54 We're gonna set this column to this value. Hey, there's no indirection right there, that's nice. And then we have our where condition here. And then we have an insert into, this would be like what we call an upsert. So it looks like an insert where we've got the insert into and here are our values, but if there's a conflict,

01:13 so if something already exists, this would be sort of like a little bit like a where clause. So like if a record already exists with these conditions, then let's update instead. And then that looks like the update side. So an upsert basically has both the update

01:31 and the create versions. And then just depending on this on conflict, it will determine whether to do an insert or an update. So that's an upsert. Pretty common that we're actually not gonna get to an upsert today, but later on, we'll have an upsert in this app

01:50 when you are able to create new notes, then we'll do something a little funky with that. So it'll be cool. And then we've got our delete, delete from table, table name, and where condition. So you can say, delete all of this user's notes because they're ready to be done

02:07 or delete just this one note by its ID and stuff like that. So with Prisma, of course, all of these things are supported. You've got updates and here's your where clause. This update is going to be like the update with the set and the where condition. And then of course we've already done the create,

02:25 that's the insert that we've done before. For an upsert, you're gonna have your where clause, so that's your condition. And then you have the update and then the create. And yeah, so that's your upsert. So you're gonna have both sides of that, update or create. And then for delete, you just have your where clause. That's that.

02:44 So I think you're ready to get going. We're actually gonna start with the delete and then we'll get into the upsert. We're also gonna be talking about transactions and things like that. So it should be pretty fun. Have a good time and yeah, enjoy this exercise. It's a good one.