YS's develop story

MySQL ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ํ•œ๋ฒˆ์— ์ €์žฅํ•˜๊ธฐ (Node js, mysql2 promise) ๋ณธ๋ฌธ

Database/MySQL

MySQL ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ํ•œ๋ฒˆ์— ์ €์žฅํ•˜๊ธฐ (Node js, mysql2 promise)

Yusang 2021. 7. 1. 09:51

๐Ÿฅ MySQL ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ํ•œ ๋ฒˆ์— ์ €์žฅํ•˜๊ธฐ (Node js, mysql2 promise) 

โŒ› Problem

๐Ÿ˜• ๋ณ€๊ฒฝ ์ „ ์ฝ”๋“œ

      const connection = await pool.getConnection(async (conn) => conn);
      
      ...........
      
      
      for (i in reviewImg) {
            let postReviewImgParams = [
                getReviewIdx[0].reviewIdx,
                reviewImg[i]
            ]
            await hospitalDao.postReviewImg(connection, postReviewImgParams);
        }
async function postReviewImg(connection, postReviewImgParams) {
    const postReviewImgQuery = `
    INSERT INTO ReviewImg(reviewIdx, reviewImg) VALUES (?,?);
    `;
    const [postReviewImgRow] = await connection.query(
        postReviewImgQuery,
        postReviewImgParams
    );

    return postReviewImgRow;
}

์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ๋ฆฌ๋ทฐ์—๋Š” ํ•˜๋‚˜์˜ ๋ฆฌ๋ทฐ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ๋ทฐ ์ด๋ฏธ์ง€ ํ…Œ์ด๋ธ”์„ ๋”ฐ๋กœ DB์— ์ƒ์„ฑํ•˜์—ฌ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๊ณ , ๋ฆฌ๋ทฐ๋ฅผ ์ €์žฅ ํ•  ๋•Œ ์ด๋ฏธ์ง€๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ €๋Š” ์œ„์™€ ๊ฐ™์ด ๋กœ์ง์„ ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Š” ์ด๋ฏธ์ง€ ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ์„ connection ํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ €๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ•œ ๋ฒˆ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•,

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์— ํ•œ ๋ฒˆ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“œ Solution

 

How do I do a bulk insert in mySQL using node.js

How would one do a bulk insert into mySQL if using something like https://github.com/felixge/node-mysql

stackoverflow.com

์œ„ ๋งํฌ์—์„œ ๊ทธ ํ•ด๋‹ต์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๐Ÿ˜„ ๋ณ€๊ฒฝ ํ›„ ์ฝ”๋“œ

       const connection = await pool.getConnection(async (conn) => conn);
      
       ...........
      
        
        let reviewImgList = [];
        
        for (i in reviewImg) {
            reviewImgList.push([getReviewIdx[0].reviewIdx,reviewImg[i]])
        }
        
        if (reviewImgList.length > 0) {
            await hospitalDao.postReviewImg(connection, [reviewImgList]);
        }
async function postReviewImg(connection, [values]) {
    const postReviewImgQuery = `
    INSERT INTO ReviewImg(reviewIdx, reviewImg) VALUES ?;
 
    `;
    const [postReviewImgRow] = await connection.query(
        postReviewImgQuery,
        [values]
    );

    return postReviewImgRow;
}

 

ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ, ๋ฌธ์ œ ์—†์ด ์ž˜ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค! ๐Ÿ˜„

Comments