kysely-org / kysely

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

Incorrect type when using left join and json agg (postgres) #985

Closed probablykabari closed 2 months ago

probablykabari commented 2 months ago
const result = kysely
  .selectFrom("some_table")
  .leftJoin("another_table", "some_table.id", "another_table.some_table_id")
  .select((eb) => [
    eb.fn
      .jsonAgg("another_table")
      .as("another_records"),
     ...

In the above, the typescript type is incorrect for the result of the json_agg function call because with a left join the values inside the json array can be [ null ] if there are no records in the join. To resolve this you need to add .filterWhere("another_table.id", "is not", null), which would make the result of the agg just null, however this type is also incorrect in the library.

This is just a type issue, the query works as expected.

koskimas commented 2 months ago

Unfortunately it's not feasible to fix this. We'd need to carry around extra information throughout Kysely types to fix this.

A known issue, but won't fix for now at least.

koskimas commented 2 months ago

You can create your own helper that works for your use cases though.

probablykabari commented 2 months ago

All good, I figured it would be difficult to type.