#Database design

5 messages · Page 1 of 1 (latest)

boreal estuary
#

hey, i have a question about db design

model Course {
    id String @id @default(cuid())

    title       String
    description String

    type CourseType

    // slug        String       @unique

    users   CourseUser[]
    modules CourseModule[]

    createdAt DateTime  @default(now())
    updatedAt DateTime? @updatedAt
}

model CourseModule {
    id String @id @default(cuid())

    title       String
    description String
    index       Int

    course   Course @relation(fields: [courseId], references: [id])
    courseId String

    createdAt DateTime  @default(now())
    updatedAt DateTime? @updatedAt
}```


I wanna know if I wanted to make it so that every ``CourseModule`` several items linked to a ressource : "video" | "project" | "webinar" where each one have some special properties

how can i implement that in a clean way? 
I thought about making an enum ``CourseModuleItemType`` and have optional keys Video Project Webinar 
Or not have the ``CourseModuleType`` property and straight just optional keys then i would know from which one exists later in the code to know the type
manic smelt
#

I think the answer really depends on how you want to be able to query this data and what else will need to relate to these pieces of data in the DB.

I don't see anything wrong with the approach you mentioned:

  • Create an enum called CourseModuleItemType with the possible values
  • Create a requried field on CourseModule of the type CourseModuleItemType
  • Add optional fields to CourseModule that are needed by each type
boreal estuary
#

thanks again, i appreciate it

manic smelt
#

Sure thing! Glad I could validate it for you. Seems you're headed the right direction 🙂