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

Using sql helper in promise chains causes unexpected preventAwait errors #693

Closed
icopp opened this issue Sep 12, 2023 · 1 comment · May be fixed by #748
Closed

Using sql helper in promise chains causes unexpected preventAwait errors #693

icopp opened this issue Sep 12, 2023 · 1 comment · May be fixed by #748
Labels
api Related to library's API wontfix This will not be worked on

Comments

@icopp
Copy link

icopp commented Sep 12, 2023

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 chain
const someTimestampValue = Promise.resolve(true).then(
  (value) => value
    ? sql<string>`'infinity'::timestamptz`
    : sql<string>`'-infinity'::timestamptz`
)

This results in a raised error:

Error: don't await RawBuilder instances directly. To execute the query you need to call `execute`

This is non-obvious behavior for a tag function, where I think the expected behavior here should be to act much more like a generic object (e.g. const a = b and const a = await b should have effectively the same behavior).

@koskimas
Copy link
Member

koskimas commented Sep 12, 2023

If we remove the check, we get a new issue: especially people coming from knex will be confused when

await sql`some sql`

doesn't execute the query. Since you are the first one to bring this up, I'm assuming that'd be a bigger problem.

You can wrap the sql snippets in objects as a workaround

const someTimestampValue = Promise.resolve(true).then(
  (value) => value
    ? { sql: sql<string>`'infinity'::timestamptz` }
    : { sql: sql<string>`'-infinity'::timestamptz` }
)

@koskimas koskimas added wontfix This will not be worked on api Related to library's API labels Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API wontfix This will not be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants