Node.js/Sequelize

[ORM] ๐Ÿ“š ์‹œํ€„๋ผ์ด์ฆˆ - ๋‹ค์ค‘ ์กฐ์ธ ๋ฐฉ๋ฒ•

์ธํŒŒ_ 2022. 6. 5. 09:38

sequelize-join

Sequelize ์—ฌ๋Ÿฌ JOIN

์‹œํ€„๋ผ์ด์ฆˆ ORM์˜ ์กฐ์ธ ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

์™ ๋งŒํ•˜๋ฉด ์„ฑ๋Šฅ์„ ์œ„ํ•ด์„œ ํ•˜๋‚˜์ด์ƒ ์กฐ์ธ์„ ํ•˜๋Š” ๊ฒƒ์€ ํ”ผํ•ด์•ผ ๋˜๋Š” ๊ฒƒ์ด ๋งž์ง€๋งŒ, ์—ฐ๊ด€๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฟ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์–ด์ฉ”์ˆ˜ ์—†์ด ๋‹ค์ค‘ ์กฐ์ธ์„ ํ•  ํ•„์š”์„ฑ์ด ์ƒ๊ธด๋‹ค. (์ด๋ฅผ ๋ฐฉ์ง€ ํ•˜๊ธฐ ์œ„ํ•ด ์—ญ์ •๊ทœํ™”๋ฅผ ์ž˜ํ•ด์•ผ ๋˜์ง€๋งŒ..)

 

๋‹ค์Œ ์‚ฌ์ง„์€ ์˜ˆ์ œ ์ฝ”๋“œ์— ์“ฐ์ธ erd ์ด๋‹ค. ๊ด€๊ณ„๋ฅผ ์ž˜๋ณด๊ณ  ์กฐ์ธ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž.

sequelize-join


์‹œํ€„๋ผ์ด์ฆˆ JOIN

  • join 2 tables
const user = await User.findOne({
    include: [{ // ์‹œํ€„๋ผ์ด์ฆˆ ์กฐ์ธ์€ ๊ธฐ๋ณธ inner join
        model: Comment, // joinํ•  ๋ชจ๋ธ
        attributes: ['id'], // selectํ•ด์„œ ํ‘œ์‹œํ•  ํ•„๋“œ ์ง€์ •
        where: { 
            id: 1, // on Comment.id = 1
        },
    }]
});
 
// ๋˜๋Š” โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

const user = await User.findOne({ ... }); // ๋จผ์ € user ์ฟผ๋ฆฌ๋ฅผ ์–ป๊ณ 
const comments = await user.getComments({ // user์™€ comments๋ฅผ ๊ด€๊ณ„๋งบ์–ด getํ•œ๋‹ค.
    attributes: ['id'],  // joinํ•ด ๊ฐ€์ ธ์˜ฌ comments๋ฅผ.id
    where: {
    	id: 1, // comments๋ฅผ.id = 1
    },
});
select users.*, comments.id 
from users
inner join comments
on comments.id = users.id
where comments.id = 1
LIMIT 1;

์‹œํ€„๋ผ์ด์ฆˆ ๋‹ค์ค‘ JOIN

  • join 3 tables
  • ํ•˜๋‚˜์˜ table์ด ๋‹ค๋ฅธ ๋‘ table๊ณผ ๊ด€๊ณ„๋ฅผ ๋งบ์—ˆ์„๋•Œ
  • A - B - C ์ด๋ ‡๊ฒŒ ์—ฐ๊ฒฐ๋œ ํ…Œ์ด๋ธ”์—์„œ B๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ๋•Œ
const user = User.findOne({
   where: { id : 123123 },
   include: [
      {
         model: User,
         attributes: ['id', 'nick'],
         as: 'Followers',
      },
      {
         model: User,
         attributes: ['id', 'nick'],
         as: 'Followings',
      },
   ],
});
select users.*, Followers.id, Followers.nick, Followings.id, Followings.nick, 
from users 
inner join follow as Followers on users.id = Followers.followerId 
inner join follow as Followings on users.id = Followings.followingId 
where users.id = 123123

์‹œํ€„๋ผ์ด์ฆˆ ๋‹ค์ค‘ ์ค‘์ฒฉ JOIN

  • join 3 tables
  • ๊ฐ table๋ผ๋ฆฌ ๋งˆ๋‹ค ๊ด€๊ณ„๋ฅผ ๋งบ์—ˆ์„๋•Œ, table์˜ table์ด ์ค‘์ฒฉํ•ด์„œ ์กฐ์ธ
  • A - B - C ์ด๋ ‡๊ฒŒ ์—ฐ๊ฒฐ๋œ ํ…Œ์ด๋ธ”์—์„œ A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ๋•Œ
const posts = Hashtag.findOne({
   where: { title: query },
   include: [
      {
         model: Post,
         attributes: [ ... ],

         // include ์•ˆ์˜ include
         include: [
            {
               model: User,
               attributes: ['id', 'nick'],
            },
         ],
      },
   ],
});
// ์ค‘์ฒฉ include๊ฐ€ ๋ณด๊ธฐ ์•ˆ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด get๋ชจ๋ธ ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด ํ•œ๋ฒˆ ์กฐ์ธ์„ ํ•˜๊ณ , include๋ฅผ ์“ฐ๋ฉด ๋ณด๋‹ค ๊ฐ€๋…์„ฑ์ด ๋Š˜์–ด๋‚œ๋‹ค.

const hashtag = await Hashtag.findOne({ where: { title: query } });
let posts = [];

if (hashtag) {
    posts = await hashtag.getPosts({ // hashtag.getPosts() => Hashtag ์™€ Post ์กฐ์ธ
        include: [
           {
              model: User, // Post ์™€ User ์กฐ์ธ
              attributes: ['id', 'nick'],
           },
        ],
    }); // hastags, posts, users ์‚ผ์ค‘ ์กฐ์ธํ•ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
}