Commit bda6ebe2 authored by KangMin An's avatar KangMin An
Browse files

Update : 지역 코드와 이름 정보 서버 시작시 입력 진행.

parent 27a97784
import fs from "fs"; import fs from "fs";
// import mysql from "mysql"; import db from "./index";
import Sequelize from "sequelize";
import dotenv from "dotenv"; export const setLocTables = () => {
// File Read
dotenv.config(); let originData = fs.readFileSync("data/admAddressCode.csv", "utf8");
const envs = process.env;
// Separate Data & Input Data
const sequelize = new Sequelize( let sepData = originData.split("\r\n");
envs.DB_DATABASE, let doeCodeSet = new Set();
envs.DB_USER, let sggCodeSet = new Set();
envs.DB_PASSWORD,
{ let doeList = [];
host: envs.DB_HOST, let sggList = [];
dialect: "postgres", let emdList = [];
console.log("Start Location data insertion...");
sepData.forEach(async (line) => {
line = line.replace(/\s/g, "");
let addr = line.split(",");
// Get Local Codes and Names
const doeCode = Number(addr[0]);
let doeName = addr[1];
doeName = doeName.replace(/\s/g, "");
const sggCode = Number(addr[2]);
let sggName = addr[3];
sggName = sggName.replace(/\s/g, "");
const emdCode = Number(addr[4]);
let emdName = addr[5];
emdName = emdName.replace(/\s/g, "");
// Save Loc Info to array.
if (!doeCodeSet.has(doeCode)) {
doeCodeSet.add(doeCode);
doeList.push({ code_doe: doeCode, name_doe: doeName });
} }
);
if (!sggCodeSet.has(sggCode)) {
const checkCONN = async () => { sggCodeSet.add(sggCode);
try { sggList.push({
await sequelize.authenticate(); code_sgg: sggCode,
console.log("Connection has been established successfully."); name_sgg: sggName,
sequelize.close(); code_doe: doeCode,
} catch (error) { });
console.error("Unable to connect to the database:", error);
} }
emdList.push({
code_emd: emdCode,
name_emd: emdName,
code_doe: doeCode,
code_sgg: sggCode,
});
});
console.log("Inserting Location Data...");
// Insert to DB.
doeList.map(async (node) => {
await db.Doe.create(node, { logging: false });
});
sggList.map(async (node) => {
await db.Sgg.create(node, { logging: false });
});
emdList.map(async (node) => {
await db.Emd.create(node, { logging: false });
});
console.log("Finish the insertion!");
}; };
checkCONN(); export default setLocTables;
// // DB Connection
// const db = mysql.createConnection({
// host: process.env.MYSQL_HOST || "localhost",
// user: process.env.MYSQL_USER || "root",
// password: process.env.MYSQL_PASSWORD,
// database: process.env.MYSQL_DATABASE || "EUE",
// });
// const inputDo = (code, name) => {
// name = name.replace(/\s/g, "");
// let q = `INSERT INTO LOCDO (CODE,DONAME) VALUES (${code},'${name}');`;
// db.query(q, (err, result) => {
// if (err) {
// console.log(err);
// return;
// }
// console.log("Result : " + result);
// });
// };
// const inputSi = (code, name, doCode) => {
// name = name.replace(/\s/g, "");
// let q = `INSERT INTO LOCSIGUNGU (CODE,DOCODE,SGGNAME) VALUES (${code},${doCode},'${name}');`;
// db.query(q, (err, result) => {
// if (err) {
// console.log(err);
// return;
// }
// console.log("Result : " + result);
// });
// };
// const inputDong = (code, name, doCode, siCode) => {
// name = name.replace(/\s/g, "");
// let q = `INSERT INTO LOCINFO (CODE,DOCODE,SGGCODE,EMDNAME) VALUES (${code},${doCode},${siCode},'${name}');`;
// db.query(q, (err, result) => {
// if (err) {
// console.log(err);
// return;
// }
// console.log("Result : " + result);
// });
// };
// const setDB = () => {
// // DB Connect
// db.connect((err) => {
// if (err) {
// console.log(err);
// return;
// }
// console.log("connected as id" + db.threadId);
// });
// // File Read
// let originData = fs.readFileSync("data/admAddressCode.csv", "utf8");
// // Separate Data & Input Data
// let sepData = originData.split("\r\n");
// let setDoCode = new Set();
// let setSiCode = new Set();
// sepData.forEach((line) => {
// line = line.replace(/\s/g, "");
// let addr = line.split(",");
// const doCode = Number(addr[0]);
// if (!setDoCode.has(doCode)) {
// const doName = addr[1];
// inputDo(doCode, doName);
// setDoCode.add(doCode);
// }
// const siCode = Number(addr[2]);
// if (!setSiCode.has(siCode)) {
// const siName = addr[3];
// inputSi(siCode, siName, doCode);
// setSiCode.add(siCode);
// }
// const dongCode = Number(addr[4]);
// const dongName = addr[5];
// inputDong(dongCode, dongName, doCode, siCode);
// });
// // Connection Close
// db.end();
// };
// setDB();
...@@ -2,6 +2,7 @@ import app from "./app"; ...@@ -2,6 +2,7 @@ import app from "./app";
import dotenv from "dotenv"; import dotenv from "dotenv";
import "./schedules"; // 매일 자정 데이터 처리 import "./schedules"; // 매일 자정 데이터 처리
import db from "./db/index"; import db from "./db/index";
import setLocTables from "./db/locationSetting";
dotenv.config(); dotenv.config();
...@@ -17,6 +18,7 @@ db.sequelize ...@@ -17,6 +18,7 @@ db.sequelize
.then(() => { .then(() => {
console.log(db.msg.connection_success); console.log(db.msg.connection_success);
}) })
.then(() => setLocTables())
.catch((err) => { .catch((err) => {
console.log(db.msg.connection_err); console.log(db.msg.connection_err);
console.log(err); console.log(err);
......
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