What's the reasoning for using null for optional values in generated typings?
I feel like lots of folks prefer to use only one type to determine the absence of a value and usually, they settle on undefined.
Is it possible to customize this during generation?
Thanks a lot for raising this issue @krzkaczor! Would you mind sharing some of your application code where you're interacting with the Prisma Client and where you'd prefer to use undefined instead of null?
@schickling:
Building on top of https://github.com/prisma/prisma-examples/blob/prisma2/typescript/rest-express/src/index.ts
const users = await prisma.user.findOne({ where: { id: 1} })
findOne return type is user | null. Whereas I would prefer it to be user | undefined. And of course, I would also want to have undefined in the runtime 馃槅
I'm also in for such change in the api. It makes a lot of sense when working with optional attributes, which can be undefined but not null.
I think undefined is a better match.


A well-known js dev weighs in: https://github.com/sindresorhus/meta/issues/7 https://news.ycombinator.com/item?id=24956156
Thanks for the question! In the Prisma Client there's a specific difference between null and undefined.
null is a specific value that's persisted in an SQL database as NULL. If I run:
prisma.user.create({
data: {
id: 10,
customer: null
}
}
The row in an SQL database looks like this:
|id|customer|
|:-|:-|
|10|NULL|
undefined is the absence of any value. That looks like this:
prisma.user.create({
data: {
id: 10,
customer: undefined
}
}
Conceptually that's the same as
prisma.user.create({
data: {
id: 10
}
}
This leaves the prisma engine to decide the value, falling back on the @default value.
|id|customer|
|:-|:-|
|10|'default value'|
This conceptual difference doesn't show up that often in Javascript libraries, which is why Sindre chose to consolidate the concept. For Prisma, the difference matters. Hope this helps!
Most helpful comment
@schickling:
Building on top of https://github.com/prisma/prisma-examples/blob/prisma2/typescript/rest-express/src/index.ts
findOnereturn type isuser | null. Whereas I would prefer it to beuser | undefined. And of course, I would also want to haveundefinedin the runtime 馃槅