Error: don't await SelectQueryBuilder instances directly.
To execute the query you need to call
execute
or executeTakeFirst
.
I'm trying to create a function that enhances the query (SelectQueryBuilder
) with pagination, sorting and filtering options that are derived from a HTTP request.
This function looks like:
this function needs to async
as the getValidatedData()
is also async
in a handler I call applyQueryParams
like:
but this fails with:
and I don't understand what is wrong š¦ ....
this seems not to be correct: qb = await this.applyQueryParams(qb);
, but I need to await
it.
anybody an idea what I'm doing wrong?Solution:Jump to solution
If you return a thenable object from an async function its
then
method gets called. Kysely query builders all have a then
method that throws that exception. It's there because many other query builders and ORMs allow you to do stuff like
...6 Replies
I found this related issue as well: https://github.com/kysely-org/kysely/issues/838
but it does not give a solution, or maybe there is none, and an
async
function can't return a query builder?GitHub
SelectQueryBuilder preventAwait makes it impossible to return a que...
Issue I have a database repository class that internally uses Kysely for querying. There are two methods that share a similar query, so I have a helper function to create the query. To fill where p...
GitHub
Using sql helper in promise chains causes unexpected preventAwait e...
Here's a simple example: import { sql } from 'kysely' // a plain promise for the example, but this could be a value determined inside a transaction // or as part of some other promise c...
GitHub
A way to disable 'preventAwait' by wirekang Ā· Pull Request #748 Ā· k...
Although @koskimas closed #693 as 'wontfix', I'm suggesting a way to disable the behavior of preventAwait, with a small footprint. We often returning non-Promise value in as...
Solution
If you return a thenable object from an async function its
then
method gets called. Kysely query builders all have a then
method that throws that exception. It's there because many other query builders and ORMs allow you to do stuff like
and Kysely doesn't. If we didn't have the then
method, we'd get a lot of issues about queries not getting executed.
Just wrap the return value to something not thenable. For example { qb }
thx @koskimas I already was using that (hopefully temporary) when I got the error:
and then using it like:
and that works.
I must admit, it doesn't really look pretty š
my other alternative is to split retrieving the async request data and enhancing the query.
that way I can make it non async
I'm now using this: