From the course: Supabase Essential Training

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Refactoring

Refactoring

- [Speaker] Just like code in any other language. PL/pgSQL benefits from careful consideration of maintenance and reusability. Let's take a look at some ways to compose and reuse functions now. So let's say we want to have a function that looks up an item and then purchases it with a user's gift card. I'm going to paste this in and we're going to take a look at it. So this is a new function called purchase item. It's going to return JSON, it accepts the user ID and the item ID, and then it begins the process. First, it's going to pick the item name and the price in cents and store those as variables. And then it's going to update the gift card balance. And then finally, it's going to insert into the purchases table. And then after, it's done with all that, it will return the result as a JSON object. So let's create this function now and let's go ahead and run it. I'm going to say select star from purchase item, and…

Contents