[Supabase] Use Triggers to Automatically Update Your Supabase Tables

时间:2021-08-20
本文章向大家介绍[Supabase] Use Triggers to Automatically Update Your Supabase Tables,主要包括[Supabase] Use Triggers to Automatically Update Your Supabase Tables使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Documentation on creating public user tables has been moved here. Supabase has also removed triggers from this doc but you can still use them.

In this lesson, we'll use Supabase's triggers functionality to automatically update data in our application when a user creates an account. Triggers are a powerful functionality of relational databases that allow us to minimize the amount of manual development work we need to do to build applications.

Idea is when user signup, will save to default auth.users table, we want to automaticlly add to our own public.user table as well, with just id information

-- insert a row into public.user
create function public.handle_new_user()
returns trigger as $$
begin
  insert into public.user (id)
  values (new.id);
  return new;
end;
$$ language plpgsql security definer;

-- triger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

原文地址:https://www.cnblogs.com/Answer1215/p/15166459.html