Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/#141: 데이터베이스 테이블 생성 시 ERD 에 명시된 테이블데로 생성하도록 수정 #144

Merged
merged 4 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed src/db/data/handler.ts
Empty file.
2 changes: 1 addition & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import env from '@config';
import mysql from 'mysql2';
import mysql from 'mysql2/promise';

let DBHost;

Expand Down
205 changes: 95 additions & 110 deletions src/db/table/createTables.ts
Original file line number Diff line number Diff line change
@@ -1,150 +1,135 @@
import { College } from 'src/@types/college';
import db from 'src/db';

const createDepartmentTable = () => {
const createDepartmentTable = async () => {
const createTableQuery = `CREATE TABLE departments (
id INT PRIMARY KEY AUTO_INCREMENT,
collegeName VARCHAR(255) NOT NULL,
departmentName VARCHAR(255) NOT NULL,
departmentSubName VARCHAR(255) NOT NULL,
departmentLink VARCHAR(255) NOT NULL
college_name VARCHAR(50) NOT NULL,
department_name VARCHAR(100) NOT NULL,
department_subname VARCHAR(100) NOT NULL,
department_link VARCHAR(255) NOT NULL,
graduation_link VARCHAR(255)
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('학과 테이블 생성 실패', error);
} else {
console.log('학과 테이블 생성 성공!');
}
});
};

const createGraduationTable = () => {
const createGraduationQuery = `CREATE TABLE graduation (
id INT PRIMARY KEY AUTO_INCREMENT,
department VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL
);`;

db.query(createGraduationQuery, (error) => {
if (error) {
console.log('졸업요건 테이블 생성 실패', error);
} else {
console.log('졸업요건 테이블 생성 성공!');
}
});
try {
await db.execute(createTableQuery);
console.log('학과 테이블 생성 성공!');
} catch (error) {
console.log('학과 테이블 생성 실패', error);
}
};

const createNoticeTable = (college: College[]) => {
for (const data of college) {
const major =
data.departmentSubName !== '-'
? data.departmentSubName
: data.departmentName;
for (const tableName of [`${major}고정`, `${major}일반`]) {
const createTableQuery = `CREATE TABLE ${tableName} (
id INT PRIMARY KEY AUTO_INCREMENT,
const createMajorNoticeTable = async () => {
const createTableQuery = `CREATE TABLE major_notices (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
uploadDate VARCHAR(255) NOT NULL
upload_date VARCHAR(20) NOT NULL,
rep_yn BOOLEAN,
department_id INT NOT NULL,
FOREIGN KEY (department_id) REFERENCES departments(id)
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('테이블 생성 실패', error);
} else {
console.log('학과공지 테이블 생성 성공!');
}
});
}
try {
await db.execute(createTableQuery);
console.log('학과 공지사항 테이블 생성 성공!');
} catch (error) {
console.log('학과 공지사항 테이블 생성 실패', error);
}
};

const createSubscribeTable = (college: College[]) => {
for (const data of college) {
const major =
data.departmentSubName !== '-'
? data.departmentSubName
: data.departmentName;
const createNoticeTable = async () => {
const createTableQuery = `CREATE TABLE notices (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
uploadDate VARCHAR(20) NOT NULL,
author VARCHAR(50) NOT NULL,
rep_yn BOOLEAN,
category ENUM('SCHOOL', 'LANGUAGE') NOT NULL
);`;

try {
await db.execute(createTableQuery);
console.log('공지사항 테이블 생성 성공!');
} catch (error) {
console.log('공지사항 테이블 생성 실패', error);
}
};

const tableName = `${major}구독`;
const createTableQuery = `CREATE TABLE ${tableName} (
id INT PRIMARY KEY AUTO_INCREMENT,
user VARCHAR(600) NOT NULL UNIQUE
const createSubscribeTable = async () => {
const createTableQuery = `CREATE TABLE subscribe_users (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user VARCHAR(600) NOT NULL UNIQUE,
department_id INT NOT NULL,
FOREIGN KEY (department_id) REFERENCES departments(id)
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('테이블 생성 실패', error);
} else {
console.log('학과구독 테이블 생성 성공!');
}
});
try {
await db.execute(createTableQuery);
console.log('알림 구독 테이블 생성 성공!');
} catch (error) {
console.log('알림 구독 테이블 생성 실패', error);
}
};

const createSchoolNoticeTable = () => {
for (const tableName of [`학교고정`, `학교일반`]) {
const createTableQuery = `CREATE TABLE ${tableName} (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
uploadDate VARCHAR(255) NOT NULL
);`;
const createSubscribeKeywordTable = async () => {
const createTableQuery = `CREATE TABLE subscribe_keywords (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
keyword VARCHAR(100) NOT NULL UNIQUE,
user_id BIGINT NOT NULL,
FOREIGN KEY (user_id) REFERENCES subscribe_users(id)
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('테이블 생성 실패', error);
} else {
console.log('학교 테이블 생성 성공!');
}
});
try {
await db.execute(createTableQuery);
console.log('알림 구독 키워드 테이블 생성 성공!');
} catch (error) {
console.log('알림 구독 키워드 테이블 생성 실패', error);
}
};

const createWhalebeDataTable = () => {
const createTableQuery = `CREATE TABLE 웨일비 (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL UNIQUE,
date VARCHAR(255) NOT NULL,
imgUrl VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL
const createWhalebeDataTable = async () => {
const createTableQuery = `CREATE TABLE whalebe (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
operating_period VARCHAR(30) NOT NULL,
recruitment_period VARCHAR(30) NOT NULL,
imgurl VARCHAR(500) NOT NULL
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('웨일비 DB 생성 실패', error);
return;
}
try {
await db.execute(createTableQuery);
console.log('웨일비 테이블 생성 성공!');
});
} catch (error) {
console.log('웨일비 테이블 생성 실패', error);
}
};

const createLanguageDataTable = () => {
const createTableQuery = `CREATE TABLE 어학공지 (
id INT PRIMARY KEY AUTO_INCREMENT,
const createRecruitNoticeTable = async () => {
const createTableQuery = `CREATE TABLE recruit_notices (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
uploadDate VARCHAR(255) NOT NULL
upload_date VARCHAR(30) NOT NULL,
recruitment_period VARCHAR(30) NOT NULL
);`;

db.query(createTableQuery, (error) => {
if (error) {
console.log('어학 DB 생성 실패', error);
return;
}
console.log('어학 테이블 생성 성공!');
});
try {
await db.execute(createTableQuery);
console.log('채용 공지 테이블 생성 성공!');
} catch (error) {
console.log('채용 공지 생성 실패', error);
}
};

const createAllTables = (college: College[]) => {
const createAllTables = () => {
createDepartmentTable();
createMajorNoticeTable();
createNoticeTable();
createSubscribeTable();
createSubscribeKeywordTable();
createWhalebeDataTable();
createLanguageDataTable();
createGraduationTable();
createSchoolNoticeTable();
createNoticeTable(college);
createSubscribeTable(college);
createRecruitNoticeTable();
};

export default createAllTables;
30 changes: 13 additions & 17 deletions src/hooks/startCrawlingData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ import { saveGraduationRequirementToDB } from '@db/data/graduation';
import { saveLanguageNoticeToDB } from '@db/data/languageHandler';
import {
saveDepartmentToDB,
saveNoticeToDB,
saveMajorNoticeToDB,
saveSchoolNoticeToDB,
saveWhalebeToDB,
} from '@db/data/noticeHandler';
import db from '@db/index';
import createNoticeTable from '@db/table/createTables';
import { RowDataPacket } from 'mysql2';

export const initialCrawling = () => {
export const initialCrawling = async () => {
try {
const checkInitialQuery = "SHOW TABLES LIKE 'departments';";
db.query(checkInitialQuery, async (err, res) => {
if (err) return;
const rows = res as RowDataPacket[];
console.log(rows.length);
if (rows.length === 0) {
const collegeList = await collegeCrawling();
createNoticeTable(collegeList);
await saveDepartmentToDB(collegeList);
await saveLanguageNoticeToDB();
await saveGraduationRequirementToDB();
await saveSchoolNoticeToDB();
await saveWhalebeToDB();
await saveNoticeToDB();
}
});
const [rows] = await db.execute<RowDataPacket[]>(checkInitialQuery);
if (rows.length > 0) return;

const collegeList = await collegeCrawling();
createNoticeTable();
await saveDepartmentToDB(collegeList);
await saveLanguageNoticeToDB();
await saveGraduationRequirementToDB();
await saveSchoolNoticeToDB();
await saveWhalebeToDB();
await saveMajorNoticeToDB();
} catch (err) {
console.log(err);
}
Expand Down