[오류] Prisma DTO 에서 bigint 다루기

1. prisma schema

model u_user {
    u_idx           BigInt                 @id @default(autoincrement())
    u_active_yn     u_user_u_active_yn     @default(true)
    u_group_auth_yn u_user_u_group_auth_yn @default(true)
    update_dt       BigInt?
}

 

 

2. user.dto.ts

export class UserDTO {
    @Expose()
    @IsInt()
    @ApiProperty()
    u_idx: bigint

    @Expose()
    @IsBoolean()
    @ApiProperty()
    u_active_yn: string

    @Expose()
    @IsBoolean()
    @IsNotEmpty()
    @ApiProperty()
    u_group_auth_yn: string

    @Expose()
    @IsDate()
    @ApiProperty()
    update_dt: bigint
}

 

 

3. user.reposirory

createUser = (user: CreateUserDTO): Observable<u_user> => {
        return from(
            this.OstreamDB.u_user.create({
                data: {
                    u_idx: user.u_idx,
                    insert_id: user.insert_id,
                },
            })
        )
    }

create를 만들고보니 u_idx의 타입이 prisma 테이블은 BigInt고, DTO는 bigint여서 맞지 않았다.

 

Try1 DTO의 타입을 BigInt로 변경하기

prisma create가 u_idx?: bigint | number로 값을 받고있어서 BigInt로 받아지지 않았다.

 

Try2 DTO의 타입을 Number로 변경하기

export class UserDTO {
    @Expose()
    @IsInt()
    @Transform(({ value }) => Number(value))
    @ApiProperty()
    u_idx: number

    @Expose()
    @IsDate()
    @Transform(({ value }) => Number(value))
    @ApiProperty()
    update_dt: number
}

 

DTO 내부의 bigint를 쓰는 필드의 자료형을 class-transformer의 Transform을 이용하여 형변환을 해줬다.

이렇게 사용하면 prisma에서 값을 받아올때도 plainToInstance 처리를 해주면 bigint 오류가 나지 않는다.

'Back-End > Nest.js' 카테고리의 다른 글

DTO 내부 Validation  (0) 2023.08.15