Skip to content
Draft
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
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ services:
depends_on:
db:
condition: service_healthy
restart: on_failure
restart: unless-stopped
ports:
- 3000:3000
db:
image: mysql:latest
restart: on_failure
restart: unless-stopped
ports:
- ${MYSQL_PORT}:${MYSQL_PORT}
expose:
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"prettier:check": "prettier --check ."
"prettier:check": "prettier --check .",
"seed": "bun run prisma/seeds/main.ts"
},
"prisma": {
"seed": "bun seed"
},
"dependencies": {
"@mdx-js/react": "3.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Warnings:
- You are about to drop the column `level` on the `User` table. All the data in the column will be lost.
- Added the required column `game_id` to the `Match` table without a default value. This is not possible if the table is not empty.
- Added the required column `updated_at` to the `Match` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE `Match` DROP FOREIGN KEY `Match_challenger_id_fkey`;

-- DropForeignKey
ALTER TABLE `Match` DROP FOREIGN KEY `Match_defender_id_fkey`;

-- AlterTable
ALTER TABLE `Match` ADD COLUMN `deleted_at` DATETIME(3) NULL,
ADD COLUMN `game_id` INTEGER NOT NULL,
ADD COLUMN `updated_at` DATETIME(3) NOT NULL,
MODIFY `played_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);

-- AlterTable
ALTER TABLE `User` DROP COLUMN `level`;

-- CreateTable
CREATE TABLE `Game` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL,
`deleted_at` DATETIME(3) NULL,
`min_team_size` INTEGER NOT NULL,
`max_team_size` INTEGER NOT NULL,

UNIQUE INDEX `Game_name_key`(`name`),
INDEX `Game_name_idx`(`name`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `UserGameStats` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`user_id` INTEGER NOT NULL,
`game_id` INTEGER NOT NULL,
`level` INTEGER NOT NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NOT NULL,
`deleted_at` DATETIME(3) NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Team` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`game_id` INTEGER NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL,
`deleted_at` DATETIME(3) NULL,

INDEX `Team_name_idx`(`name`),
INDEX `Team_game_id_idx`(`game_id`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `TeamMember` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`team_id` INTEGER NOT NULL,
`user_id` INTEGER NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL,
`deleted_at` DATETIME(3) NULL,

INDEX `TeamMember_team_id_idx`(`team_id`),
INDEX `TeamMember_user_id_idx`(`user_id`),
UNIQUE INDEX `TeamMember_team_id_user_id_key`(`team_id`, `user_id`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Rules` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`game_id` INTEGER NOT NULL,
`path` VARCHAR(255) NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL,
`deleted_at` DATETIME(3) NULL,

INDEX `Rules_game_id_idx`(`game_id`),
UNIQUE INDEX `Rules_game_id_key`(`game_id`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Season` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`game_id` INTEGER NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, make the relationship many-to-many that way you could make a Season that involves multiple games?

`start_date` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`end_date` DATETIME(3) NULL,
`active` BOOLEAN NOT NULL DEFAULT true,
`deleted_at` DATETIME(3) NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL,

INDEX `Season_game_id_idx`(`game_id`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE INDEX `Match_game_id_idx` ON `Match`(`game_id`);

-- AddForeignKey
ALTER TABLE `UserGameStats` ADD CONSTRAINT `UserGameStats_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `UserGameStats` ADD CONSTRAINT `UserGameStats_game_id_fkey` FOREIGN KEY (`game_id`) REFERENCES `Game`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Match` ADD CONSTRAINT `Match_game_id_fkey` FOREIGN KEY (`game_id`) REFERENCES `Game`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Match` ADD CONSTRAINT `Match_challenger_id_fkey` FOREIGN KEY (`challenger_id`) REFERENCES `Team`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Match` ADD CONSTRAINT `Match_defender_id_fkey` FOREIGN KEY (`defender_id`) REFERENCES `Team`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Team` ADD CONSTRAINT `Team_game_id_fkey` FOREIGN KEY (`game_id`) REFERENCES `Game`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `TeamMember` ADD CONSTRAINT `TeamMember_team_id_fkey` FOREIGN KEY (`team_id`) REFERENCES `Team`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `TeamMember` ADD CONSTRAINT `TeamMember_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Rules` ADD CONSTRAINT `Rules_game_id_fkey` FOREIGN KEY (`game_id`) REFERENCES `Game`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Season` ADD CONSTRAINT `Season_game_id_fkey` FOREIGN KEY (`game_id`) REFERENCES `Game`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- RenameIndex
ALTER TABLE `Match` RENAME INDEX `Match_challenger_id_fkey` TO `Match_challenger_id_idx`;

-- RenameIndex
ALTER TABLE `Match` RENAME INDEX `Match_defender_id_fkey` TO `Match_defender_id_idx`;
113 changes: 104 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,115 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
level Int
challenger_matches Match[] @relation("challenger")
defender_matches Match[] @relation("defender")
id Int @id @default(autoincrement())
name String @db.VarChar(255)
game_stats UserGameStats[]
teams TeamMember[]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
teams TeamMember[]
teams Team[]

}

model Game {
id Int @id @default(autoincrement())
name String @unique @db.VarChar(255)
matches Match[]
seasons Season[]
teams Team[]
rules Rules?
user_stats UserGameStats[]
created_at DateTime @default(now()) @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)
min_team_size Int
max_team_size Int

@@index([name])
}

model UserGameStats {
id Int @id @default(autoincrement())
user_id Int
user User @relation(fields: [user_id], references: [id])
game_id Int
game Game @relation(fields: [game_id], references: [id])
level Int
created_at DateTime @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)
}

model Match {
id Int @id @default(autoincrement())
challenger User @relation("challenger", fields: [challenger_id], references: [id])
defender User @relation("defender", fields: [defender_id], references: [id])
id Int @id @default(autoincrement())
game_id Int
game Game @relation(fields: [game_id], references: [id])
challenger_id Int
challenger Team @relation("challenger", fields: [challenger_id], references: [id])
defender_id Int
defender Team @relation("defender", fields: [defender_id], references: [id])
Comment on lines 56 to +59
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a MatchParticipants model, so you can create a 2v2

score_challenger Int
score_defender Int
played_at DateTime
played_at DateTime @default(now())
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)

@@index([game_id])
@@index([challenger_id])
@@index([defender_id])
}

