locationSetting.js 1.73 KB
Newer Older
1
import fs from "fs";
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import db from "./index";

export const setLocTables = () => {
  // File Read
  let originData = fs.readFileSync("data/admAddressCode.csv", "utf8");

  // Separate Data & Input Data
  let sepData = originData.split("\r\n");
  let doeCodeSet = new Set();
  let sggCodeSet = new Set();

  let doeList = [];
  let sggList = [];
  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)) {
      sggCodeSet.add(sggCode);
      sggList.push({
        code_sgg: sggCode,
        name_sgg: sggName,
        code_doe: doeCode,
      });
    }

    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!");
75
76
};

77
export default setLocTables;