课程: Supabase Essential Training
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
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 that's going to need a user ID and an item ID. So let's get both of…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。