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

[Feature]: Allow executing raw queries with a more idiomatic way #455

Open
kasvith opened this issue Apr 30, 2023 · 7 comments
Open

[Feature]: Allow executing raw queries with a more idiomatic way #455

kasvith opened this issue Apr 30, 2023 · 7 comments
Labels
api Related to library's API enhancement New feature or request

Comments

@kasvith
Copy link

kasvith commented Apr 30, 2023

Currently executing raw queries has the following structure

sql`SELECT 1 AS result`.execute(kysely)

It would be nice to have it the other way around as well as people naturally tend to think with the current instance first.

kysely.execute(sql`SELECT 1 AS result`)

or

kysely.raw(sql`SELECT 1 AS result`).execute()
@igalklebanov igalklebanov added enhancement New feature or request api Related to library's API labels Apr 30, 2023
@igalklebanov
Copy link
Member

igalklebanov commented May 1, 2023

Hey 👋

I think this might fit

const { rows } = await db.executeQuery(sql<{ result: number }>`select 1 as result`)

rows[0].result

@kasvith
Copy link
Author

kasvith commented May 1, 2023

its also good

@capaj
Copy link

capaj commented Nov 24, 2023

does this still work? I am trying to do

          await trx.executeQuery(
            sql<any>`ALTER SCHEMA ${current.subdomain} RENAME TO ${subdomain};`
          )

and I keep getting this query cannot be compiled to SQL

@capaj
Copy link

capaj commented Nov 24, 2023

ok this works ok

await sql`ALTER SCHEMA "test" RENAME TO "test23"`.execute(trx)

but when I try to use variables in the template it throws

@capaj
Copy link

capaj commented Nov 24, 2023

sorry for spamming. This is most likely a problem in pgPool as it has the same problem with the parameter for this query. TIL you cannot rename a schema in a stored procedure

@capaj
Copy link

capaj commented Nov 24, 2023

BTW on this note, is there a way to do raw query with plain old string literal in kysely?

@igalklebanov
Copy link
Member

igalklebanov commented Nov 24, 2023

@capaj

ok this works ok

await sql`ALTER SCHEMA "test" RENAME TO "test23"`.execute(trx)

but when I try to use variables in the template it throws

Some engines don't support any parameters in DDL, or in some queries. Try using sql.ref or sql.raw in those interpolated positions.

BTW on this note, is there a way to do raw query with plain old string literal in kysely?

Check sql's methods. e.g. sql.raw.

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 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants