Created
August 14, 2018 09:51
-
-
Save chrischmo/4a2c6bf33a80c14100c6e2bb0a4e429b to your computer and use it in GitHub Desktop.
postgrest DDL files
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 or replace function mutation_comments_trigger() returns trigger as $$ | |
declare | |
c record; | |
parent_type text; | |
begin | |
if (tg_op = 'DELETE') then | |
if old.parent_type = 'task' then | |
delete from data.task_comment where id = old.id; | |
if not found then return null; end if; | |
elsif old.parent_type = 'project' then | |
delete from data.project_comment where id = old.id; | |
if not found then return null; end if; | |
end if; | |
return old; | |
elsif (tg_op = 'UPDATE') then | |
if (new.parent_type = 'task' or old.parent_type = 'task') then | |
update data.task_comment | |
set | |
body = coalesce(new.body, old.body), | |
task_id = coalesce(new.task_id, old.task_id) | |
where id = old.id | |
returning * into c; | |
if not found then return null; end if; | |
return (c.id, c.body, 'task'::text, c.task_id, null::int, c.task_id, c.created_on, c.updated_on); | |
elsif (new.parent_type = 'project' or old.parent_type = 'project') then | |
update data.project_comment | |
set | |
body = coalesce(new.body, old.body), | |
project_id = coalesce(new.project_id, old.project_id) | |
where id = old.id | |
returning * into c; | |
if not found then return null; end if; | |
return (c.id, c.body, 'project'::text, c.project_id, c.project_id, null::int, c.created_on, c.updated_on); | |
end if; | |
elsif (tg_op = 'INSERT') then | |
if new.parent_type = 'task' then | |
insert into data.task_comment (body, task_id) | |
values(new.body, new.task_id) | |
returning * into c; | |
return (c.id, c.body, 'task'::text, c.task_id, null::int, c.task_id, c.created_on, c.updated_on); | |
elsif new.parent_type = 'project' then | |
insert into data.project_comment (body, project_id) | |
values(new.body, new.project_id) | |
returning * into c; | |
return (c.id, c.body, 'project'::text, c.project_id, c.project_id, null::int, c.created_on, c.updated_on); | |
end if; | |
end if; | |
return null; | |
end; | |
$$ security definer language plpgsql; |
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 table client ( | |
id serial primary key, | |
name text not null, | |
address text, | |
user_id int not null references "user"(id) default request.user_id(), | |
created_on timestamptz not null default now(), | |
updated_on timestamptz | |
check (length(name)>2 and length(name)<100), | |
check (updated_on is null or updated_on > created_on) | |
); | |
create index client_user_id_index on client(user_id); | |
create table project ( | |
id serial primary key, | |
name text not null, | |
client_id int not null references client(id), | |
user_id int not null references "user"(id) default request.user_id(), | |
created_on timestamptz not null default now(), | |
updated_on timestamptz | |
check (length(name)>2), | |
check (updated_on is null or updated_on > created_on) | |
); | |
create index project_user_id_index on project(user_id); | |
create index project_client_id_index on project(client_id); | |
create table task ( | |
id serial primary key, | |
name text not null, | |
completed bool not null default false, | |
project_id int not null references project(id), | |
user_id int not null references "user"(id) default request.user_id(), | |
created_on timestamptz not null default now(), | |
updated_on timestamptz | |
check (length(name)>2), | |
check (updated_on is null or updated_on > created_on) | |
); | |
create index task_user_id_index on task(user_id); | |
create index task_project_id_index on task(project_id); | |
create table project_comment ( | |
id serial primary key, | |
body text not null, | |
project_id int not null references project(id), | |
user_id int not null references "user"(id) default request.user_id(), | |
created_on timestamptz not null default now(), | |
updated_on timestamptz | |
check (length(body)>2), | |
check (updated_on is null or updated_on > created_on) | |
); | |
create index project_comment_user_id_index on project_comment(user_id); | |
create index project_comment_project_id_index on project_comment(project_id); | |
create table task_comment ( | |
id serial primary key, | |
body text not null, | |
task_id int not null references task(id), | |
user_id int not null references "user"(id) default request.user_id(), | |
created_on timestamptz not null default now(), | |
updated_on timestamptz | |
check (length(body)>2), | |
check (updated_on is null or updated_on > created_on) | |
); | |
create index task_comment_user_id_index on task_comment(user_id); | |
create index task_comment_task_id_index on task_comment(task_id); |
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 or replace view clients as | |
select id, name, address, created_on, updated_on from data.client; | |
create or replace view projects as | |
select id, name, client_id, created_on, updated_on from data.project; | |
create or replace view tasks as | |
select id, name, completed, project_id, created_on, updated_on from data.task; | |
create or replace view comments as | |
select | |
id, body, 'project'::text as parent_type, project_id as parent_id, | |
project_id, null as task_id, created_on, updated_on | |
from data.project_comment | |
union | |
select id, body, 'task'::text as parent_type, task_id as parent_id, | |
null as project_id, task_id, created_on, updated_on | |
from data.task_comment; | |
-- Specify owners of the views (needed for RLS) | |
alter view clients owner to api; | |
alter view projects owner to api; | |
alter view tasks owner to api; | |
alter view comments owner to api; | |
-- Trigger for comment mutation | |
create trigger comments_mutation | |
instead of insert or update or delete on comments | |
for each row execute procedure util.mutation_comments_trigger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment