โ ๊ฐ์
๊ฒ์๊ธ ์ญ์ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๊ณผ์ ์์, ๊ฒ์๊ธ์ ์ญ์ ํ ๋ ํด๋น ๊ฒ์๊ธ์ ์ฐ๊ด๋ ์ด๋ฏธ์ง๋ ํจ๊ป ์ญ์ ๋์ด์ผ ํ๋ค. ์ด ์์ ์ ๊ฒ์๊ธ๊ณผ ์ด๋ฏธ์ง ๋ ๊ฐ์ ํ ์ด๋ธ์์ ์ ๋ฐ์ดํธ๊ฐ ์ด๋ฃจ์ด์ ธ์ผ ํ๋ฉฐ, ๋ ์ค ํ๋๋ผ๋ ์คํจํ ๊ฒฝ์ฐ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ์ด ๊นจ์ง ์ํ์ด ์๋ค.
๊ทธ๋ฌ๋... ์ํ๋ฒ ์ด์ค๋ ํ์ฌ ํธ๋์ญ์ ์ ๊ฐํธํ๊ฒ ์ง์ํ์ง ์๋๋ค๊ณ ํ๋ค. supabase Discussion ์๋ ์ธ๊ธ๋์๋ฏ, ์ํ๋ฒ ์ด์ค๋ PostgreSQL ์ ๊ธฐ๋ฐ์ผ๋ก ํ๊ณ ์์ด์ PostgreSQL ์์ ์ ๊ณตํ๋ ํจ์๋ฅผ ์ด์ฉํ๋ฉด ํน์ ์ฟผ๋ฆฌ์ ๋ํด ํธ๋์ญ์ ์ ์ ์ฉํ ์ ์๋ค! ๐ฅน
๋ฐ๋ผ์ ๋ ์์ฒญ(๊ฒ์๊ธ ์ญ์ /์ด๋ฏธ์ง ์ญ์ ) ์ ์์ ํ๊ฒ ์ฒ๋ฆฌํ๋ ค๋ฉด PostgreSQL ํจ์๋ฅผ ์์ฑํ์ฌ ํธ๋์ญ์ ์ ์ ์ฉํด ๋ณด๋๋ก ํ์.
โ ํจ์ ๋ง๋ค๊ธฐ
PostgreSQL ๋ฌธ์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ PostgreSQL ํจ์๋ฅผ ์์ฑํ์ฌ ํธ๋์ญ์ ์ด ์ ์ฉ๋๋๋ก ๊ตฌํํ๋ค.
create or replace function delete_post_and_images(post_id uuid)
returns void as $$
begin
-- ๊ฒ์๊ธ ์ญ์
update posts
set deleted_at = now()
where posts.post_id = post_id;
-- ์ด๋ฏธ์ง ์ญ์
update images
set deleted_at = now()
where images.post_id = post_id;
exception
when others then
raise;
end;
$$ language plpgsql;
๊ฒ์๊ธ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์ญ์ ํ๋ ๋์ deleted_at ํ๋๋ฅผ ์ ๋ฐ์ดํธํ๋ ๋ฐฉ์์ ์ฑํํ๋ค.
export const fetchDeletePost = async (postId: string) => {
const supabase = createClient();
const { data, error } = await supabase.rpc('delete_post_and_images', {
post_id: postId,
});
if (error) {
console.log('๊ฒ์๋ฌผ ์ญ์ ์ค ์ค๋ฅ ๋ฐ์ ', error);
throw new Error(`๊ฒ์๋ฌผ ์ญ์ ์ค ์ค๋ฅ ๋ฐ์: ${error.message}`);
}
return { message: '๊ฒ์๋ฌผ ์ญ์ ์ฑ๊ณต', data };
};
Supabase์์ ์ด ํจ์๋ฅผ ํธ์ถํ๊ธฐ ์ํด ์๋์ ๊ฐ์ด rpc ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
โ ํธ๋์ญ์ ํ ์คํธ
ํจ์๋ฅผ ๋ง๋ค์์ผ๋, ํธ๋์ญ์ ์ด ์ ์ ์ฉ๋๋์ง ํ ์คํธํด ๋ณด๊ณ ์ถ์๋ค. ์ด๋ป๊ฒ ํ ์คํธํ์ง...?
๊ณ ์๋ก ์ค๋ฅ ๋ด๊ธฐ ํ ์คํธ
ํจ์์ ํธ๋์ญ์ ์ด ์ ๋๋ก ๋์ํ๋์ง ํ ์คํธํ๊ธฐ ์ํด ์ผ๋ถ๋ฌ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ๋ฐฉ๋ฒ์ ๊ณ ์ํ๋ค. ํ ์คํธ๋ฅผ ์ํด ํจ์๋ฅผ ์ฝ๊ฐ ์์ ํ์ฌ ๊ฒ์๊ธ์ post_id ์ ์ด๋ฏธ์ง์ post_id ๋ฅผ ๊ฐ๊ฐ ๋ณ๋์ ๊ฐ์ผ๋ก ๋ฐ๋๋ก ๋ณ๊ฒฝํ๋ค. ์ดํ images ์ post_id ์ ์๋ชป๋ ๊ฐ์ ๋ฃ์ด ์ค๋ฅ๋ฅผ ์ ๋ํ๋ค.
create or replace function delete_post_and_images(post_post_id uuid, image_post_id uuid)
returns void as $$
begin
-- ๊ฒ์๊ธ ์ญ์
update posts
set deleted_at = now()
where posts.post_id = post_post_id;
-- ์ด๋ฏธ์ง ์ญ์
update images
set deleted_at = now()
where images.post_id = image_post_id;
exception
when others then
raise;
end;
$$ language plpgsql;
ํ ์คํธ ๊ฒฐ๊ณผ
- ๊ณ ์๋ก ์ค๋ฅ ๋ฐ์ : images ํ ์ด๋ธ์ post_id ์ ์ด์ํ ๊ฐ์ ๋ฃ์ด ์คํํ ๊ฒฐ๊ณผ, ํจ์ ์คํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
- ํธ๋์ญ์ ๋กค๋ฐฑ ํ์ธ : ์ค๋ฅ๊ฐ ๋ฐ์ํ์์๋ ๋ถ๊ตฌํ๊ณ posts ์ images ํ ์ด๋ธ ๋ชจ๋์์ deleted_at ํ๋๊ฐ ์ ๋ฐ์ดํธ๋์ง ์์๋ค.
์ฌ์ค ํ ์คํธ ๋ฐฉ๋ฒ์ด ์ ์ ํ์ง ํ๋จ์ด ์์ง๋ ์์ง๋ง... ์ด์ ๋๋ฉด ํธ๋์ญ์ ์ด ์ ์์ ์ผ๋ก ์๋ํ๊ณ ์๋ค๊ณ ๋ด๋ ๋๊ฒ ์ง..?? :)
์๋ฌดํผ! ์ ๋๋ ๊ฑฐ๋ก ๋ณด๊ณ ! ๋ ์์ (๊ฒ์๊ธ ์ญ์ ์ ์ด๋ฏธ์ง ์ญ์ ) ์ค ํ๋๋ผ๋ ์คํจํ ๊ฒฝ์ฐ ์ ์ฒด ์์ ์ด ๋กค๋ฐฑ๋์ด ๋ฐ์ดํฐ์ ๋ฌด๊ฒฐ์ฑ์ด ์ ์ง๋๋ค๊ณ ๋ณผ ์ ์๋ค!!
โ ๊ฒฐ๋ก
PostgreSQL ํจ์๋ฅผ ํ์ฉํ ํธ๋์ญ์ ๊ตฌํ๊ณผ Supabase์ rpc ํธ์ถ์ ํตํด ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ์ ๋ณด์ฅํ ์ ์์๋ค. ํนํ, ํ ์คํธ๋ฅผ ํตํด ํธ๋์ญ์ ์ด ์๋ํ ๋๋ก ๋์ํ๋์ง ํ์ธํ ์ ์์๋ค. ์ด ๋ฐฉ๋ฒ์ Supabase์ PostgreSQL์ ๊ฐ๋ ฅํ ์กฐํฉ์ ํ์ฉํ ์ค์ฉ์ ์ธ ์ ๊ทผ๋ฒ์ด๋ผ ์๊ฐ๋๋ค.
๊ฐ์ ํ๊ฑฐ๋ ๊ณ ๋ฏผ์ด ํ์ํ ๋ถ๋ถ์ด ์๋ค๋ฉด ์ธ์ ๋ ๊ณต์ ๋ถํ๋๋ฆฝ๋๋ค!
