K
Kysely•6mo ago
bombillazo

Dynamic conditional raw query question

Hello there, Im trying to create a dynamic query like this:
await sql`SELECT
column AS "data"
FROM my_table
${type ? ` WHERE type = ${type}` : ''};`.execute(client);
await sql`SELECT
column AS "data"
FROM my_table
${type ? ` WHERE type = ${type}` : ''};`.execute(client);
But kysely is trying to do substitution where I have my TS logic to either add the WHERE clause or not. How can I fix this ?
3 Replies
bombillazo
bombillazo•6mo ago
Skip the questions of why im using a string query vs the query builder, this is a once off raw query to tables I don't have the schema for. 😆
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
koskimas
koskimas•6mo ago
You can use Kysely<any> to write that query. Just cast any db instance
const anyDb: Kysely<any> = db
const anyDb: Kysely<any> = db
But this also works
await sql`SELECT
column AS "data"
FROM my_table
${type ? sql`WHERE type = ${type}` : sql``};
`.execute(client);
await sql`SELECT
column AS "data"
FROM my_table
${type ? sql`WHERE type = ${type}` : sql``};
`.execute(client);
sql.raw is not safe when there's user input involved.
Want results from more Discord servers?
Add your server