[Node.js] #12 MySQL๊ณผ Sequelize ๋ณด์ถฉ (for CRUD๐Ÿ“ฅ๐Ÿ‘€โœ๏ธ๐Ÿ“ค)

โœจNew Wisdomโœจยท2020๋…„ 8์›” 26์ผ
2

๐Ÿ“— Nodejs ๐Ÿ“—

๋ชฉ๋ก ๋ณด๊ธฐ
13/20
post-thumbnail

๐Ÿ’ฌ ๐Ÿ““Node.js ๊ต๊ณผ์„œ๐Ÿ““๋ฅผ ์ธ๊ฐ•์œผ๋กœ ๊ณต๋ถ€ ์ค‘์ธ๋ฐ ํ”„๋กœ์ ํŠธ ๋”ฐ๋ผํ•˜๋ฉด์„œ ๊ทธ๋ƒฅ ๋ฌด์ž‘์ • ๋”ฐ๋ผํ•˜๊ธฐ ๋ณด๋‹ค๋Š”
์ž˜ ๋ชจ๋ฅด๊ฒ ๋Š”๊ฑฐ ํ•œ๋ฒˆ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋ ค ํ•œ๋‹ค.
sequelize์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„์คฌ์—ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ์งง๊ฒŒ ๋‹ค๋ค„์„œ ํ”„๋กœ์ ํŠธ ํ•˜๋ฉด์„œ ์ด๊ฒŒ ๋ฌด์Šจ ๋ฉ”์†Œ๋“ ์ง€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ •๋„๋Š” ์•Œ์•„์•ผ๊ฒ ์–ด์„œ ๋”ฐ๋กœ ์ •๋ฆฌ...โœ๏ธ

๐Ÿšฉ Create (=Insert)

sequelize๋กœ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” create()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค!

...
const { Post } = require('../models');
...
router.post('/',async (req,res,next)=>{
	let body = req.body;
    
    Post.create({
    	title : body.postTitle,
        author : body.author
    })
    .then(result => {
    	console.log("success");
    })
    .catch(err => {
    	console.log("fail");
    })
});
  • create() ์˜ ์ธ์ž๋กœ ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€ํ•  ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ์ฒด๋กœ ์ •์˜ํ•˜์—ฌ ์ „๋‹ฌํ•œ๋‹ค.

๐Ÿ˜ฏ ์ฐธ๊ณ ๋กœ sequelize๋Š” Promise ๋ฌธ๋ฒ•์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋–„๋ฌธ์— ์ฟผ๋ฆฌ๊ฐ€ ์ž˜ ์ˆ˜ํ–‰๋˜์—ˆ์œผ๋ฉด then() ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค.

๐Ÿšฉ Read (=Select)

router.get('/hashtag',async(req,res,next)=>{
    const query = req.query.hashtag;
    if(!query){
        return res.redirect('/');
    }
    try{
        const hashtag = await Hashtag.findOne({where:{title:query}});
        let posts = [];
        if(hashtag){
            // A.getB : ๊ด€๊ณ„์žˆ๋Š” row ์กฐํšŒ
            // A.addB : ๊ด€๊ณ„ ์ƒ์„ฑ
            // A.setB : ๊ด€๊ณ„ ์ˆ˜์ •
            // A.removeB : ๊ด€๊ณ„ ์ œ๊ฑฐ
            post = await hashtag.getPosts({
                include : [{model : User}]
            })
        }
        return res.render('main',{
            title : `${query} | Nodebird`,
            user : req.user,
            tiwits : posts, // ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ
        })
    }catch(error){
        console.error(error)
        next(error);
    }
})
  • findOne()์ด๋‚˜ findAll()๋กœ ๋ฐ์ดํ„ฐ๋ฅผ select ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • where : ์กฐ๊ฑด์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • attributes : ์ผ๋ถ€ ์†์„ฑ๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • order : ์ •๋ ฌ ์กฐ๊ฑด์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿšฉ Update

...
const { Post } = require('../models');
...
router.post('/:id',async (req,res,next)=>{
	let body = req.body;
    let postId  = req.params.id
    
    Post.update({
    	title : body.postTitle,
        author : body.author
    },{
    	where : {id:postId}
    })
    .then(result => {
    	console.log("success");
    })
    .catch(err => {
    	console.log("fail");
    })
});
  • update() : ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ˆ˜์ •ํ•  ๊ฐ’๋“ค์„ ์ ์–ด์ฃผ๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์กฐ๊ฑด์„ ๋ช…์‹œํ•œ๋‹ค.

๐Ÿšฉ Delte (=Destroy)

...
const { Post } = require('../models');
...
router.delete('/:id',async (req,res,next)=>{
    let postId  = req.params.id
    
    Post.destroy({
    	where : {id:postId}
    })
    .then(result => {
    	console.log("success");
    })
    .catch(err => {
    	console.log("fail");
    })
});
  • where ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์—ฌ ์–ด๋–ค ๊ฐ’์„ ์‚ญ์ œํ•  ์ง€ ๋ช…์‹œํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์ •๋ฆฌํ•˜๋‹ˆ๊นŒ ์ด์ œ ์ข€ sequelize์— ๋Œ€ํ•œ ๊ฐ์ด ์˜ค๋Š”๋“ฏ!
๋ฌด์ž‘์ • ๋”ฐ๋ผ์น˜์ง€ ๋ง์ž!

profile
๐Ÿš› ๋ธ”๋กœ๊ทธ ์ด์‚ฌํ•ฉ๋‹ˆ๋‹ค https://newwisdom.tistory.com/

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