sal.tor
sal.tor
TTCTheo's Typesafe Cult
Created by sal.tor on 3/5/2023 in #questions
Prisma: best way to structure these D&D schemas?
Context: I'm using tRPC, Prisma, and MongoDB I have a few models like Campaign, Player, NPC, and Battle Campaigns have many Player/NPC/Battle:
model Campaign {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
battles Battle[]
npcs Npc[]
players Player[]
}

model Npc {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
name String @default("Please name me")
forStory Boolean @default(false)
known Boolean @default(false)
}

model Player {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
name String @default("Please name me")
maxHp Int @default(5)
passiveWisdom Int @default(10)
}

model Battle {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
title String @default("Please name me")
}
model Campaign {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
battles Battle[]
npcs Npc[]
players Player[]
}

model Npc {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
name String @default("Please name me")
forStory Boolean @default(false)
known Boolean @default(false)
}

model Player {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
name String @default("Please name me")
maxHp Int @default(5)
passiveWisdom Int @default(10)
}

model Battle {
id String @id @default(auto()) @map("_id") @db.ObjectId
campaignId String @db.ObjectId
campaign Campaign @relation(fields: [campaignId], references: [id])
title String @default("Please name me")
}
Battles are meant to have many Player and many NPC But since there’s an order to battle I’m thinking I should create something like a BattleParticipant model which holds an order value and which entity it’s in regard to Each participant can have an entityType (a string value) so i know whether it’s a Player or NPC, and entityId so I can properly refer to it
model BattleParticipant {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String @default("Please name me")
order Int @default(0)
entityType String
entityId String @db.ObjectId
}
model BattleParticipant {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String @default("Please name me")
order Int @default(0)
entityType String
entityId String @db.ObjectId
}
I'll post my desired outcome and alternative I've thought of in a comment
8 replies