I am diving into creating my very own Rest API using Nestjs and Prisma for the first time. This project is a basic representation of an inventory management system, keeping things simple with shelves and boxes to store items. The structure in place has tables in a Postgres DB that define the relationships between shelves and boxes (excluding other models for brevity):
model Shelf {
id String @id @default(uuid())
boxes Box[]
}
model Box {
id String @id @default(uuid())
shelf Shelf @relation(fields:[shelfId], references:[id])
shelfId String
items Item[]
}
I have successfully implemented functionalities like creating, fetching, filtering, and deleting data. However, I am facing a challenge when attempting to retrieve all boxes on a specific shelf, followed by retrieving all items within a particular box.
Despite extensive research through PrismaClient documentation, I haven't been able to find a solution to this issue. My current belief is that the logic should reside in the shelf controller/service, but I'm not entirely sure. At present, I have an endpoint set up in shelves.controller.ts:
@Get(':id/boxes')
@ApiOkResponse({ type: ShelfEntity, isArray: true })
getBoxes(@Param('id') id:string ){
return this.shelvesService.getBoxes(id)
}
The ShelfEntity definition looks like this:
export class ShelfEntity implements Shelf {
@ApiProperty({ required: true })
id: string
}
This endpoint calls the shelves.service.ts file, which queries the Prisma client to fetch boxes filtered by shelfId:
getBoxes(id: string) {
return this.prisma.shelf.findUnique({ where: {id}, select: { boxes:true })
}
Based on the expected response described in Prisma Documentation, I should receive an array of Box objects from this GET request. However, I am encountering a 404 error. Any assistance or guidance provided would be immensely appreciated.