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

Question about a generic getById #995

Closed
Guid75 opened this issue May 20, 2024 · 2 comments
Closed

Question about a generic getById #995

Guid75 opened this issue May 20, 2024 · 2 comments
Labels
question Further information is requested typescript Related to Typescript

Comments

@Guid75
Copy link

Guid75 commented May 20, 2024

Brand new user of this fantastic library, I'm searching a way to write a generic function to handle the getById classic usecase, here is my naive approach:

import { Kysely, Selectable } from "kysely";

export type FooTable = {
  id: string;
  name: string;
  fromFoo: boolean;
};

export type BarTable = {
  id: string;
  name: string;
  fromBar: string;
}

type Tables = {
  foo: FooTable;
  bar: BarTable;
};


export async function getById<TableName extends keyof Tables>(
  dataSource: Kysely<Tables>,
  tableName: TableName,
  id: string
) {
  const query = dataSource
    .selectFrom(tableName)
    .selectAll()
    .where("id", "=", id);

  return await query.execute();
}

Here is the playground link

I don't really get why I cannot pass the id parameter into the where clause :(
What did I miss?

@koskimas
Copy link
Member

koskimas commented May 20, 2024

Works if you simplify it.

But in general you shouldn't try to write generic code like this with Kysely. Kysely's types are extremely strict and it's hard to combine strict and generic. Your example was probably not a real use case, but you saved very little code by having that helper.

@Guid75
Copy link
Author

Guid75 commented May 20, 2024

Ok, thank you very much, I can live with some little repetitions

@igalklebanov igalklebanov added question Further information is requested typescript Related to Typescript labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested typescript Related to Typescript
Projects
None yet
Development

No branches or pull requests

3 participants