Session 24. CRUD - Updata/Delete

๊น€๋ฏผ์žฌยท2021๋…„ 10์›” 9์ผ
0

TIL, WeCode, Courseย 

๋ชฉ๋ก ๋ณด๊ธฐ
43/48
post-thumbnail

*๐Ÿ”Study Keyword :

  • ๋ฐ์ดํ„ฐ๋ฅผ ๐Ÿ”‘upadate ํ•˜๊ณ  ๐Ÿ—๏ธdeleteํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด์ž~

1. upadate

-WHAT ISโ“

upadate๋ž€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค

- ๐Ÿค” upadate์™€ delete์˜ ์ฐจ์ด๋Š” ๋ญ๊ฐ€์žˆ์„๊นŒ์š”?

  • upadate๋Š” ์ˆ˜์ •! delete๋Š” ์‚ญ์ œ! ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ deleteํ›„ ์ƒˆ๋กœ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค๋ฉด?
    1> ๊ด€๊ณ„๋œ fk๊ฐ€ ๊ผฌ์ธ๋‹ค.
    2> ๋กœ๊ทธ๋กœ ๋‚จ์€ ์ด๋ ฅ๊ด€๋ฆฌ๊ฐ€ ์•ˆ๋œ๋‹ค.
    3> ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ค๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ฟผ๋ฆฌ๋ฅผ ๋” ๋งŽ์ด ์“ฐ๊ฒŒ ๋œ๋‹ค.

Prisma์—์„œ U.D ๊ตฌํ˜„ํ•˜๊ธฐ

-HOW TO USEโ•โ“

1_1. prisma update๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

  • ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋• PUT ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • prisma.user.updat ์™ธ์—๋„ updateMany, upsert ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.
<script>
// Router.js
router.put("/update/", usersController.updateUser);
// Dao.js
const updateUser = async (req, res) => {
  const { email, password, user_name } = req.body;
  const { id } = req.query;
  return await prisma.user.update({
    where: {
      id: Number(id),
    },
    data: {
      email: email,
      password: password,
      userName: user_name,
    },
  });
};
</script>
  • where์— ์—…๋ฐ์ดํŠธํ•˜๊ณ ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์กฐ๊ฑด์„ ๋„ฃ๋Š”๋‹ค.
    (id๊ฐ€ request์—์„œ ๋ณด๋‚ด์ฃผ๋Š” id์™€ ๋™์ผํ–ˆ์„ ๋•Œ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค๋Š” ์˜๋ฏธ)
  • data์— ์ˆ˜์ •ํ•  column๋ช…์„ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋„ฃ์€ ๋’ค ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ˆ˜์ •ํ•œ๋‹ค.

1_2. sql update ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

<script>
const updateUser = async (req, res) => {
  const { email, password, user_name } = req.body;
  // req.params๋กœ ํ•ด๋‹น id์ฐพ์•„์„œ ์—…๋ฐ์ดํŠธ ํ• ๋•Œ
  const { id } = req.params;
  // req.query๋กœ ํ•ด๋‹น id์ฐพ์•„์„œ ์—…๋ฐ์ดํŠธ ํ• ๋•Œ
  //const { id } = req.query;
  return await prisma.$queryRaw`
    UPDATE
      users
    SET
      email=${email}, password=${password}, user_name=${user_name}
    WHERE
      id=${id};
  ;`;
};
</script>
  • UPDATE, SET, WHERE ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
  • UPDATE (์—…๋ฐ์ดํŠธ ํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„) SET (์—…๋ฐ์ดํŠธ ํ•  ์นผ๋Ÿผ๊ณผ ๊ฐ’) WHERE (์—…๋ฐ์ดํŠธํ•  ์กฐ๊ฑด)
    ๐Ÿ’ก์ฃผ์˜๐Ÿ’ก)
  • WHERE๋ฌธ ์ž…๋ ฅํ•˜์ง€ ์•Šํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋œ์ž๋Š” ์ ์„ ํ•ญ์ƒ์ฃผ์˜!

๐Ÿ’ก์ฃผ์˜2๐Ÿ’ก)

  • req.query๋กœ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ

    router.put("/update/", usersController.updateUser);
    http://localhost:8000/users/update/?id=12
    ๋’ค์— ์žˆ๋Š” ?id=12๊ฐ€ req.query๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด {id : 12}๊ฐ€ ๋‚˜์˜จ๋‹ค.
  • req.params๋กœ ์—…๋ฐ์ดํŠธํ•  ๋•Œ

    http://localhost:8000/users/update/11
  • req.params๋Š” ๋ผ์šฐํ„ฐ์— ์ „๋‹ฌํ•˜๋Š” "/update/:id์— ์žˆ๋Š” :id๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    router.put("/update/:id", usersController.updateUser);

