https://sequelize.org/docs/v6/core-concepts/model-instances/
Model Instances | Sequelize
As you already know, a model is an ES6 class. An instance of the class represents one object from that model (which maps to one row of the table in the database). This way, model instances are DAOs.
sequelize.org
์ ๋ชฉ์ ๋ ์ฝ๋๋ ์ ํํ ๊ฐ์ ๋์์ ์ํํ๋ ์ฝ๋์ด๋ค.
๋๋ฌด ์ง์ฌ์ง ํ์์๋ง ์ฝ๋งค์ฌ์์๋ ํ๋ ๋๋์ ๊ต์ฅํ ํฌ๊ฒ ๋ฐ์๋ค.
์ค๋ ์ฝ๋ ํผ๋๋ฐฑ์ ๋ฐ์ผ๋ฉด์ "์ด๋ ๊ฒ๋ ํ ์ ์์๋ค?" ๋ผ๋ ๋๋์ ํฌ๊ฒ ๋ฐ์ ๋จ๊ฒจ๋๋ค.
๊ธฐ์กด์ ๊ฒ์๊ธ์ ์ญ์ ํ๋ ์ฝ๋์ด๋ค.
// 5. post(๊ฒ์๊ธ) ์ญ์
router.delete('/posts/:postId', authMiddleware, async (req, res) => {
const { postId } = req.params;
const { userId } = res.locals.user;
try {
const post = await Posts.findOne({ where: { postId } });
// ํด๋น ๊ฒ์๊ธ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ
if (!post) {
return res.status(403).json({ errorMessage: '๊ฒ์๊ธ์ด ์กด์ฌํ์ง ์์ต๋๋ค..' });
}
// ๊ฒ์๊ธ์ userId์ ํ์ฌ ๋ก๊ทธ์ธ๋ user์ userId๊ฐ ์ผ์นํ์ง ์์ ๊ฒฝ์ฐ
if (!userId || post.UserId !== userId) {
return res.status(403).json({ errorMessage: '๊ฒ์๊ธ ์ญ์ ๊ถํ์ด ์กด์ฌํ์ง ์์ต๋๋ค.' });
}
// ๊ฒ์๊ธ ์ญ์
await Posts.destroy({ where: { postId } }).catch((err) => {
res.status(401).json({ errorMessage: '๊ฒ์๊ธ์ด ์ญ์ ๋์ง ์์์ต๋๋ค.' });
});
return res.status(200).json({ message: '๊ฒ์๊ธ์ ์ฑ๊ณต์ ์ผ๋ก ์ญ์ ํ์์ต๋๋ค.' });
} catch (error) {
console.log(error.message);
res.status(400).json({ errorMessage: '๊ฒ์๊ธ ์ญ์ ์ ์คํจํ์์ต๋๋ค.' });
}
});
์์ธ์ฒ๋ฆฌ์์ ํด๋น ๊ฒ์๊ธ์ ์กด์ฌ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ , ๊ทธ ๊ณผ์ ์์ ์ด๋ฏธ ํ ๋ฒ db์ ์ ๊ทผํด ์ฐ๋ฆฌ๊ฐ ์ญ์ ํ๊ณ ์ ํ๋ post๋ฅผ ๊ธ์ด์๋ค.
๋ ์ด๋ ๊ฒ ๊ธ์ด์จ post๋ฅผ ๊ฒ์๊ธ์ด ์กด์ฌํ๋์ง ์ํ๋์ง์ ํ ๋ฒ์ ์์ธ์ฒ๋ฆฌ์ ์ฌ์ฉํ์ ๋ฟ์ด๋ค.
๊ทผ๋ฐ ์ด๋ ๊ฒ ๊ฐ์ ธ์จ post๋ฅผ ์ค์ง์ ์ธ ๊ฒ์๊ธ ์ญ์ ๋ก์ง์์ ์ฌ์ฉํ ์๊ฐ์ ํ์ง ๋ชปํ๋ค.
์ด๋ฏธ ๊ธ์ด์จ post๋ฅผ destroy() ์ํจ ์ฝ๋
// 5. post(๊ฒ์๊ธ) ์ญ์
router.delete('/posts/:postId', authMiddleware, async (req, res) => {
const { postId } = req.params;
const { userId } = res.locals.user;
try {
const post = await Posts.findOne({ where: { postId } });
// ๊ฒ์๊ธ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ
if (!post) {
return res.status(403).json({ errorMessage: '๊ฒ์๊ธ์ด ์กด์ฌํ์ง ์์ต๋๋ค..' });
}
// ๊ฒ์๊ธ์ userId์ ํ์ฌ ๋ก๊ทธ์ธ๋ user์ userId๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ
if (!userId || post.UserId !== userId) {
return res.status(403).json({ errorMessage: '๊ฒ์๊ธ ์ญ์ ๊ถํ์ด ์กด์ฌํ์ง ์์ต๋๋ค.' });
}
// ๊ฒ์๊ธ ์ญ์
post.destroy().catch((err) => {
res.status(401).json({ errorMessage: '๊ฒ์๊ธ์ด ์ญ์ ๋์ง ์์์ต๋๋ค.' });
});
return res.status(200).json({ message: '๊ฒ์๊ธ์ ์ฑ๊ณต์ ์ผ๋ก ์ญ์ ํ์์ต๋๋ค.' });
} catch (error) {
console.log(error.message);
res.status(400).json({ errorMessage: '๊ฒ์๊ธ ์ญ์ ์ ์คํจํ์์ต๋๋ค.' });
}
});
์ด๋ฐ์์ผ๋ก ๋๊ฐ์ด destroy๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ธ์ด์จ post๋ฅผ ์ญ์ ๊ฐ ๊ฐ๋ฅํ๋ค.
๋ํ DB์์ ์กฐ๊ฑด์ผ๋ก ์ฐพ์ ํ์์์ด ์ด๋ฏธ ์ฐพ์์จ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ await์ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
#destroy #sequelize_logging
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] : Set (0) | 2023.07.22 |
---|---|
[JS] : Map (0) | 2023.07.21 |
[JavaScript] : tirm() (0) | 2023.07.14 |
[JavaScript] : TypeError: Converting circular structure to JSON ์ค๋ฅํด๊ฒฐ (0) | 2023.06.14 |
[JavaScript] : ๋จ์ถ์์ฑ๋ช , ์ ๊ฐ๊ตฌ๋ฌธ, ๋๋จธ์ง ๋งค๊ฐ๋ณ์, ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด. (0) | 2023.06.09 |