sql magic operator with dates?

We were using drizzle orm but We implemented by ourselves a "multi-update" query to update fields based on different primary keys using the CASE ... THEN postgres syntax, this forced us to use the sql magic operator. Is there a way to integrate the sql magic operator with the timestamp type or with other custom types?
sql`WHEN ${where} THEN ${value}`
sql`WHEN ${where} THEN ${value}`
It seems that doing something like this does not apply the correct parsing on timestamps or defined custom types. Date in javascript doesnt use the correct toString function and the update expects a string so we had to manually check for a date object and apply .toISOString()
2 Replies
Angelelz
Angelelz2mo ago
When you use the sql operator you have to do that manually But there is an undocumented method in the sql operator to make this type of stuff easier to operate You could use it like this:
sql`WHEN ${where} THEN ${sql.param(value, users.createdAt)}`
sql`WHEN ${where} THEN ${sql.param(value, users.createdAt)}`
In the case when you want to use the decoder in the createdAt column of the users table
Want results from more Discord servers?
Add your server