prisma bigint to number

00_8_3·2022년 7월 15일
0

Migration To Prisma

목록 보기
2/11

Prisma에서 bigint

...

model users {
    id               BigInt             @id @default(autoincrement()) @db.UnsignedBigInt
    email            String             @unique(map: "email") @db.VarChar(40)
    password         String?            @db.VarChar(1000)
}

mysql 스키마의 id를 bigint로 정의 후
콘솔을 찍어 보면

const user = await prisma.users.findUnique({where: {id: 1}});
console.log("user :", user);

아래와 같이 id 값에 js의 bigint로 나오는 것을 볼 수 있다.

user : {
      id: 1n,
      email: 'ehgks0083@gmail.com',
      user_metas: [ { id: 1n, is_verified: false, type: 'seeker', user_id: 1n } ],
      profiles: [
        { id: 1n, name: null, address: null, birthday: null, user_id: 1n }
      ]
    }

해결방법

공식 문서를 보니 아래와 같이 stringfy 해서 사용하라고 하는데
큰 데이터들이 들어온다면 성능면에서 문제가 없을지 의문이다.

...

//const user = ... ;

const updatedData = JSON.stringify(user, (key, value) => (typeof value === "bigint" ? value = Number(value.toString()) : value));
    
console.log("prisma updatedData :", JSON.parse(updatedData));

참고

https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields#working-with-bigint

https://github.com/prisma/studio/issues/614

0개의 댓글