Files
Artveris/apps/api/src/common/dto/pagination.dto.ts
T

39 lines
1007 B
TypeScript

import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsIn, IsInt, IsOptional, IsString, Min } from 'class-validator';
export class PaginationDto {
@ApiPropertyOptional({ description: '页码,从 1 开始', default: 1 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page = 1;
@ApiPropertyOptional({ description: '每页条数', default: 10 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(0)
pageSize = 20;
@ApiPropertyOptional({ description: '排序字段' })
@IsOptional()
@IsString()
sortField?: string;
@ApiPropertyOptional({ description: '排序方向', enum: ['ascend', 'descend', 'asc', 'desc'] })
@IsOptional()
@IsIn(['ascend', 'descend', 'asc', 'desc'])
sortOrder?: 'ascend' | 'descend' | 'asc' | 'desc';
get skip(): number {
return (this.page - 1) * this.pageSize;
}
get take(): number | undefined {
if (this.pageSize === 0) return undefined;
return this.pageSize;
}
}