Davepar
Davepar
DTDrizzle Team
Created by rgripper on 2/16/2024 in #help
Force parameter to be of certain type
This is how I defined a custom type that converts Postgres timestamp to Temporal.PlainDateTime:
const temporalTimestamp = customType<{
data: Temporal.PlainDateTime;
driverData: string;
config?: { precision: number };
}>({
dataType(config) {
return `timestamp(${config?.precision ?? 0})`;
},
fromDriver(value: string): Temporal.PlainDateTime {
// This hack is for Drizzle Studio, which seems to always convert timestamps to Dates.
if ((value as unknown as Date).toISOString) {
value = (value as unknown as Date).toISOString().slice(0, -1);
}
return Temporal.PlainDateTime.from(value);
},
toDriver(value: Temporal.PlainDateTime): string {
return value.toString() + 'Z';
},
});
const temporalTimestamp = customType<{
data: Temporal.PlainDateTime;
driverData: string;
config?: { precision: number };
}>({
dataType(config) {
return `timestamp(${config?.precision ?? 0})`;
},
fromDriver(value: string): Temporal.PlainDateTime {
// This hack is for Drizzle Studio, which seems to always convert timestamps to Dates.
if ((value as unknown as Date).toISOString) {
value = (value as unknown as Date).toISOString().slice(0, -1);
}
return Temporal.PlainDateTime.from(value);
},
toDriver(value: Temporal.PlainDateTime): string {
return value.toString() + 'Z';
},
});
All timestamps in my db are UTC, so I don't store timezone. It's working great so far, but notice the hack to get around a bug in Drizzle Studio. It seems to always convert db timestamps to js Date type.
2 replies