MySQL.js
1.97 KB
1
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
const mysql = require("mysql");
const mysql2 = require("mysql2/promise");
const fs = require("fs");
const parseICal = require("./ICal");
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connectOption = {
host: "localhost",
user: id,
password: pw,
database: "db",
};
async function jcalToSQL(jcal, userID) {
// const fsql = fs.readFileSync("server/libs/sql.pvdata", "utf8");
// const [id, pw] = fsql.split("\r\n");
const connection = await mysql2.createConnection(connectOption);
await connection.connect();
const commonCols = [
"userID",
"uid",
"label",
"subjectID",
"type",
"description",
"url",
"detail",
];
const dateScheCols = [...commonCols, "date"];
const timeScheCols = [...commonCols, "date", "startTime", "endTime"];
const dateQueryString =
"INSERT IGNORE INTO schedules_date (" +
dateScheCols.join(",") +
") VALUES ?";
const timeQueryString =
"INSERT IGNORE INTO schedules_time (" +
timeScheCols.join(",") +
") VALUES ?";
const dateSchedules = [];
const timeSchedules = [];
for (const sche of jcal) {
sche.userID = userID;
sche.type = "assignment";
if (sche.scheType === "date")
dateSchedules.push(dateScheCols.map((col) => sche[col]));
else if (sche.scheType === "time")
timeSchedules.push(timeScheCols.map((col) => sche[col]));
else console.log("unexpected scheType", sche);
}
let result = await connection.query(dateQueryString, [dateSchedules]);
console.log(`schedules_date | ${result}`);
result = await connection.query(timeQueryString, [timeSchedules]);
console.log(`schedules_time | ${result}`);
connection.end();
}
// iCal의 소유주를 DB에 등록 후 userID get
// subjectID가 subjects, userID가 users에 있어야함
// const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
// const jcal = parseICal(fdata);
// jcalToSQL(jcal, 1);
module.exports = { jcalToSQL, connectOption };