If I want to work with a date attribute as part of...
# orm-help
k
If I want to work with a date attribute as part of my model, would you suggest just storing it as a string or store it as a DateTime (I don’t need the time part)
1
s
Definitely DateTime! You should always try store data in their respective typed format. Maybe you don’t need it yet, but one day you might want to query/filter/sort/manipulate your data based on the date. So storing it as a proper date will give you access to a lot of convenient built-in functionalities.
👍 1
k
@Sylvain (seel-vahn) In terms of inserting the data as a DateTime into my database. Do I need to convert the string date (since I receive as a string from the client json) into a
new Date
before I insert it?
Or can I just call insert using the string date and Prisma handles storing it as a DateTIme automatically?
r
I would store them as DateTime. Like what @Sylvain (seel-vahn) mentioned, you may don't need it for now but it will help you someday. A little story, in my company we have millions of data for date with string type in MongoDB because lack awareness of the design, and after years we need a feature which requires date filtering. We may change the date immediately but we can't do anything with the past data.
s
If I’m not mistaken, I think either would work - as Prisma client can accept both Javascript dates (using
new Date
) and strings (as long as they are following the ISO 8601 DateTime format).
If your date is coming as a String (but different format than ISO 8601) - you can use a small library like date-fns to convert it: https://date-fns.org/v2.8.1/docs/formatISO
k
@radisa Sounds like a nightmare I’d like to avoid. Thanks for the insight haha
I’ll store as DateTime and as a native Date inside of the database like
Copy code
dateOfBirth    DateTime? @db.Date
@Sylvain (seel-vahn) It seemed to work the same either by passing (using
new Date
) or by just passing the string in ISO 8601 format.
👍 2
Convenient