kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.79k stars 274 forks source link

MYSQL: kysely converting the datetime field to UTC while fetching data from database #1172

Closed stuvan closed 1 month ago

stuvan commented 1 month ago

This is my query

 const query = sql`select id, registration_start_time, registration_end_time, timezone from \`scrim\``;
      const records = await query.execute(this.db);

the returned result image

only column in my table image

The data from mysql cli image

Look like kysely converting my date to UTC while fetching result and also on inserting or updating data

AS I reside in the land of +05:30 kysely subtracting the 5 hours and 30 minutes.

Any workaround for now?

koskimas commented 1 month ago

Kysely doesn't touch the data in any way. This is done by the mysql2 driver.

And nothing is subtracted from anything. The Date objects are just shown in the UTC time zone. That's exactly the same date and time.

stuvan commented 1 month ago

This is done by the mysql2 driver

In the enchanted realm of data management, it’s the mysql2 driver that handles this task. For future adventurers seeking guidance, remember to set your timezone properly. As I was converting my datetime to UTC before storing it in the database.

Here’s the solution that worked for mr:

 new MysqlDialect({
        pool: createPool({
          uri: process.env.DATABASE_URL,
          database: process.env.DATABASE_NAME,
          timezone: 'Z', // <---- Set default timezone, in my case UTC
        }),
      }),

This way, your data will flow smoothly through time!