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

Expose ExtractTableAlias type to allow for higher order queries #1271

Open
zaalbarxx opened this issue Nov 21, 2024 · 0 comments
Open

Expose ExtractTableAlias type to allow for higher order queries #1271

zaalbarxx opened this issue Nov 21, 2024 · 0 comments

Comments

@zaalbarxx
Copy link

zaalbarxx commented Nov 21, 2024

Hi!
Recently I was working on some project which uses Kysely and repository pattern for calling database. Here I've created some "common" query functions.

The problem can be seen here:
https://stackblitz.com/edit/typescript-bgfy14?file=index.ts,common.ts,package.json,tsconfig.json

If you look at common.ts there is an getAllWhere method which Typescript is complaining with. Here TS is trying to compare signatures and says that

Type 'ExpressionBuilder<DB, ExtractTableAlias<DB, TableName>>' is not assignable to type 'ExpressionBuilder<DB, TableName>'

I guess that's how TS works and it cannot properly assume that TableName extends keyof DB & string is actually always a table name without alias so later on it tries to infer aliased table name with ExtractTableAlias (and obviously falls back to the same value/string, because TableName cannot have alias). So it would be helpful if I could do something like:

getAllWhere<
      T extends SimpleReferenceExpression<DB, TableName>,
      VE extends OperandValueExpressionOrList<DB, ExtractTableAlias<DB, TableName>, T>,
      QE extends ExpressionOrFactory<DB, ExtractTableAlias<DB, TableName>, SqlBool> // <=== **ExtractTableAlias** used
    >(
      clauses: [T, ComparisonOperatorExpression, VE][] | QE
    ): Promise<Selectable<Entity>[]>

Of course I could do

const query = connection.selectFrom(tableName) as QueryBuilder<DB, TableName, {}>

, or just "steal" this ExtractTableAlias type and put it inside the project, but I think that's not optimal.
Maybe there is some other way to correctly type this thing so I would be grateful for some guidance, but looking at the exposed types I cannot really see a way to do it right.

Cheers

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

1 participant