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

A way to disable 'preventAwait' #748

Closed
wants to merge 14 commits into from

Conversation

wirekang
Copy link
Contributor

@wirekang wirekang commented Oct 26, 2023

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 async function or then() chains. It would be great if Kysely have an option for those situation, for people who aren't Knex-newbie or know how to execute query.

The name 'allowNoopAwait' is a little bit temporary, it can be renamed as 'allowAwait', 'ignoreAwait', 'disablePreventAwait', 'iKnowHowToExecute' or something you suggest.

@vercel
Copy link

vercel bot commented Oct 26, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 29, 2024 9:31am

@igalklebanov
Copy link
Member

igalklebanov commented Nov 7, 2023

Hey 👋

We often returning non-Promise value in async function or then() chains. It would be great if Kysely have an option for those situation

I didn't get the necessity quite honestly. Can you elaborate on that?

@igalklebanov igalklebanov added the enhancement New feature or request label Nov 7, 2023
@wirekang
Copy link
Contributor Author

wirekang commented Nov 8, 2023

I didn't get the necessity quite honestly. Can you elaborate on that?

The code from original issue #693, and I'm not sure I can find better example.. Yeah it is not "often" but "rarely", sorry for overstating.
But it worth considering turning off the behavior that throwing error without actual logical reason.

@koskimas
Copy link
Member

koskimas commented Nov 9, 2023

But it worth considering turning off the behavior that throwing error without actual logical reason.

There is a logical reason. People coming from knex and objection would make the mistake of awaiting queries without execute. Kysely is heavily inspired by knex which naturally leads people thinking it behaves the same.

@koskimas koskimas closed this Nov 9, 2023
@koskimas koskimas reopened this Nov 9, 2023
@wirekang
Copy link
Contributor Author

There is a logical reason. People coming from knex and objection would make the mistake of awaiting queries without execute. Kysely is heavily inspired by knex which naturally leads people thinking it behaves the same.

@koskimas I didn't denied it. I meant "programming logic error". Anyways, It was a light suggestion, never mind.

@koskimas
Copy link
Member

koskimas commented Jan 13, 2024

I think it wouldn't hurt to add this. But remove the delete clazz.prototype.then thing. Having a simple then method that simply values through when prevent = false shouldn't have any measurable effect on performance.

Edit: Or actually, will that cause infinite recursion? Does resolve call .then?

@wirekang
Copy link
Contributor Author

wirekang commented Jan 14, 2024

@koskimas Yes. That was the reason I have to delete then.

igalklebanov and others added 5 commits August 25, 2024 11:53
…kysely-org#1085)

* add reusable helpers recipe and implement missing expression features

* force node 22.4.1 in CI because of an npm bug
* feat: add postgres range types (kysely-org#1086)

* feat: support refresh naterialized view

* fix tests by adding .materialized() to remove the matview

* fix failing test

* fix: References typo (kysely-org#1092)

* chore: refresh-view-node.ts => refresh-materialized-view-node.ts

* chore: export node in index.ts

---------

Co-authored-by: Isak <[email protected]>
Co-authored-by: Jonathan Wu <[email protected]>
@matthew-holder-revvity
Copy link

matthew-holder-revvity commented Aug 27, 2024

preventAwait actually causes issues with anybody trying to return partially built queries from async functions. Not sure if the builders originally implemented the thenable protocol, but it's obvious it shouldn't be used as a means to execute the queries. It does mean that any return of queries or an attempt at query composition in async functions cannot work with preventAwait on the builders.

@marceloverdijk
Copy link

marceloverdijk commented Sep 5, 2024

I'm hitting this as well to use a async function to enhance a query...

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:

  async applyQueryParams<DB, TB extends keyof DB, O>(qb: SelectQueryBuilder<DB, TB, O>): Promise<SelectQueryBuilder<DB, TB, O>> {
    const data = await this.getValidatedData<PaginationSchema & SortSchema & FilterSchema>();
    const page = .. // derive page
    const pageSize = .. // derive page size
    const sort = .. // derive sort
    const filter = .. // derive filter

    console.log("Query builder before pagination:", qb);

    qb = qb.limit(pageSize);
    qb = qb.offset((page - 1) * pageSize);

    console.log("Query builder after pagination:", qb);

    return qb;
  }

this function needs to be async as the getValidatedData() is also async.

in a handler I call applyQueryParams like:

async handle(c: C) {

    let qb = c.var.mydb
      .selectFrom('customer')
      .selectAll('customer');

    console.log("Before applying query params:", qb);

    qb = await this.applyQueryParams(qb);

    console.log("After applying query params:", qb);

    const customers = await qb.execute();

but this fails with:

Before applying query params: SelectQueryBuilderImpl {}
Query builder before pagination: SelectQueryBuilderImpl {}
Query builder after pagination: SelectQueryBuilderImpl {}
✘ [ERROR] Error: don't await SelectQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.

I understand the rationale behind the preventAwait for knex users, but Typescript users - not coming from Knex - are now confused and limited.

@koskimas
Copy link
Member

koskimas commented Sep 6, 2024

I'll remove preventAwait if you guys promise the handle all incoming issues it will cause. I'll just tag you and ignore the issue. Sounds good?

@koskimas
Copy link
Member

koskimas commented Sep 10, 2024

Surely without a thenable interface on the queries, there should be no issue; other than folks from knex wondering why awaiting the query doesn't work, right?

And objection and a bunch of other ORMs. That's a big part of Kysely's users. What's your point?

@koskimas
Copy link
Member

koskimas commented Sep 29, 2024

Yet another issue about this. Let's just remove preventAwait. Let's see what happens in terms of issues.

@wirekang If you have time and interest, could you remove preventAwait everywhere in this PR and rebase it on top of the v0.28 branch? Just let me know if not and let's close this and I'll remove it later.

@wirekang
Copy link
Contributor Author

@koskimas Just removed all preventAwait related lines from v0.28 branch. Is it what you meant? (This PR is for master branch).

wirekang added a commit to wirekang/kysely that referenced this pull request Sep 29, 2024
@wirekang wirekang mentioned this pull request Sep 29, 2024
@wirekang
Copy link
Contributor Author

New PR for v0.28 branch: #1160

@wirekang wirekang closed this Sep 29, 2024
koskimas pushed a commit that referenced this pull request Sep 29, 2024
igalklebanov pushed a commit that referenced this pull request Oct 3, 2024
igalklebanov pushed a commit that referenced this pull request Oct 3, 2024
igalklebanov pushed a commit that referenced this pull request Oct 12, 2024
igalklebanov pushed a commit that referenced this pull request Oct 19, 2024
igalklebanov pushed a commit that referenced this pull request Oct 24, 2024
igalklebanov pushed a commit that referenced this pull request Oct 24, 2024
igalklebanov pushed a commit that referenced this pull request Oct 27, 2024
igalklebanov pushed a commit that referenced this pull request Nov 2, 2024
igalklebanov pushed a commit that referenced this pull request Nov 24, 2024
igalklebanov pushed a commit that referenced this pull request Dec 1, 2024
igalklebanov pushed a commit that referenced this pull request Dec 8, 2024
igalklebanov pushed a commit that referenced this pull request Jan 5, 2025
igalklebanov pushed a commit that referenced this pull request Jan 9, 2025
igalklebanov pushed a commit that referenced this pull request Jan 11, 2025
igalklebanov pushed a commit that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using sql helper in promise chains causes unexpected preventAwait errors
6 participants