Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Null vs undefined for optional values #572

Closed
krzkaczor opened this issue Mar 15, 2020 · 6 comments
Closed

Null vs undefined for optional values #572

krzkaczor opened this issue Mar 15, 2020 · 6 comments
Labels
kind/discussion Discussion is required. team/client Issue for team Client.

Comments

@krzkaczor
Copy link

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?

@schickling
Copy link
Member

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?

@krzkaczor
Copy link
Author

krzkaczor commented Mar 17, 2020

@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 😆

@pantharshit00 pantharshit00 added the kind/discussion Discussion is required. label Mar 18, 2020
@tajnymag
Copy link

tajnymag commented Sep 7, 2020

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.

@benatkin
Copy link

I think undefined is a better match.

Screen Shot 2020-09-14 at 23 10 54

null-undefined

@benatkin
Copy link

benatkin commented Nov 1, 2020

A well-known js dev weighs in: sindresorhus/meta#7 https://news.ycombinator.com/item?id=24956156

@pantharshit00 pantharshit00 added the team/client Issue for team Client. label Dec 24, 2020
@timsuchanek timsuchanek added process/candidate Candidate for next Milestone. and removed process/candidate Candidate for next Milestone. labels Jan 5, 2021
@matthewmueller
Copy link
Contributor

matthewmueller commented Jan 7, 2021

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/discussion Discussion is required. team/client Issue for team Client.
Projects
None yet
Development

No branches or pull requests

8 participants