I have a nestjs app using prisma, with following postgresql table:
Table "public.Transaction"
Column | Type |
-------+-------------------+
id | integer |
amount | double precision |
date | date |
In prisma, I defined the table as this:
model Transaction {
id Int @id @default(autoincrement())
amount Float
date DateTime @db.Date
Since I am using postgresql, I need to pass a date with 'YYYY-MM-DD' format.
I am trying to pass this format as a string:
const createdTransaction = await this.prisma.transaction.create({
data: {
amount: 3333,
date: '2022-11-22',
},
});
But I get following error:
Argument date: Got invalid value '2022-11-22' on prisma.createOneTransaction. Provided String, expected DateTime.
I know I have to pass a Date type, but using the javascript formatting, everything ends up in a string.
What value should I pass to date within nestjs?
Because if I use new Date(2022,11,22), it is accepted by prisma, but this is stored as Thu Dec 22 2022 00:00:00 GMT+0100 (Central European Standard Time), a string, which is not what postgresql expects.
I think you can just make the type of the date column string in your Transaction model.
This way you can pass the date string to PostgreSQL.
Another way is maybe to make the type number and pass the milliseconds date to PostgreSQL.
In your service before returning the object or in your frontend you can parse the date back to a string.