orderByTyped
Description
orderByTyped is a method that applies typesafe order to a query. addOrderByTyped adds an order to the existing ones.
Signature
orderByTyped(
selection: KeyProxyCallback<Entity>,
order?: 'ASC' | 'DESC',
nulls?: 'NULLS FIRST' | 'NULLS LAST',
options?: TypedOrderByOptions
): SelectQueryBuilder<Entity>
addOrderByTyped(
selection: KeyProxyCallback<Entity>,
order?: 'ASC' | 'DESC',
nulls?: 'NULLS FIRST' | 'NULLS LAST',
options?: TypedOrderByOptions
): SelectQueryBuilder<Entity>
Arguments
- selection: KeyProxyCallback<Entity>: The callback which selects fields of entity. A path into a JSON column (
user => user.metadata.role) throws aTypeError: for a JSON value use tableHelperjson/jsonText. A @CalculatedColumn is ordered by its value, which has to be selected withaddSelectCalculated/addSelectCalculatedTypedfirst (aTypeErrorotherwise);optionsdon't apply to it. In place of the callback, aselectExpression(sql)(fromtypeorm-extensions/helpers) orders by an SQL expression with no property to select it into: it is selected once under a generated alias, whichgetManyleaves out of the entities, anddistinctOnTypedtakes the same value. - order?: 'ASC' | 'DESC': Order direction. Default is 'ASC'.
- nulls?: 'NULLS FIRST' | 'NULLS LAST': NULLS ordering.
- options?:
TypedOrderByOptions:{ useQuotes?: boolean }. WithuseQuotes: truethe key is the quoted database column ("user"."updated_at",@Column({ name })applied) instead ofuser.updatedAt. Without it:user.updatedAt, 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. Replaces theuseDoubleQuotesargument of 1.3.x.
Usage
// same as `query.orderBy('user.updatedAt', 'DESC')`
query.orderByTyped(user => user.updatedAt, 'DESC')
// same as `query.addOrderBy('user.name', 'ASC')`
query.addOrderByTyped(user => user.name, 'ASC')
// columns of joined relations, including array relations
// same as `query.orderBy('images.url', 'ASC', 'NULLS LAST')`
query
.leftJoinTyped(user => user.images, 'images')
.orderByTyped(user => user.images.url, 'ASC', 'NULLS LAST')
// same as `query.addOrderBy('"user"."name"', 'DESC', 'NULLS LAST')`
query.addOrderByTyped(user => user.name, 'DESC', 'NULLS LAST', { useQuotes: true })