type OrderParam
Description
OrderParam<Entity> is a type that represents an order configuration.
Definition
Defaults:
direction = 'ASC'nulls = undefined(database default)
type OrderParam<Entity> = {
field: NonFunctionPropertyNames<Required<Entity>>;
direction?: 'asc' | 'desc' | 'ASC' | 'DESC' | 1 | -1 | true | false;
nulls?: 'first' | 'last' | 'FIRST' | 'LAST' | 'NULLS FIRST' | 'NULLS LAST' | 'nulls first' | 'nulls last';
isReversed?: boolean;
tableAlias?: string;
};
field: entity property to order by.direction:'asc'/'desc'in any case;1isASC,-1isDESC;trueisASC,falseisDESC. The same values as query-string text ('-1','false') work too, as an unparsed query parameter holds them. Anything else isASC.nulls:NULLS FIRST/NULLS LAST, given asfirst/lastor in the SQL form, in any case and with spaces around. Any other value adds noNULLS. MySQL and MariaDB have noNULLS: there(column) IS NULLis selected under a generatednulls_…alias and ordered by first, so raw rows carry that column.isReversed: used only whendirectionis not set:trueand1areDESC,falseand0areASC, also as query-string text ('true','1','false','0').tableAlias: alias to prefix the field with, e.g. a joined relation's alias. Takes precedence overApplyOrderOptions.alias.
Example
@Entity()
class UserEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@CreateDateColumn()
createdAt: Date;
}
const orderBy: OrderParam<UserEntity> = { field: 'createdAt', direction: 'asc', nulls: 'last' };
// newest first
const newest: OrderParam<UserEntity> = { field: 'createdAt', isReversed: true };
// order by a column of a joined relation
query
.leftJoin('user.images', 'images')
.applyOrder<ImageEntity>({ field: 'url', direction: 'desc', tableAlias: 'images' });