select / groupBy combined helper
I realize this usecase looks awful, but our API takes an input of a list of fields to group by, which also means it will return those fields in the result. What I have works, but there's a lot of duplication:
but these 3 lines are repeated for each possible parameter a dozen times; and some are more complicated such as extracting multiple fields out of a timestamp.
I would prefer something like this but I can't figure out how to get it to typecheck
Playground here:
https://kyse.link/C4mRA
Solution:Jump to solution
But here's how you'd do it if you really really want to https://kyse.link/4PswR
4 Replies
I have an almost working solution now, with
AnyColumn<Database, 'person'>
but it doesn't work in the general case, for example some of my columns are RawBuildersIt's almost impossible to make this work. If you somehow manage to write the needed types, it will definitely be harder to maintain and understand than just having the copypasta
Solution
But here's how you'd do it if you really really want to https://kyse.link/4PswR
I have another approach for anyone who finds this. It's a bit of a failed experiment for my use-case but for a simpler one it might work ok:
https://kyse.link/gz42N
Basically I define a type that can hold a select and a groupBy
and then some helpers that can infer the right types at the call-site, for example:
and if you have a list of these you can
.map(f => f.select)
to get a list you can pass to db.select(...)
plus .map(f => f.groupBy)
to pass to db.groupBy(...)
.