K
Kysely5mo ago
decho

Creating a jsonAgg helper funtcion that returns a context aware `AggregateFunctionBuilder`

Greetings. I am wondering if it's possible to create a helper that does exactly what eb.fn.jsonAgg does but for JSONB. - I have tried using the helpers/postgres/jsonArrayFrom, but that is functionally not the same because it creates a subquery. - I have tried creating a simple helper, but it's not type/context aware, and it doesn't support method chaining (.distinct() for example):
function jsonbAgg<O>(
expr: Expression<O>,
): RawBuilder<Simplify<O>[]> {
return sql`jsonb_agg(${expr})`;
}
function jsonbAgg<O>(
expr: Expression<O>,
): RawBuilder<Simplify<O>[]> {
return sql`jsonb_agg(${expr})`;
}
- Lastly, I have tried recreating the jsonAgg function from the source code and that almost works, however it's not aware of the query context, and you can for example aggregate on a completely unrelated table, since it uses the entire DB type as first generic parameter (<DB extends MyDB, TB extends keyof DB....> Example of the problem demonstrated: https://kyse.link/7zHaq --- I have since refactored my code, but this might not always be an easy option in the future, so I was curious to know if there is a way around this problem.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server