Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
create function es.project(event es.events) returns void
language plpgsql as $$
begin
case event.type
when 'user_registered' then
insert into es.active_users
(user_id , name , sha256 , updated_at) values
(event.aggregate_id , event.payload->>'name' , event.payload->>'sha256' , event.added_at);
when 'user_changed_password' then
update es.active_users set
sha256 = event.payload->>'sha256',
updated_at = event.added_at
where user_id = event.aggregate_id;
when 'user_banned' then
delete from es.active_users
where user_id = event.aggregate_id;
else
raise notice 'no case for event "%"', event.type;
end case;
end;
$$;
create function es.trigger_project() returns trigger language plpgsql as $$
begin
perform es.project(new);
return null;
end;
$$;
create trigger on_event_insert after insert on es.events
for each row execute function es.trigger_project();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.