Nestjs/Prisma not saving date number correctly

Viewed 16

When I console log the response body right before I save it to the database, my response body shape looks correct. See below


//console.log response body

CreateOpenHourDto {
day: 'WEDNESDAY',
startTime: 1663858800000,
endTime: 1663878600000,
calendarId: 1
}

However, whenever I go into prisma studio and check the new db entry, the startTime, endTime is differnt.

enter image description here

There is nothing I have done to transform the data. Any tips are appreciated.

I am using nestjs, prisma, postgres sql

1 Answers

My prisma model listed start and end times as "int" types and it should have been BigInt. For anyone that plans on using bigint. Be aware

Prisma returns records as plain JavaScript objects. If you attempt to use JSON.stringify on an object that includes a BigInt field, you will see the following error:

Do not know how to serialize a BigInt To work around this issue, use a customized implementation of JSON.stringify:

JSON.stringify(
  this,
  (key, value) => (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged
)

This sounds hacky but its coming straight form the documentation as of the time of this comment.

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

Related