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 incorrectly generated for pg lists #117

Open
ShadowAya opened this issue Nov 18, 2023 · 0 comments
Open

ColumnType incorrectly generated for pg lists #117

ShadowAya opened this issue Nov 18, 2023 · 0 comments
Labels
bug Something isn't working in progress We're working on it

Comments

@ShadowAya
Copy link

ShadowAya commented Nov 18, 2023

issue

I use Kysely with a PostgreSQL database. In one of my tables. I have a key of type timestamp without time zone[].

The generated type:

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface RateLimiting {
  action_id: string;
  timestamps: Timestamp[];
  user_id: string;
}

this puts a list of Timestamps into timestamps, but from my knowledge, that is improper use of ColumnType.

what happens

When doing a query, the following happens:

const limit = await db.selectFrom('rate_limiting')
            .selectAll()
            .where('user_id', '=', ctx.user.id)
            .where('action_id', '=', action)
            .executeTakeFirst();
            
typeof limit?.timestamps[0]

this returns one of the Timestamps as is, so now we are working with a ColumnType instead of Date (the type in this example has __insert__, __select__ and __update__ properties)

image

possible solution

I created a utility type for this:

type List<T> = T extends ColumnType<infer S, infer I, infer U> ? ColumnType<S[], I[], U[]> : never;

export interface RateLimiting {
  action_id: string;
  timestamps: List<Timestamp>;
  user_id: string;
}

image

I hope this is helpful, and thanks for the work that's been done here to make the Kysely experience better. I would love to commit a fix straight to the repo, but I'm not very experienced with this kind of stuff, only TypeScript. It'd be appreciated if a more experienced maintainer looks into the actual generation of such types.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@ShadowAya ShadowAya changed the title ColumnType falsely generated for pg lists ColumnType falsely generated for pg lists Nov 18, 2023
@ShadowAya ShadowAya changed the title ColumnType falsely generated for pg lists ColumnType incorrectly generated for pg lists Nov 18, 2023
@RobinBlomberg RobinBlomberg added bug Something isn't working in progress We're working on it labels Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in progress We're working on it
Projects
None yet
Development

No branches or pull requests

2 participants