groupByTyped
Description
groupByTyped sets GROUP BY to the columns a callback selects. addGroupByTyped adds them to the existing GROUP BY.
Signature
groupByTyped(
selection: KeyProxyCallback<Entity>,
options?: TypedGroupByOptions
): SelectQueryBuilder<Entity>
addGroupByTyped(
selection: KeyProxyCallback<Entity>,
options?: TypedGroupByOptions
): SelectQueryBuilder<Entity>
Arguments
- selection: KeyProxyCallback<Entity>: The callback which selects one column, or several at once (
user => [user.id, user.email]); each of them goes into GROUP BY. A JSON column goes into GROUP BY as a whole (event => event.settings; on PostgreSQL ajsonbcolumn, asjsonhas no equality operator). A relation whose join column is on the other side (user => user.images) or an embedded entity (person => person.address) throws aTypeError; a many-to-one relation groups by its join column. The callback starts at the entity, or at the root set withtyped<Root>(). Columns of joined relations work too (user => user.images.url). A path into a JSON column (user => user.metadata.role) throws aTypeErrorand leaves GROUP BY as it was. - options?:
TypedGroupByOptions:{ useQuotes?: boolean }. WithuseQuotes: truethe key is the quoted database column ("ticket"."ticket_status",@Column({ name })applied) instead ofticket.status. Without it:ticket.status, which TypeORM maps to the column itself. An alias without metadata, a subquery or a table in FROM or a join that isn't an entity (.from('recent_events', 'recentEvents')), is quoted unlessuseQuotes: falseis passed: TypeORM leaves its keys as written, and PostgreSQL folds an unquotedrecentEvents.startsAttorecentevents.startsat. An alias the query adds after the call is handled the same way once it is added: TypeORM maps the keys of an entity's alias, and those of a subquery or a CTE are quoted. So is an alias the query never adds, which only raw SQL brings in (aLATERALsubquery in the condition of a raw join): its keys are quoted when the SQL is built. On PostgreSQL a column that raw SQL names without quotes (AS userId) isuserid: quote it in the SQL (AS "userId") or passuseQuotes: false.
Usage
// same as `query.groupBy('user.id')`
query
.leftJoinTyped(user => user.images, 'images')
.select('user.id', 'id')
.addSelect('COUNT(images.id)', 'imagesCount')
.groupByTyped(user => user.id)
// same as `query.groupBy('user.id').addGroupBy('user.email')`
query.groupByTyped(user => [user.id, user.email])
// same as `query.addGroupBy('images.url')`
query.addGroupByTyped(user => user.images.url)
// same as `query.groupBy('"ticket"."ticket_status"')`
query.groupByTyped(ticket => ticket.status, { useQuotes: true })