Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
create function es.project(event returns void
language plpgsql as $$
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;
raise notice 'no case for event "%"', event.type;
end case;
create function es.trigger_project() returns trigger language plpgsql as $$
perform es.project(new);
return null;
create trigger on_event_insert after insert on
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.