MySQL DATETIME time-zone conversion #1620
Replies: 3 comments 2 replies
-
I think Drizzle should do a better job in translating data from drivers and make it more transparent to users. A workaround is to override the const table = mysqlTable("table", {
myDatetime: datetime("my_datetime")
})
table.myDatetime.mapFromDriverValue = (val: string) => val; for example. |
Beta Was this translation helpful? Give feedback.
-
Done: |
Beta Was this translation helpful? Give feedback.
-
For those facing the exact same problem and looking for a temporary solution, you can map dates using the following approach: const table = mysqlTable("table", {
myDatetime: datetime("my_datetime")
})
table.myDatetime.mapFromDriverValue = (value: any) => new Date(value)
table.myDatetime.mapToDriverValue = (value: any) => value Another approach would be to create a custom type. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I had already posted a message on another discussion which is closed. It may therefore not receive the attention I would like :)
I'm curious about the reasonning behind the approach you've taken.
When using MySql2 driver, you can specify a timezone configuration at the connection/pool level.
Connection Options
This option makes it possible to properly handle situations where dates in a mysql database are not stored in utc format.
Basically, all dates going to mysql are converted from UTC (internal javascript format) to the timezone configured. And all dates retrieved are converted back to utc before being converted to javascript date (Javascript date constructor expect an input string as being UTC).
I made some tests trying to change the timezone configuration when creating a MySql2 connection but it didn't change anything. I did not check the source but it's like you were short circuiting this option. What didn't you like with this approach ? that seems rather neat.
The problem with your approach is that it makes it painful to migrate existing apps. Many orm/query builders rely on the driver for retrieving data (at least in the MySql space) and therefore complies with this timezone configuration.
For example, I have a codebase using Typeorm/MySql2. Typeorm is completely transparent and will store/retrieve dates through the driver without any specific correction.
Possible Solution without any breaking change
Wouldn't it be possible to introduce a "transparent" or "driver" mode for date fields?
Using this "transparent" or "driver" mode would mean that basically, the driver does its thing without any correction on your side.
Beta Was this translation helpful? Give feedback.
All reactions