-- A Supabase app carrying one of each item `protobase import supabase` leaves in its report, made as its developer -- would in the SQL editor (role postgres): a pg_cron job calling pg_net with a Vault secret, a trigger calling an Edge -- Function over pg_net, an extension a plain Postgres does not have, and a foreign key no index serves. create extension if not exists pg_cron; create extension if not exists pg_net with schema extensions; create extension if not exists pg_hashids with schema extensions; create table public.projects (id bigint generated by default as identity primary key, name text not null); create table public.todos ( id bigint generated by default as identity primary key, project_id bigint not null references public.projects (id), title text not null, done boolean, inserted_at timestamptz not null default now() ); -- A short public id for a todo, from pg_hashids. create function public.todo_code(todo_id bigint) returns text language sql stable as $$ select extensions.id_encode(todo_id, 'todos', 6) $$; -- Tells the notify Edge Function when a todo is done. create function public.todo_done() returns trigger language plpgsql security definer set search_path = '' as $$ begin perform net.http_post( url := 'https://abcdefghijklmnopqrst.supabase.co/functions/v1/notify', body := jsonb_build_object('id', new.id, 'title', new.title) ); return new; end $$; create trigger on_todo_done after update of done on public.todos for each row when (new.done and not coalesce(old.done, false)) execute function public.todo_done(); select vault.create_secret('tok_live_123', 'digest_token', 'Token of the digest service'); -- Every morning, the open todos to the digest service, with the token from Vault. select cron.schedule('morning-digest', '0 6 * * *', $$ select net.http_post( url := 'https://digest.example.com/hooks/todos', headers := jsonb_build_object('Authorization', 'Bearer ' || (select decrypted_secret from vault.decrypted_secrets where name = 'digest_token')), body := jsonb_build_object('open', (select count(*) from public.todos where not coalesce(done, false))) ) $$); insert into public.projects (name) values ('Home'), ('Work'); insert into public.todos (project_id, title, done) values (1, 'Buy milk', false), (2, 'Ship the release', null), (2, 'Write the notes', true);