I have a Student prisma model (with a relation to ...
# random
d
I have a Student prisma model (with a relation to a studycourse (e.g. ecomonics bachelor) . Now I want to create a DTO for the Student. With TypeORM I used to use a Mapper class to convert from a databaseclass to a dto. With Prisma I'm struggling because from await prisma.student.findFirst() I get the type Student but without the relationship to studycourse. If I include it via await prisma.student.findFirst({include:{ course:true }}); I get the Type Student & { course: StudyCourse | null } Which makes it very tedious to implement a mapper for it. Is there another way to do this?
// This is your Prisma schema file,
// learn more about it in the docs: <https://pris.ly/d/prisma-schema>
datasource db {
provider = "postgresql"
url      = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Department {
id           Int      @default(autoincrement()) @id
name         String   @unique
description  String?
studyCourses StudyCourse[]
lecturers Lecturer[]
}
model StudyCourse {
id        Int      @default(autoincrement()) @id
name      String   @unique
degree    String
department Department @relation(fields: [departmentId],references: [id])
departmentId Int @unique
students Student[]
}
model Student {
id String @id
title String @default("")
firstname String
lastname String
gender Gender
birthdate DateTime
mail String
phone String?
active Boolean
course StudyCourse? @relation(fields: [courseId],references: [id])
courseId Int?
semester Int
matriculationsNumber String
}
model Lecturer {
id String @id
title String @default("")
firstname String
lastname String
gender Gender
birthdate DateTime
mail String
phone String?
active Boolean
department Department @relation(fields: [departmentId],references: [id])
departmentId Int
}
model Administrative {
id String @id
title String @default("")
firstname String
lastname String
gender Gender
birthdate DateTime
mail String
phone String
active Boolean
}
enum Gender {
MALE
FEMALE
DIVERSE
}
For context I have an angular frontend and a (typescript) nodejs backend. Maybe I'm completly on the wrong track.
without nesting I think I could use the prisma types, but I couldn't find a way to use them with nesting on the client side
f
Copy code
course StudyCourse @relation(fields: [courseId],references: [id])
Without the
?
on
Student
model should solve the problem
d
Without ? I get the same type, only that courseId might not be null Without ?
export type Student = {
id: string
title: string
firstname: string
lastname: string
gender: Gender
birthdate: Date
mail: string
phone: string | null
active: boolean
courseId: number
semester: number
matriculationsNumber: string
test: number
}
with ?
export type Student = {
id: string
title: string
firstname: string
lastname: string
gender: Gender
birthdate: Date
mail: string
phone: string | null
active: boolean
courseId: number | null
semester: number
matriculationsNumber: string
test: number
}
f
Copy code
course StudyCourse
  courseId Int
Both these should be without
?
But I think that - since we are speaking of a relation that might be
null
the
?
should be kept and you should recreate a type
Copy code
type StudentWithRequiredCourse = Student && Required<Pick<Student, 'course'>>
This should somehow work for you
b
https://github.com/vegardit/prisma-generator-nestjs-dto this might be what you're looking for
d
@Filippo Sarzana Using this type I get following errors (With double & I get even more)
@Benjamin Kroeger Looks interesting, I'll give it a try