From the course: Supabase Essential Training
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Writing trigger functions
From the course: Supabase Essential Training
Writing trigger functions
- [Instructor] Triggers are useful for reacting to changes in a table. This way you can add functionality that does work for you automatically. For instance, this table is keeping track of gift card balances. We can write a trigger that will automatically create a log of what is being added to or deducted from the balance. We're going to do that in this gift_card_transactions table. It has an id, a timestamp, the amount in cents, and a status. Let's go to the SQL console now and add a trigger. We're creating a function called generate_gift_card_transaction, and it returns a trigger as its return type. And then all this function is doing is making an entry into that gift_card_transactions table. Once it's done with that, it is returning the new record. Notice we have both the old record and the new record here being referenced. We're taking the old records balance and subtracting the new records balance and then setting…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.