prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
38.59k stars 1.51k forks source link

Next.js date serialization in getServerSideProps #5399

Closed lcswillems closed 3 years ago

lcswillems commented 3 years ago

Problem

I have a table Post with a field publishedAt of time DateTime. The Javascript type given to this field is Date. However, I would like it to be string or even number (timestamp). How could I do this without having to manually map the field?

Additional context

I use Next.js. In getServerSideProps, I request latest posts and return them. However, as the publishedAt field is of type Date, I get an error from Next.js saying it is not able to stringify it.

Jolg42 commented 3 years ago

So Next.js cannot serialize dates, you'll need to use https://github.com/blitz-js/superjson#using-with-nextjs (there is a Next.js example in the docs).

Related https://github.com/vercel/next.js/discussions/11498#discussioncomment-146987

That should work, let me know if if this should be opened again.

lcswillems commented 3 years ago

Thank you @Jolg42 for your answer! It is what I ended up doing.