Skip to content

Commit 9cfdf50

Browse files
committed
feat: json transformer
1 parent cc765db commit 9cfdf50

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"**/*.svg",
5555
"**/migration.sql",
5656
"**/data.json",
57+
"**/server/src/seeds/json/**",
5758
"**/Cargo.*",
5859
"scraper/target",
5960
"**/rust-toolchain.toml",

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"bun.lockb",
2323
"server/target",
2424
"data.json",
25+
"server/src/seeds/json",
2526
"scraper/target",
2627
".next",
2728
"next-env.d.ts",

server/src/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/common
2-
/seeds/data.json
2+
/seeds/json/*
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
3+
4+
import { prisma } from "../database/client";
5+
6+
// 後期 (scraper) 形式のデータを読み込む。
7+
const FILE_PATH = path.join(__dirname, "data.json");
8+
9+
// sample
10+
// [
11+
// {
12+
// name: "zenki",
13+
// courses: [
14+
// {
15+
// name: "数理科学基礎",
16+
// teacher: "(人名)",
17+
// semester: "S1,S2",
18+
// period: "月曜2限、水曜1限",
19+
// code: "30003 CAS-FC1871L1",
20+
// },
21+
// ],
22+
// },
23+
// ];
24+
25+
async function main() {
26+
const jsonData: {
27+
courses: {
28+
name: string;
29+
teacher: string;
30+
semester: string;
31+
period: string;
32+
code: string;
33+
}[];
34+
}[] = JSON.parse(fs.readFileSync(FILE_PATH, "utf-8"));
35+
console.log(jsonData);
36+
37+
const coursesData = jsonData[0].courses
38+
.filter((course) => course.semester.split("")[0] === "S")
39+
.map((course) => {
40+
const { code, name, teacher } = course;
41+
return {
42+
id: code.split(" ")[0],
43+
name: name,
44+
teacher: teacher,
45+
};
46+
});
47+
48+
await prisma.course.createMany({
49+
data: coursesData,
50+
});
51+
52+
const slotsData: {
53+
day: "mon" | "tue" | "wed" | "thu" | "fri" | "sat" | "sun" | "other";
54+
period: number;
55+
courseId: string;
56+
}[] = [];
57+
58+
for (const courseData of jsonData[0].courses) {
59+
const { code, period } = courseData;
60+
61+
if (courseData.semester.split("")[0] !== "S") continue;
62+
63+
for (const p of period.split("、")) {
64+
const [dayJp, periodStr] = p.split("曜");
65+
const day =
66+
dayJp === "月"
67+
? "mon"
68+
: dayJp === "火"
69+
? "tue"
70+
: dayJp === "水"
71+
? "wed"
72+
: dayJp === "木"
73+
? "thu"
74+
: dayJp === "金"
75+
? "fri"
76+
: dayJp === "土"
77+
? "sat"
78+
: dayJp === "日"
79+
? "sun"
80+
: "other";
81+
82+
slotsData.push({
83+
day,
84+
period: Number.parseInt(periodStr?.split("")[0]) || 0,
85+
courseId: code.split(" ")[0],
86+
});
87+
}
88+
}
89+
90+
await prisma.slot.createMany({
91+
data: slotsData,
92+
skipDuplicates: true,
93+
});
94+
95+
console.log("Data inserted successfully!");
96+
}
97+
98+
main()
99+
.then(async () => {
100+
await prisma.$disconnect();
101+
})
102+
.catch(async (e) => {
103+
console.error(e);
104+
await prisma.$disconnect();
105+
process.exit(1);
106+
});

0 commit comments

Comments
 (0)