Menu

Our updated_at is maintained by the ORM, not the database, and 10 of 25 tables have one
📰

Our updated_at is maintained by the ORM, not the database, and 10 of 25 tables have one

DEV Community: typescript·Daniel Pertu·about 16 hours ago
#Rt7y9X9R
#devtotypescript#devto#dev#updated_at#count
Reading 0:00
15s threshold

Two helpers at the top of Nakodo 's schema file decide how every row in the database records its own age:

const createdAt = () => timestamp ({ withTimezone : true }). notNull (). defaultNow (); const updatedAt = () => timestamp ({ withTimezone : true }) . notNull () . defaultNow () . $onUpdate (() => new Date ());

Enter fullscreen mode

Exit fullscreen mode

They look like a pair. They are not. defaultNow() compiles into the table definition and is executed by Postgres. $onUpdate compiles into nothing at all: it is a JavaScript callback that Drizzle runs while building an UPDATE , adding updated_at = $n to the statement it sends.

You can see the asymmetry in the generated migration, which is the only SQL that exists for these columns:

"created_at" timestamp with time zone DEFAULT now () NOT NULL , "updated_at" timestamp with time zone DEFAULT now () NOT NULL ,

Enter fullscreen mode

Exit fullscreen mode

$ grep -il "create trigger \| create function" drizzle/ * .sql $ echo $?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.