Hi guys. Great work everyone at the prisma communi...
# orm-help
r
Hi guys. Great work everyone at the prisma community. Love working with it! I am a frontend developer who want to learn data-modeling, with relational dbs. Now i am working on a learning project and this is my use case for a table tennis schema. We are playing table tennis on work and have different Leagues ( = A league is just a team of people playing each other). The question is: 1. How can I save one Game with two players and their score? 2. Feedback is welcome on my schema! This is the schema:
Copy code
model User {
  id            String   @id
  email         String   @unique
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt

  profile Profile?
}

model Profile {
  id            String           @id @default(cuid())
  user          User?            @relation(fields: [userId], references: [id])
  userId        String?
  playedGames   PlayerInGame[]
  joinedLeagues PlayerInLeague[]
}

model League {
  id      String           @id @default(cuid())
  name    String           @unique
  games   Game[]
  players PlayerInLeague[]
}

model PlayerInLeague {
  id        String  @id @default(cuid())
  league    League  @relation(fields: [leagueId], references: [id])
  player    Profile @relation(fields: [profileId], references: [id])
  points    Int
  leagueId  String
  profileId String
}

model Game {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  homePoints Int
  awayPoints Int
  league           League       @relation(fields: [leagueId], references: [id])
  leagueId         String

  // HERE I NEED TO KEEP TRACK OF PLAYERS and their statistics. Who won, how many point etc. 
}

model PlayerInGame {
  id        String  @id @default(cuid())
  game      Game    @relation(fields: [gameId], references: [id])
  player    Profile @relation(fields: [profileId], references: [id])
  gameId    Int
  profileId String  @unique
}
r
Hey @Roy 👋 One way to go about that would be something like:
Copy code
model User {
  id            String           @id
  email         String           @unique
  playedGames   Game[]
  joinedLeagues PlayerInLeague[]
  gameStats     GameStats[]
  createdAt     DateTime         @default(now())
  updatedAt     DateTime         @updatedAt
}

model League {
  id      String           @id @default(cuid())
  name    String           @unique
  games   Game[]
  players PlayerInLeague[]
}

model PlayerInLeague {
  id        String @id @default(cuid())
  league    League @relation(fields: [leagueId], references: [id])
  player    User   @relation(fields: [profileId], references: [id])
  points    Int
  leagueId  String
  profileId String
}

model Game {
  id         Int         @id @default(autoincrement())
  createdAt  DateTime    @default(now())
  homePoints Int
  awayPoints Int
  league     League      @relation(fields: [leagueId], references: [id])
  leagueId   String
  // HERE I NEED TO KEEP TRACK OF PLAYERS and their statistics. Who won, how many point etc.
  gameStats  GameStats[]
  players    User[]
}

model GameStats {
  id       Int     @id @default(autoincrement())
  games    Game?   @relation(fields: [gameId], references: [id])
  user     User?   @relation(fields: [userId], references: [id])
  points   Int
  isWinner Boolean
  gameId   Int?
  userId   String?
}
Let me know if this make sense. I have also simplified your model wherever required 🙂
r
Thanks for you help! I will have a look!
💯 1