How can I store only “Time” with prisma in mongodb
# orm-help
e
How can I store only “Time” with prisma in mongodb
r
You need to create a custom scalar
e
type BusinessDay @embedded { // Day start in 0 day: Int openTime: DateTime closeTime: DateTime }
businessHours: [BusinessDay!]
r
Not ideal but you may store it as a string and then convert it to time when you pull it from prisma
that should work unless you need to do calculations on it “inside” a query.
openTime: "08:00", closeTime: "17:00"
for display purposes that should work well ..
e
but string may cause error when I query for companies which are open at a specific time
r
yeah - if you need to use it inside a query then I’m unsure ..
e
why not use DateTime
r
For the use case of display a time only you could use String. For being able to store Time without the Date you can try use
custom scalars
. Would
DateTime
work? It should but you’re storing Date too.. (that may not be an issue for your use case)
e
how to create custom scalar
can u put a hint
r
<https://lmgtfy.com/?q=prisma+custom+scalar&s=g>
e
oh 🤣
😂 1
so how these custom scalar map to mongodb type
not found any good resource
So I think if I represent Time as Float it will solve my problem
“2:45” => 2.45
r
👍
Sorry - I haven’t used the custom scalars yet (only know of them) and also not using mongo..
but if floats work then that’s good! 🙂
s
Just chiming in with my two cents: I’d use a float/integer to store the time in milliseconds rather than the interpolation you’re thinking of @Ehsan sarshar Then you can format it as a “Time” when you want to display it (in a UI I presume)
👍 1
e
Humm! using milliseconds for storing time is the final solution and will end this issue