K
Kysely•5mo ago
Tim Lonsdale

Unioning an array of sql`{SQL_QUERY}`

Hey chaps, I'm doing some really heinous shit with triggers etc (hence why I'm not using Kysely's default query builder syntax). I have an array of SQL queries that I'd like to UNION ALL together - any idea how best to do that? Note - the length of the query array is dynamic - Thanks!!
3 Replies
Tim Lonsdale
Tim LonsdaleOP•5mo ago
do I use Array.reduce? I suppose that would work
Igal
Igal•5mo ago
Hey 👋 You could pass that array to sql.join.
import { sql } from 'kysely'

sql
.join(
[
db.selectFrom("person").selectAll(),
db.selectFrom("pet").selectAll(),
],
sql` union all `,
)
.execute(db);
import { sql } from 'kysely'

sql
.join(
[
db.selectFrom("person").selectAll(),
db.selectFrom("pet").selectAll(),
],
sql` union all `,
)
.execute(db);
https://kyse.link/YuLDd
Tim Lonsdale
Tim LonsdaleOP•5mo ago
thanks mate!

Did you find this page helpful?