model Team {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
members TeamMember[]
matches_as_challenger Match[] @relation("challenger")
matches_as_defender Match[] @relation("defender")
game_id Int
game Game @relation(fields: [game_id], references: [id])
created_at DateTime @default(now()) @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)

@@index([name])
@@index([game_id])
}

model TeamMember {
id Int @id @default(autoincrement())
team_id Int
team Team @relation(fields: [team_id], references: [id])
user_id Int
user User @relation(fields: [user_id], references: [id])
created_at DateTime @default(now()) @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)

@@unique([team_id, user_id])
@@index([team_id])
@@index([user_id])
}

model Rules {
id Int @id @default(autoincrement())
game_id Int
game Game @relation(fields: [game_id], references: [id])
path String @db.VarChar(255)
created_at DateTime @default(now()) @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)
deleted_at DateTime? @db.DateTime(3)

@@unique([game_id])
@@index([game_id])
}

model Season {
id Int @id @default(autoincrement())
name String
game_id Int
game Game @relation(fields: [game_id], references: [id])
start_date DateTime @default(now()) @db.DateTime(3)
end_date DateTime? @db.DateTime(3)
active Boolean @default(true)
deleted_at DateTime? @db.DateTime(3)
created_at DateTime @default(now()) @db.DateTime(3)
updated_at DateTime @updatedAt @db.DateTime(3)

@@index([game_id])
}
38 changes: 38 additions & 0 deletions prisma/seeds/games.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Game } from '@prisma/client';
import { PrismaClient } from '@prisma/client/extension';

export function seedGames(prisma: PrismaClient): Record<string, Promise<Game>> {
return {
foosball: prisma.game.create({
data: {
name: 'Foosball',
min_team_size: 1,
max_team_size: 2,
seasons: {
create: {
name: `Foosball Season 1`,
start_date: new Date().toISOString(),
end_date: new Date(
new Date().setMonth(new Date().getMonth() + 3),
).toISOString(),
active: true,
},
},
},
}),
fifa: prisma.game.create({
data: {
name: 'FIFA',
min_team_size: 1,
max_team_size: 2,
seasons: {
create: {
name: `FIFA Season 1`,
start_date: new Date().toISOString(),
active: true,
},
},
},
}),
};
}
27 changes: 27 additions & 0 deletions prisma/seeds/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PrismaClient } from '@prisma/client';
import { seedGames } from './games';

async function main() {
const prisma = new PrismaClient();

try {
console.log('🧼 Cleaning up...');

await prisma.$transaction([
prisma.season.deleteMany(),
prisma.game.deleteMany(),
]);

console.log('🌱 Seeding games...');
await Promise.all(Object.values(seedGames(prisma)));

console.log('✅ Seeding complete!');
} catch (error) {
console.error('❌ Error seeding data:', error);
throw error;
} finally {
await prisma.$disconnect();
}
}

main().catch(() => process.exit(1));
Loading