KyselyK
Kysely2y ago
5 replies
Marcus - noxify

Complex Query Builder from dynamic input - Typescript issue

Hi, it's me - again 🙂

I'm currently trying to migrate our current query builder.

It seems that everything is working as expected, but I get some type errors and I'm not sure how to fix this - Maybe you have an idea how to fix this / what the rootcause is.

The query is compiled and I can see it in my vitest & in the playground ( you have to open the web console 😉 )

Playground link: https://kyse.link/8cA4l

It's based on:
* https://kysely.dev/docs/recipes/expressions#creating-reusable-helpers
* https://kysely.dev/docs/recipes/expressions#conditional-expressions
An Expression is the basic type-safe query building block in Kysely. Pretty much all methods accept expressions as inputs. Most internal classes like SelectQueryBuilder and RawBuilder (the return value of the sql tag) are expressions themselves.
Solution
Hey 👋

This is a bit harder to grok.

What's immediate is that
conditionBuilder
might return
undefined
and
.where
expects something.
Also,
.flatMap(identity)
could be changed to
.flat()
.
conditionBuilder
should return
ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>
.
filters
should be of type
ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>[]

getFieldQuery
should also return
ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>

You can
as any
getFieldQuery
's return values.
https://kyse.link/FIsu8
Was this page helpful?