chad
chad
KKysely
Created by chad on 7/30/2024 in #help
select / groupBy combined helper
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
export type SelectAndGroupBy<DB, TB extends keyof DB> = {
select: SelectExpression<DB, TB>;
groupBy: StringReference<DB, TB> | RawBuilder<never>;
};
export type SelectAndGroupBy<DB, TB extends keyof DB> = {
select: SelectExpression<DB, TB>;
groupBy: StringReference<DB, TB> | RawBuilder<never>;
};
and then some helpers that can infer the right types at the call-site, for example:
export const fromColumn = <DB, TB extends keyof DB>(
col: StringReference<DB, TB>,
alias: string,
): SelectAndGroupBy<DB, TB> => ({
select: expressionBuilder<DB, TB>().ref(col).as(alias),
groupBy: col,
});
export const fromColumn = <DB, TB extends keyof DB>(
col: StringReference<DB, TB>,
alias: string,
): SelectAndGroupBy<DB, TB> => ({
select: expressionBuilder<DB, TB>().ref(col).as(alias),
groupBy: col,
});
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(...).
6 replies
KKysely
Created by chad on 7/30/2024 in #help
select / groupBy combined helper
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 RawBuilders
6 replies