Hello, I using Prisma + TypeScript – I have set an...
# orm-help
s
Hello, I using Prisma + TypeScript – I have set an optional database field, however the generated types are not optional. For example: Database field:
createdAt         DateTime?       @default(now())
Generated TypeScript:
createdAt: Date | null
Is this expected? Is this configurable to change? Otherwise I have to pass a value for these optional fields..
p
There is no such thing as “undefined” in a database. A field can be either NULL, EMPTY or with a value.
s
Maybe I wasn't clear. The TypeScript type generated, forces you to set a value. How does the default DB value work if I must set a value when creating a record?
j
The main type may have
createdAt: Date | null
but there should be additional
*CreateInput
and
*UpdateInput
(etc.) types where you do have
createdAt?
, and those are what are used for the actual insert/update/etc.
For example,
Copy code
model User {
  id         Int         @id @default(autoincrement()) 
  email      String?     @unique @db.VarChar(100)
  password   String?     @db.VarChar(100)
}
generates
Copy code
export type User = {
  id: number
  email: string | null
  password: string | null
}
and (notice
id
omitted completely)
Copy code
export type UserCreateInput = {
    email?: string | null
    password?: string | null
}
💯 2
s
Perfect! That's exactly what I'm after. Thanks @Jason Abbott