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 (
.where
s) 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
Hey 👋🏻
Extract to helpers that receive builder as argument and invoke stuff on it.
Then use helpers with
.$call
to invoke helpers.how would type-safety work here? how would the builder argument know that the common columns exist on it?
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 🙂
generics are my weakness
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
I'll come up with a playground link tomorrow
much appreciated
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 consumeryou are a wizard
huge thanks :D