Skip to main content

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 a TypeError: for a JSON value use tableHelper json / jsonText. A @CalculatedColumn is ordered by its value, which has to be selected with addSelectCalculated / addSelectCalculatedTyped first (a TypeError otherwise); options don't apply to it. In place of the callback, a selectExpression(sql) (from typeorm-extensions/helpers) orders by an SQL expression with no property to select it into: it is selected once under a generated alias, which getMany leaves out of the entities, and distinctOnTyped takes the same value.
  • order?: 'ASC' | 'DESC': Order direction. Default is 'ASC'.
  • nulls?: 'NULLS FIRST' | 'NULLS LAST': NULLS ordering.
  • options?: TypedOrderByOptions: { useQuotes?: boolean }. With useQuotes: true the key is the quoted database column ("user"."updated_at", @Column({ name }) applied) instead of user.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 unless useQuotes: false is passed: TypeORM leaves its keys as written, and PostgreSQL folds an unquoted recentEvents.startsAt to recentevents.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 (a LATERAL subquery 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) is userid: quote it in the SQL (AS "userId") or pass useQuotes: false. Replaces the useDoubleQuotes argument 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 })