Files
prosapp-migration/backend/src/common/dto/pagination.dto.ts
T

48 lines
846 B
TypeScript

import { IsOptional, IsInt, Min, Max } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiPropertyOptional } from '@nestjs/swagger';
export class PaginationDto {
@ApiPropertyOptional({ default: 1 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page?: number = 1;
@ApiPropertyOptional({ default: 20 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(100)
limit?: number = 20;
}
export interface PaginatedResult<T> {
data: T[];
meta: {
total: number;
page: number;
limit: number;
totalPages: number;
};
}
export function paginate<T>(
data: T[],
total: number,
page: number,
limit: number,
): PaginatedResult<T> {
return {
data,
meta: {
total,
page,
limit,
totalPages: Math.ceil(total / limit) || 1,
},
};
}