Created
December 23, 2019 16:33
-
-
Save docteurklein/868dcd62c86d95e688130fea09f0e45a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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