Anton Reindl
12/01/2021, 2:31 PMareas and epics. An area has many epics and they are related through an areaId on epics. I have a controller method area/<area-uid>/epics in my API.
What’s the best way to get all epics for a given area ?
I tried two ways:
1. Two function calls
const area = await this.findOne(uid)
return this.prisma.epic.findMany({ where: { area } })
--> this creates two independent SQL quereies
2. One function call
return (await this.prisma.area.findUnique({ where: { uid }, select: { epics: true } }))['epics']
Works, but feels quirky.
---
Is there a better solution? Like a simple right join?
Thanks for feedback. I am sure this is a rookie thing.
Cheers
AntonMaciek K
12/01/2021, 2:58 PMprisma.epic.findMany({
where: {
areaId: uid
}
});nikolasburk
const epicsForArea = await prisma.epic.findMany({
where: {
areaId: uid
}
})nikolasburk
Maciek K
12/01/2021, 2:59 PMnikolasburk
model Area {
id String @id @default(uuid())
label String?
description String?
// ... more fields
epics Epic[]
}
model Epic {
id String @id @default(uuid())
title String?
startDate DateTime?
endDate DateTime?
// ... more fields
areaId String
area Area @relation(fields: [areaId], references: [id])
}Anton Reindl
12/01/2021, 4:01 PMnikolasburk
Anton Reindl
12/01/2021, 5:31 PMmodel Epic {
id Int @id @default(autoincrement())
uid String @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
cid String? @unique
title String @db.VarChar
description String?
deadline DateTime @db.Date
outcome String? @db.Text
details String? @db.Text
successMeasurement String? @db.Text
impact String? @db.Text
scoreDescription String? @db.Text
tenantId String @db.VarChar
kanban Kanban @relation(fields: [kanbanId], references: [id])
kanbanId Int
type EpicType? @relation(fields: [typeId], references: [id])
typeId Int?
owner User? @relation(fields: [ownerId], references: [id])
ownerId Int?
team UsersOnEpics[]
area Area @relation(fields: [areaId], references: [id])
areaId Int
}
Area:
model Area {
id Int @id @default(autoincrement())
uid String @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
cid String? @unique
title String @db.VarChar
description String?
leitmotif String? @db.VarChar
type AreaTypeEnum @default(company)
tenantId String @db.VarChar
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
createdBy String? @db.VarChar
owner User? @relation(fields: [ownerId], references: [id])
ownerId Int?
epics Epic[]
@@index([id, uid, cid, tenantId])
}nikolasburk
uid then you should be able to adjust the query that Maciek and I shared like so:
const epicsForArea = await prisma.epic.findMany({
where: {
uid: uid
}
})
Does this work now? 🙂Anton Reindl
12/02/2021, 9:56 AMnikolasburk