Retrieve date(only) column as plain string without added time information (or custom type, in general)
See original GitHub issueProblem
I’m having a date column in PostgreSQL. I.e. it only contains the date, no time, no timezone. In Prisma schema I have to define:
date DateTime @db.Date
In my API I only want to specify the date without time. I have to write a lot of nasty transformation, each time this column is read from or written to the database, e.g.
return await prisma.entity.findMany().map(e => ({ ...e, date: e.date.toISOString().substring(0,10) }))
Suggested solution
AFAIK there is no native “dateonly” type in Javascript. I’d really appreciate, if I could specify in the schema that this column should be handled as string:
date String @db.Date
Alternative: Transformer
An even more powerful alternative would be to allow transformers (e.g. using class-transformer) for each column to be specified, that are executed when transferring data from and to the database. This would allow users to have their columns transformed to the type they like to work with.
Alternative: Middleware
It was suggested (https://stackoverflow.com/questions/66109340/retrieve-dateonly-column-as-string-or-map-to-string-without-time-using-prisma) to use middlewares for this purpose. While this works in principle, it breaks the type checking. And the strong type checking is one of the biggest advantages of Prisma over alternative solutions. So, I don’t want to lose that.
Example:
prisma.$use(async (params, next) => {
if (
params.model === 'Model' &&
params.action === 'findMany'
) {
const result = await next(params)
result.forEach((element) => {
if (element.date) {
element.date = element.date.toISOString().substring(0, 10)
}
})
return result
}
return next(params)
})
prisma.model.findMany will still tell you the date property is Date while it actually is String. Also it is tedious to write transformation for all operations, where this model might be included. I’d prefer to declare this once for a column.
Additional context
This problem might be applicable to a wide range of column types where it would be nice to handle it in a different type than Prisma suggests.
E.g. users could want to handle Datetime as instances of MomentJs or DayJs instead of native Date. Or they would like to handle Decimal as String or Number or BigJs. They might have custom implementations db.Xml, inet, cidr, …
Please let me know, if I should add more information etc. Or there are simple solutions, I might have missed 😃
Issue Analytics
- State:
- Created 3 years ago
- Reactions:25
- Comments:6

Top Related StackOverflow Question
I think Prisma should completely avoid JavaScript’s native
Dateobject and treat dates as regular strings (or at least Date ISO strings).Converting dates to instances of
Datesometimes adds unwanted precision:2021-01-01is timezone agnostic and carries a different date information than2021-01-01T00:00:00.000ZThere are also other issues caused by this behavior: https://github.com/prisma/prisma/issues/7490 https://github.com/prisma/prisma/issues/4355
On my end, this causes problems & confusion when parsing time in the browser.
I had to work around it by adding a Prisma middleware to sanitize the date values.