kysely-org / kysely

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

ColumnType is not extracted for array data type #957

Closed pavestru closed 5 months ago

pavestru commented 5 months ago

I have used kysely-codegen to generate the DB model. However, select statements do not return correct TS type corresponding to int8[] data type.

Simplified example

TYPESCRIPT PLAYGROUND LINK

Consider the following schema (postgresql)

CREATE TABLE some_table (some_column int8[]);

Given that this is the only table, the DB model generated by kysely-codegen would look like this (without any kysely-codegen customization):

type Int8 = ColumnType<string, string | number | bigint, string | number | bigint>

export interface SomeTable {
  some_column: Int8[]
}

export interface DB {
    some_table: SomeTable
}

Now, we have a function selecting the array:

async function getSomeData(db: Kysely<DB>) {
    const result = await db
        .selectFrom('some_table')
        .select('some_column')
        .execute()

    return result;
}

Expected return type

Expected type returned by the query for column some_column should be string[].

Rationale: some_column is array of Int8. Type Int8 is defined using ColumnType, where the first generic parameter (for SELECT) is defined as string. That means that the returned type for some_column should be string[].

Actual return type

However, the select query returns type Int8[] for some_column, because there is no support for array of ColumnType. In other words, ColumnType does not get "unwrapped" for array data type.

TYPESCRIPT PLAYGROUND LINK

igalklebanov commented 5 months ago

Hey 👋

We do not have control over kysely-codegen.

The column type should be ColumnType<string[], ..., ...> and not ColumnType<string, ..., ...>[].

To reuse your definition, you can define it as follows:

type Int8Array = ColumnType<
  SelectType<Int8>[],
  InsertType<Int8>[],
  UpdateType<Int8>[]
>

If the faulty plural type is a codegen output, should submit an issue there.