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

feat: Prismaのスキーマ定義とマイグレーションの用意 #363

Merged
merged 2 commits into from
Oct 1, 2024
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
2 changes: 2 additions & 0 deletions packages/kcms/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ dist

build/
data.json
*.db
*.db-journal
6 changes: 4 additions & 2 deletions packages/kcms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
"@hono/zod-openapi": "^0.16.0",
"@hono/zod-validator": "^0.3.0",
"@mikuroxina/mini-fn": "^6.0.0",
"@prisma/client": "5.20.0",
"config": "workspace:^",
"esbuild": "^0.24.0",
"hono": "^4.0.0",
"typescript": "^5.3.2",
"zod": "^3.22.4",
"config": "workspace:^"
"zod": "^3.22.4"
},
"devDependencies": {
"@eslint/eslintrc": "^3.0.0",
Expand All @@ -39,6 +40,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-vitest": "^0.5.0",
"prettier": "^3.1.0",
"prisma": "^5.20.0",
"tsx": "^4.6.2",
"vitest": "^2.0.0"
}
Expand Down
46 changes: 46 additions & 0 deletions packages/kcms/prisma/migrations/20241001112809_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- CreateTable
CREATE TABLE "Team" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"robot_type" TEXT NOT NULL,
"department" TEXT NOT NULL,
"club_name" TEXT,
"is_entered" BOOLEAN NOT NULL
);

-- CreateTable
CREATE TABLE "main_match" (
"id" TEXT NOT NULL PRIMARY KEY,
"course_index" INTEGER NOT NULL,
"match_index" INTEGER NOT NULL,
"left_team_id" TEXT,
"right_team_id" TEXT,
"winner_team_id" TEXT,
CONSTRAINT "main_match_left_team_id_fkey" FOREIGN KEY ("left_team_id") REFERENCES "Team" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "main_match_right_team_id_fkey" FOREIGN KEY ("right_team_id") REFERENCES "Team" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "pre_match" (
"id" TEXT NOT NULL PRIMARY KEY,
"course_index" INTEGER NOT NULL,
"match_index" INTEGER NOT NULL,
"left_team_id" TEXT,
"right_team_id" TEXT,
CONSTRAINT "pre_match_left_team_id_fkey" FOREIGN KEY ("left_team_id") REFERENCES "Team" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "pre_match_right_team_id_fkey" FOREIGN KEY ("right_team_id") REFERENCES "Team" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "run_result" (
"id" TEXT NOT NULL PRIMARY KEY,
"points" INTEGER NOT NULL,
"goal_time_seconds" INTEGER NOT NULL,
"finish_state" INTEGER NOT NULL,
"team_id" TEXT NOT NULL,
"main_match_id" TEXT,
"pre_match_id" TEXT,
CONSTRAINT "run_result_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "Team" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "run_result_main_match_id_fkey" FOREIGN KEY ("main_match_id") REFERENCES "main_match" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "run_result_pre_match_id_fkey" FOREIGN KEY ("pre_match_id") REFERENCES "pre_match" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
3 changes: 3 additions & 0 deletions packages/kcms/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
78 changes: 78 additions & 0 deletions packages/kcms/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = "file:./kcmsx.db"
}

model Team {
id String @id
name String
robotType String @map("robot_type")
department String
clubName String? @map("club_name")
isEntered Boolean @map("is_entered")

runResult RunResult[]

mainLeftTeams MainMatch[] @relation("main_left_team")
mainRightTeams MainMatch[] @relation("main_right_team")
preLeftTeams PreMatch[] @relation("pre_left_team")
preRightTeams PreMatch[] @relation("pre_right_team")
}

model MainMatch {
id String @id
courseIndex Int @map("course_index")
matchIndex Int @map("match_index")
leftTeamId String? @map("left_team_id")
leftTeam Team? @relation("main_left_team", fields: [leftTeamId], references: [id])
rightTeamId String? @map("right_team_id")
rightTeam Team? @relation("main_right_team", fields: [rightTeamId], references: [id])
// NOTE: アプリケーション側を信用するのでここはリレーションを作らないことにした (PreMatchも同様)
winnerTeamId String? @map("winner_team_id")

runResult RunResult[]

@@map("main_match")
}

model PreMatch {
id String @id
courseIndex Int @map("course_index")
matchIndex Int @map("match_index")

leftTeamID String? @map("left_team_id")
leftTeam Team? @relation("pre_left_team", fields: [leftTeamID], references: [id])
rightTeamID String? @map("right_team_id")
rightTeam Team? @relation("pre_right_team", fields: [rightTeamID], references: [id])

runResult RunResult[]

@@map("pre_match")
}

model RunResult {
id String @id
points Int
goalTimeSeconds Int @map("goal_time_seconds")
finishState Int @map("finish_state")

team Team @relation(fields: [teamID], references: [id])
teamID String @map("team_id")

mainMatch MainMatch? @relation(fields: [mainMatchId], references: [id])
mainMatchId String? @map("main_match_id")
preMatch PreMatch? @relation(fields: [preMatchId], references: [id])
preMatchId String? @map("pre_match_id")

@@map("run_result")
}
6 changes: 3 additions & 3 deletions packages/kcms/src/team/models/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Team {
private readonly members: Array<string>;
private readonly isMultiWalk: boolean;
private readonly category: Department;
private readonly depatmentType: DepartmentType;
private readonly departmentType: DepartmentType;
private readonly clubName?: string;
private isEntered: boolean;

Expand All @@ -56,7 +56,7 @@ export class Team {
this.category = category;
this.clubName = clubName;
this.isEntered = isEntered;
this.depatmentType = departmentType;
this.departmentType = departmentType;
}

getId(): TeamID {
Expand All @@ -83,7 +83,7 @@ export class Team {
}

getDepartmentType(): DepartmentType {
return this.depatmentType;
return this.departmentType;
}

getClubName(): string | undefined {
Expand Down
68 changes: 67 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.