K
Kysely2y ago
ohmi

Querying two different tables with subset of common columns

I have two tables that have an intersection of common columns that I'm looking to query. For each of the tables, I have identical conditional logic (.wheres) that I'd like to apply for both queries, and it's quite extensive so I'd prefer not to copy and paste. My query would only .select the common columns. Is there any Kysely-ic way of doing this?
8 Replies
Igal
Igal2y ago
Hey 👋🏻 Extract to helpers that receive builder as argument and invoke stuff on it. Then use helpers with .$call to invoke helpers.
ohmi
ohmiOP2y ago
how would type-safety work here? how would the builder argument know that the common columns exist on it?
Igal
Igal2y ago
Short answer: generics. Medium answer: Each builder has 2 main generics, DB - the accumulated database object (+ aliased tables, CTE), and TB - the accumulated table and CTE names. This is the query context all things are bound to. Helper's generics/args need to make sure certain tables or columns are in the builder's query context to accept it as argument. Long answer: Tomorrow, I need to sleep 🙂
ohmi
ohmiOP2y ago
generics are my weakness devastated would be super helpful if you have a minimal example to point to, no rush or anything, and goodnight this is the final hurdle before completing my migration
Igal
Igal2y ago
I'll come up with a playground link tomorrow
ohmi
ohmiOP2y ago
much appreciated
Igal
Igal2y ago
something like this? https://kyse.link/?p=s&i=qaJuxXuBRPYTZeemHHeG the as any spam inside helpers is unavoidable, typescript can't narrow the generic values there 😦 the important part is that you're strongly typed for the consumer
ohmi
ohmiOP2y ago
you are a wizard huge thanks :D
Want results from more Discord servers?
Add your server