2. delete๋ฉ”์†Œ๋“œ

2_1. prisma delete๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

  • delete๋Š” ํ•œ๊ฐœ ์‚ญ์ œ, deleteMany๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ
<script>
// Router.js
// query๋กœ ์กฐํšŒํ•ด์„œ ์‚ญ์ œ
router.delete("/delete/",usersController.deleteUser);
// Dao.js
const deleteUser = async (req, res) => {
	const { id } = req.query;
	const { email, password } = req.body;
  const updateUser = await prisma.$queryRaw`
    UPDATE users
    SET email=${email}, password=${password}
    WHERE id=${id};
`;
  res.json(updateUser);
});
</script>

2_2. sql delete ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

<script>
// Router.js
// params๋กœ ์กฐํšŒํ•ด์„œ ์‚ญ์ œ
router.delete("/delete/:id",usersController.deleteUser);
// Dao.js
const deleteUser = async (req, res) => {
  const { id } = req.params;
  return await prisma.$queryRaw`
  DELETE FROM
    users
  WHERE id=${id}
  `;
};
</script>

3. ๐Ÿ”…hard & soft delete๐Ÿ”…

  • ์†Œํ”„ํŠธ ๋”œ๋ฆฌํŠธ๋ž€
  • ์œ ์ €๊ฐ€ ํšŒ์›์„ ํƒˆํ‡ดํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ ์ „๋ถ€๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์„ cascade๋ผ๊ณ  ํ•œ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ํšŒ์›์„ ํƒˆํ‡ดํ•ด๋„ ๊ตฌ๋งค์ด๋ ฅ์„ ์ง€์šฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด cascade = true, false๋กœ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ์„ค์ •ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
  • ALTER TABLE users ADD is_deleted TINYINT
  • update users set is_delete=1 where id=6
    ๋ฅผ ํ†ตํ•ด์„œ null=> 1๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
  • ์ด์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‹จ์—์„œ ์œ ์ €๋ฅผ ์‚ญ์ œํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ์‚ญ์ œํ•  ๊ฑด์ง€ ์‚ญ์ œ๋œ ์ƒํƒœ์—์„œ ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ƒํƒœ๊ฐ€ 1์ธ ๊ฒƒ๋“ค๋งŒ ๋”ฐ๋กœ ์‚ญ์ œํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์น ๊ฑด์ง€ ๋“ฑ์˜ ๊ณผ์ •์€ ์„œ๋น„์Šค ์ฐจ์›์—์„œ ๋‹ฌ๋ผ์ง„๋‹ค.
  • ์ปฌ๋Ÿผ์ด is_delete์˜ ๊ฐ’์ธ 1์ธ ๋กœ์šฐ๋งŒ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ delete_at์„ ์ปฌ๋Ÿผ์œผ๋กœ ํ•จ๊ป˜ ๋งŒ๋“ค์–ด ์ง์„ ์ด๋ฃฌ๋‹ค.

๐Ÿ’กTIP)

  • soft delete๋กœ ์‚ฌ์šฉ์ž๋“ค์„ ํ•œ ๋ฒˆ์— ์‚ญ์ œํ•˜๋Š” ์„œ๋น„์Šค๋ณด๋‹ค ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š” ์„œ๋น„์Šค๋กœ ๊ตฌํ˜„ํ•ด๋ณด๋Š” ์‹œ๋„ํ•ด๋ณด๊ธฐ
  • is_delete์™€ deleted_at ์นผ๋Ÿผ ์ถ”๊ฐ€ํ•ด์„œ ์ ์šฉํ•ด๋ณด๊ธฐ
  • Prisma foreighkey cascade ์„ ์–ธ

*๐Ÿ’กconclusion

  • CRUD๋Š” ๋‹ค ํ•  ์ค„ ์•Œ์•„์•ผ ์ง„์งœ ๋ฒก์—”๋“œ ๊ฐœ๋ฐœ์ž!

#๐Ÿ“‘Study Source

  • ๋’ค๋Šฆ์€ CRUD UD ๊ตฌํ˜„ ์ค‘ :{
profile
์ž๊ธฐ ์‹ ๋ขฐ์˜ ํž˜์„ ๋ฏฟ๊ณ  ์‹ค์ฒœํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ ์žํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€