Comments (7)
string | null
is the correct type since the value can either be a string or a null. I don't know how to document this without stating the obvious. I could add nullable values to some examples maybe?
I tried to implement the insert type so that you could leave out nullable values, but wasn't able to make that work. I'll give it another try.
from kysely.
string | null
is the correct type since the value can either be a string or a null. I don't know how to document this without stating the obvious. I could add nullable values to some examples maybe?
If you recommend that users use string | null
instead of an optional field to represent a nullable column, you can just say so. For example, the doc could read:
To specify a nullable column of type
T
, useT | null
. This maintains an intuitive mapping between SQL NULL and Typescriptnull
. Do not use an optional property.
I don't see the downside to stating this when javascript provides at least three ways to represent a missing value (null
, property set equal to undefined
and property not present). NULL
<=> null
may seem 'obvious' but it's still a mapping between different systems.
I tried to implement the insert type so that you could leave out nullable values, but wasn't able to make that work. I'll give it another try.
Well, you can use an optional property instead! ;) I'm really not that concerned about the convenience factor of leaving out the null values on insert, I'll use happily T | null
.
from kysely.
Well, you can use an optional property instead! :)
No, that's the wrong type. null
is null
and undefined
is undefined
. Those are not different ways to represent the same thing.
- When you don't select a column, the column is not present in the result type at all.
- When you select a column, but its value is null in the db, you get a null value whose correct type is
null
. - There is no way to get an undefined value in kysely, but that's also a different case.
For example, if you define a property as optional using ?
or as string | undefined
and you get a null value from the db, this code would be incorrect
if (person.firstName !== undefined) {
// Here the type would be narrowed down to `string` from `string | undefined` but
// the value could still be `null` leading to errors.
person.firstName.toLowerCase() // This would throw, but typescript would be happy
}
So if the value can be null
the only correct way to type it is to give it a type T | null
.
from kysely.
Ah, of course, the DB engine will return null
in either case, duh! So using ?
will break the types. In this case I think it's even more important to say in the docs:
To specify a nullable column of type T, use
T | null
. Do not use an optional property to represent a nullable column, because the resulting types will be incorrect when usingselectFrom
!
Yes, it's obvious, but I think it's important to note. Lots of fools besides me will make the same mistake!
from kysely.
Where do you think I should say that? In the readme somewhere?
from kysely.
The example in the readme now has this
interface Person {
id: number
first_name: string
// If the column is nullable in the db, make its type nullable.
// Don't use optional properties.
last_name: string | null
gender: 'male' | 'female' | 'other'
}
from kysely.
Great, I was just going to suggest the same thing -- adding an example with a comment! Cheers.
from kysely.
Related Issues (20)
- Node 14 support is missed after bumping to version 0.27.0
- agg requires two type parameters HOT 1
- Selectable and Insertable types are not generating correct types HOT 1
- Spanish tutorial for Kysely basics Available
- modifying data (e.g. updateTable) inside a migration HOT 5
- Support for node16 and nodenext module resolution HOT 1
- Allow parameters in ExecuteStatement for RDS Aurora Data API HOT 2
- add modifyFront/modifyEnd to all queryBuilder classes
- How to prevent Kysely from crashing node app if database goes down HOT 6
- How to write custom SelectQueryBuilder? HOT 2
- SelectQueryBuilder preventAwait makes it impossible to return a query from an async function. HOT 2
- Add support for Postgres bulk update statement HOT 3
- Introspect should also fetch column comments so we can generate typescript comments HOT 1
- Postgres migrateUp sometimes failing if migration name has a "-" character HOT 2
- syntax error at or near "returning" with vercel postgres database HOT 3
- Fix: Mssql introspection duplicate columns
- Add limit method for UpdateQueryBuilder
- JSON select types are working incorrectly with zod passthrough object HOT 5
- Compile time checks for empty arrays HOT 2
- Incorrect types and missing suggestions when using object index signature HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kysely.