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

Generated types aren't useable in Kysely queries #112

Closed
nico-martinucci opened this issue Oct 29, 2024 · 2 comments
Closed

Generated types aren't useable in Kysely queries #112

nico-martinucci opened this issue Oct 29, 2024 · 2 comments

Comments

@nico-martinucci
Copy link

Any fields with default values get this Generated<> wrapper around them in the generated types:

export type Reviews = {
    id: string;
    createdAt: Generated<Timestamp>;
    status: Generated<ReviewStatus>;
    isDeleted: Generated<boolean>;
};

If I use this type to create a filters type:

export type ReviewsFilters = Partial<Pick<Reviews, "isDeleted" | "status">>;

And then try to use this in a query:

export const getReviews = async (filters: ReviewsFilters) => {
  let query = kyselydb.selectFrom("Reviews").selectAll();

  if (filters.status) query = query.where("status", "=", filters.status);
  if (filters.isDeleted || filters.isDeleted === false) query = query.where("isDeleted", "is", filters.isDeleted)

  return query.execute();
};

I get the following typescript error in the .where() clause for filters.status:

Argument of type 'ColumnType<"PENDING", "PENDING" | undefined, "PENDING"> | ColumnType<"APPROVED", "APPROVED" | undefined, "APPROVED"> | ColumnType<"REJECTED", "REJECTED" | undefined, "REJECTED">' is not assignable to parameter of type 'OperandValueExpressionOrList<DB, "Reviews", "status">'.
  Type 'ColumnType<"PENDING", "PENDING" | undefined, "PENDING">' is not assignable to type 'OperandValueExpressionOrList<DB, "Reviews", "status">'.

And similar for filters.isDeleted:

Argument of type 'ColumnType<false, false | undefined, false> | ColumnType<true, true | undefined, true>' is not assignable to parameter of type 'OperandValueExpressionOrList<DB, "Reviews", "isDeleted">'.
  Type 'ColumnType<false, false | undefined, false>' is not assignable to type 'OperandValueExpressionOrList<DB, "Reviews", "isDeleted">'.

Am I missing something? Ideally these would generate as:

export type Reviews = {
    id: string;
    createdAt: Timestamp;
    status: ReviewStatus;
    isDeleted: boolean;
};

Or there would be a config option to lose the Generated<> and just get those base types.

@PPillau
Copy link

PPillau commented Oct 30, 2024

I think what you need is the Kysely type Selectable. You would do something like Selectable<Reviews>.

@nico-martinucci
Copy link
Author

Ah that did it! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants