Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- angular route
- egov spring ajax 사용 예시
- Ionic modal
- 옵저버블
- TAILWIND
- Router
- mysql if
- formgroup
- 스크롤 이벤트 감지
- 아이오닉 스크롤 이벤트
- 검색
- scroll
- 스크롤 이벤트
- angular button
- summary
- 앵귤러 모달
- angular animation
- flex-1
- Oracle LISTAGG 사용 예시
- 셀렉트박스 커스텀
- 앵귤러 애니메이션
- prisma
- Angular Router
- 모달
- angular modal
- 호버
- ApexChart
- modal
- route
- ajax 사용 예시
Archives
- Today
- Total
깜놀하는 해므찌로
nestjs search 검색 controller service 기본 구조 예시 feat.Prisma 본문
반응형
SMALL
export type PageInfo = {
itemsCount: number;
totalItems: number;
itemsPerPage: number;
totalPages: number;
currentPage: number;
};
export class PaginationDTO<T> implements IPaginationDTO<T> {
items: T[];
pageInfo: PageInfo;
}
export class PaginationOptionDTO implements IPaginationOptionDTO {
@ApiProperty()
@IsNotEmpty()
@Type(() => Number)
pageNo: number;
@ApiProperty()
@IsNotEmpty()
@Type(() => Number)
pageSize: number;
@ApiProperty()
@IsNotEmpty()
orderBy: string;
@ApiProperty()
@IsNotEmpty()
align: 'asc' | 'desc';
@ApiProperty()
@IsOptional()
query?: string;
}
1. type, interface
export class PaginationDTO<T> implements IPaginationDTO<T> {
items: T[];
pageInfo: PageInfo;
}
export class PaginationOptionDTO implements IPaginationOptionDTO {
@ApiProperty()
@IsNotEmpty()
@Type(() => Number)
pageNo: number;
@ApiProperty()
@IsNotEmpty()
@Type(() => Number)
pageSize: number;
@ApiProperty()
@IsNotEmpty()
orderBy: string;
@ApiProperty()
@IsNotEmpty()
align: 'asc' | 'desc';
@ApiProperty()
@IsOptional()
query?: string;
}
export const generatePageInfo = (
count: number,
itemLength: number,
pageSize: number,
pageNo: number
): PageInfo => ({
currentPage: pageNo,
itemsCount: itemLength,
itemsPerPage: pageSize,
totalItems: count,
totalPages: Math.ceil(count / pageSize),
});
2. DTO <T> 제네릭을 받아 해당 타입의 배열로 프로퍼티 설정
@ApiTags('관리자')
@Controller({ version: '1' })
export class AdminController {
constructor(private readonly adminService: AdminService) {}
@Get('search')
@ApiOperation({
summary: '관리자 조회',
description: '검색어와 일치하는 관리자 목록을 조회합니다.',
})
async searchAdmins(@Query() option: PaginationOptionDTO) {
const [entities, count] = await this.adminService.search(option);
const result: PaginationDTO<AdminDTO> = {
items: plainToInstance(AdminDTO, entities),
pageInfo: generatePageInfo(
count,
entities.length,
option.pageSize,
option.pageNo
),
};
return result;
}
3. controller: search 검색 요청 받는 부분 (PaginationDTO<T>) 제네릭 활용
async search(option: PaginationOptionDTO): Promise<[Admin[], number]> {
const where: Prisma.AdminWhereInput = {
username: {
contains: option.query,
},
};
const orderBy: Prisma.AdminOrderByWithRelationInput = {};
orderBy[option.orderBy] = option.align;
const count = await this.prismaService.admin.count({ where });
const entities = await this.prismaService.admin.findMany({
where: { ...where },
orderBy,
take: option.pageSize, // items per Page
skip: (option.pageNo - 1) * option.pageSize,
});
return [entities, count];
}
4. service: prisma 에 접근해 데이터 조회
반응형
LIST
'IT' 카테고리의 다른 글
Prisma foreign key 배열 조회하여 등록하기 예시 / prisma connect (0) | 2023.08.18 |
---|---|
Ionic Anular nestjs video Upload example / 앵귤러 nestjs 동영상 파일 업로드 예시 feat.AWS S3 (0) | 2023.08.17 |
Angular 컴포넌트 cli 생성 시 앞에 이름 / prefix 수정하기 (0) | 2023.08.15 |
CSS tag 예시 / tailwind 태그 줄바꿈 (0) | 2023.08.14 |
Nestjs 비밀번호 / 비밀번호 확인과 같이 클래스 내 다른 property와 일치한 지 검증하고 싶을 때 (0) | 2023.08.13 |