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
Solution:
Hey 👋
This is a bit harder to grok.
What's immediate is that
conditionBuilder
might return undefined
and .where
expects something....Expressions | Kysely
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
Igal•35d ago
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/FIsu8Marcus - noxify•35d ago
Thanks igal - will give it a try
Marcus - noxify•34d ago
Hi @Igal - just want you to know that everything works now.
I just changed the
as any
to as ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>
Thanks again for spending your free time to solve this challenge.
Let's see if I can find something more challenging 😉1.4KMembers
View on DiscordWant results from more Discord servers?
More PostsHow to add table/column comments when creating tables?Hey, we're approaching close to 100 tables in our project. New people will be joining our founding tNeed help improving a custom helperMy dopamine with Kysely is quite high right now, so I am experimenting all sorts of things to createjsonArrayFrom with `as` not being typedI have the following query
```ts
await db
.selectFrom('user')
.selectAll()
.select((eb) How to use multiple schema definitions ( e.g. <catalog>.<schema>.<table> )Hey guys,
I'm currently working on a new version for our graphql wrapper.
Currently we're using kneUsing raw SQL with `or` whereI have been using the raw SQL template tag to work with certain JSON columns and wanted to add a `orCustom Plugin to transform Alias.* to "Alias.Column1", "Alias.Column2", etc.Hi everyone, in the previous question, I asked how to transform the following syntax into a new one.can kysely be extended with custom types within the databasehi. i would like to extend my database with custom types (a date type for sqlite) so that it is easinewbie need help with json_build_objectHi! Just starting with kysely and encountered problem I cant solve. I want to build raw query with kSolved: Exporting query builder classesI saw this PR https://github.com/kysely-org/kysely/pull/763 where you guys have decided not to exporbuilding kysely makes the package size very largehi. i'd be more than happy to figure this out because i like kysely a lot more than drizzle. the thiKysely setup in monolith APIHello, we have a basic http server with a basic router and a PostgreSQL database. We're wondering whIs it possible to get the total count while fetching rows in a single query?I have a query which looks like this:
```typescript
const { items, count } = await db
.selectFrom(