How to model my Prisma for a Golf-application

I have this golf application where all Users have their own bags. Inside their bags they have several Golf clubs. I want to fetch their bags and also list out all the golf clubs inside their respective bags.
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
role Role @default(USER)
bags Bag[]
}

model Bag {
id String @id @default(cuid())
name String
description String?
clubs GolfClub[]
active Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String

@@index([userId])
}

model GolfClub {
id String @id @default(cuid())
name String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
bag Bag[]
bagId String?

@@index([bagId])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
role Role @default(USER)
bags Bag[]
}

model Bag {
id String @id @default(cuid())
name String
description String?
clubs GolfClub[]
active Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String

@@index([userId])
}

model GolfClub {
id String @id @default(cuid())
name String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
bag Bag[]
bagId String?

@@index([bagId])
}
How could I achieve this? I guess I can't look inside the bag and see what clubs there is inside there. That instead, I'll have to look at the clubs and see if their Id's is inside a bag. like
getAllClubsInBag: publicProcedure
.input(z.object({ bagId: z.array(z.string()) }))
.query(({ ctx, input }) => {
return ctx.prisma.disc.findMany({
where: {
bagId: input.bagId,
},
});
}),
getAllClubsInBag: publicProcedure
.input(z.object({ bagId: z.array(z.string()) }))
.query(({ ctx, input }) => {
return ctx.prisma.disc.findMany({
where: {
bagId: input.bagId,
},
});
}),
But since the bagId is just a string. I can't like map over each bag and use their id against this procedure. Cause in my database, the bagId is null on the discs. And also just a singular string.
Solution:
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Jump to solution
2 Replies
Solution
cje
cje•2y ago
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
fjzon
fjzon•2y ago
Oh shit! Didn't even know you could query relations. Thought you had to do some reverse stuff to get the respective model. Thanks... 😅
Want results from more Discord servers?
Add your server