Skip to content

Commit 74b85ae

Browse files
committed
Fixed bun-sql:postgresql date, timestamp mappers not accounting for Date instances in driver response, updated changelogs
1 parent adf9bf1 commit 74b85ae

4 files changed

Lines changed: 522 additions & 9 deletions

File tree

‎changelogs/drizzle-orm/0.45.0.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
- Fixed pg-native Pool detection in node-postgres transactions
22
- Allowed subqueries in select fields
3-
- Updated typo algorythm => algorithm
3+
- Updated typo algorythm => algorithm
4+
- Fixed `$onUpdate` not handling `SQL` values (fixes [#2388](https://github.com/drizzle-team/drizzle-orm/issues/2388))
5+
- Fixed `pg` mappers not handling `Date` instances in `bun-sql:postgresql` driver responses for `date`, `timestamp` types (fixes [#4493](https://github.com/drizzle-team/drizzle-orm/issues/4493))

‎drizzle-orm/src/pg-core/columns/date.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export class PgDate<T extends ColumnBaseConfig<'date', 'PgDate'>> extends PgColu
3737
return 'date';
3838
}
3939

40-
override mapFromDriverValue(value: string): Date {
41-
return new Date(value);
40+
override mapFromDriverValue(value: string | Date): Date {
41+
if (typeof value === 'string') return new Date(value);
42+
43+
return value;
4244
}
4345

4446
override mapToDriverValue(value: Date): string {
@@ -81,6 +83,12 @@ export class PgDateString<T extends ColumnBaseConfig<'string', 'PgDateString'>>
8183
getSQLType(): string {
8284
return 'date';
8385
}
86+
87+
override mapFromDriverValue(value: Date | string): string {
88+
if (typeof value === 'string') return value;
89+
90+
return value.toISOString().slice(0, -14);
91+
}
8492
}
8593

8694
export interface PgDateConfig<T extends 'date' | 'string' = 'date' | 'string'> {

‎drizzle-orm/src/pg-core/columns/timestamp.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ export class PgTimestamp<T extends ColumnBaseConfig<'date', 'PgTimestamp'>> exte
5858
return `timestamp${precision}${this.withTimezone ? ' with time zone' : ''}`;
5959
}
6060

61-
override mapFromDriverValue = (value: string): Date | null => {
62-
return new Date(this.withTimezone ? value : value + '+0000');
63-
};
61+
override mapFromDriverValue(value: Date | string): Date {
62+
if (typeof value === 'string') return new Date(this.withTimezone ? value : value + '+0000');
63+
64+
return value;
65+
}
6466

6567
override mapToDriverValue = (value: Date): string => {
6668
return value.toISOString();
@@ -121,6 +123,19 @@ export class PgTimestampString<T extends ColumnBaseConfig<'string', 'PgTimestamp
121123
const precision = this.precision === undefined ? '' : `(${this.precision})`;
122124
return `timestamp${precision}${this.withTimezone ? ' with time zone' : ''}`;
123125
}
126+
127+
override mapFromDriverValue(value: Date | string): string {
128+
if (typeof value === 'string') return value;
129+
130+
const shortened = value.toISOString().slice(0, -1).replace('T', ' ');
131+
if (this.withTimezone) {
132+
const offset = value.getTimezoneOffset();
133+
const sign = offset <= 0 ? '+' : '-';
134+
return `${shortened}${sign}${Math.floor(Math.abs(offset) / 60).toString().padStart(2, '0')}`;
135+
}
136+
137+
return shortened;
138+
}
124139
}
125140

126141
export type Precision = 0 | 1 | 2 | 3 | 4 | 5 | 6;

0 commit comments

Comments
 (0)