const selectAndGroupBy = (qb: SelectQueryBuilder<Database, 'person', any>,
selection: SelectExpression<Database, 'person'>
) => qb.select(selection).groupBy(selection);
const groupBys = ['age']
const person = await db
.selectFrom('person')
.select(['id', 'first_name'])
.$if(groupBys.includes('age'), (qb) => selectAndGroupBy(qb, 'age'))
const selectAndGroupBy = (qb: SelectQueryBuilder<Database, 'person', any>,
selection: SelectExpression<Database, 'person'>
) => qb.select(selection).groupBy(selection);
const groupBys = ['age']
const person = await db
.selectFrom('person')
.select(['id', 'first_name'])
.$if(groupBys.includes('age'), (qb) => selectAndGroupBy(qb, 'age'))