kysely-org / kysely

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

Potential type improvement for deleteFrom and executeTakeFirst return result #346

Closed Vyast closed 1 year ago

Vyast commented 1 year ago

👋

Quick question/potential feature request about the following:

const result = await db
    .deleteFrom("Log")
    .where("expiresAt", "<=", date)
    .executeTakeFirst();

const count = result.numDeletedRows ? Number(result.numDeletedRows) : 0;

Running the example above, result is always an object with type DeleteResult, but if 0 rows are deleted, result.numDeletedRows returns undefined and result is empty, but the type of numDeletedRows according to typescript is always bigint. If 1 or more rows are deleted, result.numDeletedRows returns an accurate count as type bigint as expected.

I am not sure if this is the intended behavior, a bug on my end, if I am overlooking something, or if there is something I can change, but as a suggestion I feel that if the undefined return value behavior is correct, then the return type for result.numDeletedRows should represent that correctly as bigint | undefined, otherwise if it is not intended behavior, return 0 if 0 rows are deleted.

I am using the planetscale dialect for reference, and generating types using kysely-codegen

{
  "kysely": "^0.23.4",
  "kysely-planetscale": "^1.2.1",
  "kysely-codegen": "^0.9.0"
}

Thanks!

koskimas commented 1 year ago

You should open this issue in kysely-planetscale repo. Their implementation is wrong. kysely-planetscale is a third party dialect and we have no control over it.