mysql(2๐Ÿ˜) query async/await

Jaymeeยท2021๋…„ 9์›” 13์ผ
0

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์€ ๋‹ค async/await์œผ๋กœ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์˜€๋Š”๋ฐ, ์ฟผ๋ฆฌ๋ฌธ์€ ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๊ณ  ์ฝœ๋ฐฑ์ง€์˜ฅ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๋„ˆ๋ฌด ๊ธฐ๊ณ„์ ์œผ๋กœ ์ฝ”๋”ฉํ•˜์ง€ ์•Š์•˜๋‚˜ ์‹ถ์€ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

version 1

const sql_team_no = 'select max(team_no) as newTeam from travel_with;'
       
        conn.query(sql_team_no, async function(err, data){
            if(err){
                console.log(err)
                return res.send('failed to get new team number')
            }
            else{
              
              ~~~
                
                const sql = 'insert into travel_with set ?;'
                        conn.query(sql, params, function(err, data){
                            if(err){
                                console.log(err)
                                return res.status(statusCode.OK).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.QUERY_ERROR, 
                                    "failed to store new plan (existed user in travel_with table)"))
                            }else{

์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ์˜ ๋ฌธ์ œ์ ์ด ๋ฌด์—‡์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋‚˜...

sql์„ ์‹คํ˜„ํ•˜๊ณ  ๋งŒ์•ฝ์— err๊ฐ€ ๋‚œ๋‹ค๋ฉด err์— ํ•ด๋‹นํ•˜๋Š” response๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ๋น ์ ธ๋‚˜์˜ค๋ฉด ์–ด๋””๋กœ ๊ฐ€๋‚˜.
๋ฐ”๋กœ ์œ„์— ๋ณด๋ฉด sql_team_no ์ฟผ๋ฆฌ๋ฅผ ์‹คํ˜„ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๊ฐ„๋‹ค.
๊ทธ๋Ÿฌ๋ฉด? ๋ฆฌํ„ด์„ ๋‘๋ฒˆํ•œ๋‹ค
๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด Error: Can't render headers after they are sent to the client. ์™€ ๊ฐ™์€ ์„œ๋ฒ„ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋งŽ์ด ๊ฒช๋Š” ์—๋Ÿฌ๋ฅผ ๋ณด๊ฒŒ ๋œ๋‹ค.
๋‚˜๋Š” ์ฒ˜์Œ์— ๋‚ด๊ฐ€ try catch๋ฅผ ์ž˜ ๋ชป ์ดํ•ดํ•˜๊ณ  ์žˆ๋‚˜...? ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด catch๋กœ ๊ฐ€์•ผ ํ•˜๋Š”๋ฐ if(err) ์—ฌ๊ธฐ์„œ ๊ฑธ๋ ค์„œ response๋ฅผ ๋‘๋ฒˆ ํ•ด์„œ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋Š”๊ฑด๊ฐ€??

์•„๋‹ˆ๋‹ค... ๊ทธ๋ƒฅ ์•ž์„œ ๋งํ•œ๊ฒƒ์ด ์ž˜๋ชป์ด์—ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ๋‚˜์™€๋„ ๋˜ ํ•จ์ˆ˜์•ˆ์— ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—...

๊ฒฐ๊ตญ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ๋‚ด๊ฐ€ ๋ชปํ•œ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ

version 2

const connection = await mysql.createConnection(conn.db_info);

      
const locationResult = await getLocation(latlng)
const country_code = locationResult.data.plus_code.global_code // country code ex)87G8M376+PJ - koera
const country = country_code.slice(0,4);

const sql = `select * from travel_with where region_info like '${country}%' and isfinished = '0';` 
const result = await connection.query(sql)
AWS.config.update(userConfig.aws_iam_info);
const docClient = new AWS.DynamoDB.DocumentClient();

์œ„์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฌธ์„ async/await ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ response๋ฅผ ํ•ด๋„ ๋˜ ๋‹ค์‹œ ํ•จ์ˆ˜์•ˆ์— ๊ฐ‡ํžˆ๋Š” ๊ทธ๋Ÿฐ ๋ถˆ์ƒ์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

์–ด? ๋‚˜๋Š” nodejs ์—์„œ mysql ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•ด๋„ async/await ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋˜๋˜๋ฐ...? ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋‚˜ ๋˜ํ•œ ๊ธฐ์กด์— ์‚ฌ์šฉํ–ˆ๋˜ ๋ชจ๋“ˆ๋กœ๋Š” async/await๋ฅผ ์ฒ˜๋ฆฌ ํ•˜์ง€ ๋ชปํ•ด์„œ ๋ญ์ง€? ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์—ˆ๋‹ค.

mysql vs mysql2

๐Ÿ‘€ mysql2 ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
์ฆ‰, mysql์€ ์ฝœ๋ฐฑ๊ธฐ๋ฐ˜์ด๊ณ , mysql2์€ promise๊ฐ์ฒด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

mysql์€ ์ฝœ๋ฐฑ๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— promise๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” promise-mysql ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ mysql2์€ ๋‹ค๋ฅธ ๋ชจ๋“ˆ ํ•„์š”์—†์ด promise ๊ฐ์ฒด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ทธ๋Ÿฌํ•˜๋‹ค... ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ์€ mysql2๋ฅผ ํ†ตํ•ด์„œ promise๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์•ž์œผ๋กœ mysql ๋ชจ๋“ˆ์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๋“ฏ...^^ ๋ฌด์กฐ๊ฑด mysql2 ๐Ÿ‘‹

profile
backend developer

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