Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mysql]: (Feature) Added utc to datetime config. #1082

Merged
merged 8 commits into from
Sep 5, 2023

Conversation

Angelelz
Copy link
Collaborator

This PR aims to address #920

  • Added a new boolean config utc to set how drizzle-orm should treat the Date you pass and receive from the database.
  • All defaults are kept the same
  • Added tests

In my opinion we should evaluate to add this option to both timestamp and date data types.
Any feedback would be appreciated.

Angelelz and others added 3 commits August 20, 2023 15:24
New configuration option added for Mysql datetime type, for date conversion to Date JS object. drizzle-team#920
Copy link
Contributor

@dankochetov dankochetov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just always use UTC for now and remove the utc flag from the config. We'll convert the values to UTC on insert and construct Date objects using UTC on select.

We discussed this internally and decided to do it in two steps - first, fix the ongoing issue with timezone mismatch, and then add an offset parameter that would specify the timezone in which the dates are/should be stored in the DB. The second step is outside the scope of this PR.

…UTC as the defaul config for Datetime

Closes drizzle-team#920 by setting UTC as the default way to tranform the Date from sql to JS.
@Angelelz
Copy link
Collaborator Author

The requested changes were applied.
I believe we should add this to the breaking changes on the next release as we don't know if anybody has worked around this issue (Or if it was considered a feature by anybody).

  • Breaking changes: The Datetime Mysql data type when in { mode: 'Date' } is now transformed to UTC timezone on insert and select to align with the way Datetime is handled in Mysql.

@Angelelz Angelelz requested a review from dankochetov August 28, 2023 02:54
export interface MySqlDatetimeConfig<TMode extends 'date' | 'string' = 'date' | 'string'> {
mode?: TMode;
fsp?: DatetimeFsp;
// If user specifies mode: 'date'. the utc option will be required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment left from the previous implementation

mode?: TMode;
fsp?: DatetimeFsp;
// If user specifies mode: 'date'. the utc option will be required
export type MySqlDatetimeConfig<TMode extends "date" | "string" = "date" | "string"> = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you make this change?

@@ -89,7 +92,7 @@ export class MySqlDateTimeStringBuilder<T extends ColumnBuilderBaseConfig<'strin
}
}

export class MySqlDateTimeString<T extends ColumnBaseConfig<'string', 'MySqlDateTimeString'>> extends MySqlColumn<T> {
export class MySqlDateTimeString<T extends ColumnBaseConfig<'string', 'MySqlDateTimeString'>> extends MySqlColumn<T, MySqlDatetimeConfig> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you make this change?

@@ -1967,3 +1967,46 @@ test.serial('update undefined', async (t) => {

await db.execute(sql`drop table ${users}`);
});

test.serial('utc config for datetime', async (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a check to this test that verifies the actual value that's put into the DB with mode: "date"? You could use a raw select query to get the data bypassing the mapping.

)
`,
);
const datesTable2 = mysqlTable('datestable', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use dprint to format all the files you've changed.

static readonly [entityKind]: string = 'MySqlDateTimeBuilder';

constructor(name: T['name'], config: MySqlDatetimeConfig | undefined) {
constructor(name: T['name'], config: MySqlDatetimeConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you make this change?

@@ -35,7 +34,7 @@ export class MySqlDateTimeBuilder<T extends ColumnBuilderBaseConfig<'date', 'MyS
}
}

export class MySqlDateTime<T extends ColumnBaseConfig<'date', 'MySqlDateTime'>> extends MySqlColumn<T> {
export class MySqlDateTime<T extends ColumnBaseConfig<'date', 'MySqlDateTime'>> extends MySqlColumn<T, MySqlDatetimeConfig> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you make this change?

@Angelelz Angelelz requested a review from dankochetov August 30, 2023 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants