Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColumnType is not extracted for array data type #957

Closed
pavestru opened this issue Apr 22, 2024 · 1 comment
Closed

ColumnType is not extracted for array data type #957

pavestru opened this issue Apr 22, 2024 · 1 comment
Labels
invalid This doesn't seem right typescript Related to Typescript

Comments

@pavestru
Copy link

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
Copy link
Member

igalklebanov commented Apr 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right typescript Related to Typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants