import { PrismaClient } from "@prisma/client" ; const prisma = new PrismaClient ( ) ; /**
 * Creates a trigger on auth.users that executes a function
 * that in turn creates a user in public.users and cascades deletes
 * from auth.users to public.user
 *
 * Supabase uses an internal `auth.users` table for all `supabase.auth`
 * related queries or mutations. This trigger and corresponding function
 * ensures that whenever a new user is created with supabase.auth, that user
 * is also created in public.users.
 *
 */ export async function handle_insert_user ( ) { await prisma . $queryRawUnsafe ( `
  create or replace function public.handle_insert_user() 
    returns trigger as $$
    begin
        insert into public.user (id, email)
        values (new.id, new.email);
        return new;
    end;
    $$ language plpgsql security definer;
  ` ) ; try { await prisma . $queryRawUnsafe ( `
      create trigger on_auth_user_created
      after insert on auth.users
      for each row execute procedure public.handle_insert_user();
    ` ) ; } catch ( e ) { if ( e . code !== "P2010" ) { throw e ; } } console . log ( "✅ applied trigger: handle_insert_user" ) ; }