kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.22k stars 259 forks source link

Web, Getting Started - MySQL dialect example produces type error #886

Closed linas-ipxo closed 6 months ago

linas-ipxo commented 6 months ago

Hello.

Direct copying examples from https://kysely.dev/docs/getting-started?dialect=mysql produces a type error in createPerson():

image

I obviously can work around this, but as a bit of a newcomer, I'm not exactly sure if this is intented, or is this a bug somewhere.

igalklebanov commented 6 months ago

Hey 👋

This is a documentation issue - PR's welcome.

  1. findPersonById should accept bigint or even number | bigint.
  2. Since we're in MySQL context - insertId is not undefined, you can add a non-null assertion as follows: return await findPersonById(insertId!)
linas-ipxo commented 6 months ago

Okay, but then person is defined as

export interface PersonTable {
  // Columns that are generated by the database should be marked
  // using the `Generated` type. This way they are automatically
  // made optional in inserts and updates.
  id: Generated<number>
  ...

and the Person interface doesn't accept bigint in other parts of the examples. I can change everything to bigint, but shouldn't the executeTakeFirstOrThrow() function return insertId typed based on the interface (number)? and not bigint?

koskimas commented 6 months ago

We don't know which property is the auto incrementing primary key. All Kysely sees is the table interface and there's no indication of the primary key. Some tables don't even have it or they have a composite primary key.

Bigint is used because it can hold any integer value, unlike number.

You can easily cast the type using Number(insertId!)

koskimas commented 6 months ago

I fixed the getting started guide.