Current section: Updating Data 6 exercises

Implementing Transactions for Reliable Data Operations


00:00 Let's say you're building a banking app and so you have this transaction thing, right? You've got one user over here and one user over here and they want to transfer money So user one transfers money out of their account and then you're gonna transfer money into user twos account But then there's a failure and so user one already lost the money and now you're stuck here holding money

00:19 Maybe that's actually fine. Just kidding. Unless you're a thief. Don't do that. That's bad So the problem is we need to have something some sort of transaction that like once it's finished then we're good But if any step of it doesn't work properly We just reset everything back to where it was and that's what these are called

00:38 They're called transactions and we have a similar situation here Where if we had some sort of error that happened in the process of updating this note Personally and and the product manager says that we want to roll back everything We don't want to have the user halfway update their note

00:54 So let's take a look at an example of what that might look like if we move this To right after we delete the images. We haven't yet updated any images or anything else like that So I come over here and I say yay, we're gonna edit this and we're gonna do whatever In here. Yeah, whatever and we're gonna update this image

01:14 We're gonna change it to a different one. And actually, you know what? I changed my mind We're gonna delete this and then we're gonna submit and whoa, we have an error not great But if I refresh then we actually we we did successfully delete an image and we did add or update our title

01:31 But we didn't update this image and so it just like is a little confusing to have only half of the things work When there's an error, I would rather just have it all fail if any part of it fails And so we're going to transactional eyes this exercise So that is your job is to start a transaction using Prisma's transaction utility

01:50 There are actually a couple ways to do this, but I personally prefer the callback method So you go ahead and work on that and we'll see you when you're all done