Commit 4928599e authored by KangMin An's avatar KangMin An
Browse files

Update : MySQL 비동기 처리 코드 갱신 및 기타 코드 갱신

parent d0ccf743
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"helmet": "^4.1.1", "helmet": "^4.1.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"mysql": "^2.18.1", "mysql2": "^2.2.5",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"pug": "^3.0.0" "pug": "^3.0.0"
}, },
......
import { getPoolConnection as db, dbMSG } from "../db"; import { pool as db, dbMSG } from "../db";
import { serverMSG, statusCode } from "../serverinfo"; import { serverMSG, statusCode } from "../serverinfo";
export const getDo = async (req, res) => { // 각각의 지역 코드 정보를 가져오는
const query = "SELECT CODE, DONAME FROM LOCDO"; const getQueryResult = async (query) => {
let isError = false;
let result;
try {
const [row, fields] = await db.execute(query);
result = row;
db((connErr, connection) => {
if (connErr) {
console.log(dbMSG.connection_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(dbMSG.query_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
console.log(dbMSG.query_success); console.log(dbMSG.query_success);
res.status(statusCode.ok).json({ info: result }); } catch (error) {
//Error Log
console.log("", error);
isError = true;
// 발생한 오류가 DB와 연결 오류인지 확인 후 Error Message 지정 및 전달
if (error.code === "ECONNREFUSED") result = dbMSG.connection_err;
else result = dbMSG.query_err;
} }
});
connection.release(); return [isError, result];
};
// Do Code에 대한 GET 요청 처리
export const getDo = async (req, res) => {
const query = "SELECT CODE, DONAME FROM LOCDO";
const [isError, result] = await getQueryResult(query);
if (!isError) {
res.status(statusCode.ok).json({ DO: result });
} else {
console.log(result);
res.status(statusCode.err).send(serverMSG.server_err);
} }
});
}; };
export const getSGG = (req, res) => { // SGG Code에 대한 GET 요청 처리
export const getSGG = async (req, res) => {
const { const {
params: { id }, params: { id },
} = req; } = req;
const query = `SELECT CODE, SGGNAME FROM LOCSIGUNGU WHERE DOCODE = ${id}`; const query = `SELECT CODE, SGGNAME FROM LOCSIGUNGU WHERE DOCODE = ${id}`;
db((connErr, connection) => { const [isError, result] = await getQueryResult(query);
if (connErr) {
console.log(dbMSG.connection_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(dbMSG.query_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
console.log(dbMSG.query_success);
res.status(statusCode.ok).json({ info: result });
}
});
connection.release(); if (!isError) {
res.status(statusCode.ok).json({ DO: id, SGG: result });
} else {
console.log(result);
res.status(statusCode.err).send(serverMSG.server_err);
} }
});
}; };
export const getEMD = (req, res) => { // EMD Code에 대한 GET 요청 처리
export const getEMD = async (req, res) => {
const { const {
params: { id }, params: { id },
} = req; } = req;
const query = `SELECT CODE, EMDNAME FROM LOCINFO WHERE SGGCODE = ${id}`; const query = `SELECT CODE, EMDNAME FROM LOCINFO WHERE SGGCODE = ${id}`;
db((connErr, connection) => { const [isError, result] = await getQueryResult(query);
if (connErr) {
console.log(dbMSG.connection_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(dbMSG.query_err);
res.status(statusCode.err).json({ error: serverMSG.server_err });
} else {
console.log(dbMSG.query_success);
res.status(statusCode.ok).json({ info: result });
}
});
connection.release(); if (!isError) {
res.status(statusCode.ok).json({ SGG: id, EMD: result });
} else {
console.log(result);
res.status(statusCode.err).send(serverMSG.server_err);
} }
});
}; };
import mysql from "mysql"; import mysql from "mysql2/promise";
import dotenv from "dotenv"; import dotenv from "dotenv";
dotenv.config(); dotenv.config();
...@@ -12,20 +12,12 @@ const db_config = { ...@@ -12,20 +12,12 @@ const db_config = {
connectionLimit: 5, connectionLimit: 5,
}; };
// Creation of MySQL Pool // Creation of MySQL Pool, and Export
const pool = mysql.createPool(db_config); export const pool = mysql.createPool(db_config);
// Pool을 이용시, Connection을 생성 후 반환하여 사용
export const getPoolConnection = (callback) => {
console.log("db_connection()");
pool.getConnection(async (err, connection) => {
console.log("db_connection_poolConnection()");
await callback(err, connection);
});
};
// Messages for Data Base.
export const dbMSG = { export const dbMSG = {
connection_err: "DB Connection Error.", connection_err: "DB Connection Error.",
query_err: "DB Query Error.", query_success: "DB Query Success.",
query_success: "Query Success", query_err: "DB Querry Error.",
}; };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment