Allow updatedAt
to be set in create + update data payload
#11423
sebastianmulders
started this conversation in
Feature Requests & Ideas
Replies: 1 comment 2 replies
-
Something like |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Context
Currently,
updatedAt
is always overridden withnew Date().toISOString()
bytraverseFields.ts
, preventing it from being explicitly set when creating or updating documents. While this behavior makes sense for most cases, it creates challenges when performing database migrations, where retaining the originalupdatedAt
timestamps is crucial.My Attempt & Realization
I initially created a PR to modify
traverseFields.ts
, allowingupdatedAt
to be explicitly set in the payload while keeping the current default behavior intact if no value is provided. However, after submitting the PR, the test suite failures made me realize that this behavior is intentionally enforced.Here’s the PR: #11407
Challenges with the Current Behavior
Collection Hooks Do Not Work Well for This –
It seems that
traverseFields.ts
is always the last thing that runs, meaning there is no opportunity to modifyupdatedAt
within collection hooks. If I'm missing something here, I'd love clarification on whether there's an existing way to handle this within the framework.Direct DB Insertions as a Workaround? –
Another potential approach would be to perform direct database insertions, bypassing Payload's lifecycle methods entirely. However, this feels like an incomplete solution since it would miss out on other features that Payload and Drizzle ORM provide, such as validation, access control, and hooks.
Discussion: Should This Be Configurable?
Since the current behavior is by design, I’d like to open a discussion on whether it could be reconsidered, or if there’s a way to introduce configurability. Possible approaches:
allowCustomUpdatedAt: true
) to enable explicitly settingupdatedAt
when needed.updatedAt
values when necessary.Next Steps
Before revising my PR, I’d love to get input from the PayloadCMS team and the community on whether this is a valid use case worth addressing, and if so, what the best approach would be.
Looking forward to your thoughts!
Beta Was this translation helpful? Give feedback.
All reactions