Files
payload/packages/db-mongodb/src/updateGlobalVersion.ts
Alessio Gravili 5c16443431 fix: ensure updates to createdAt and updatedAt are respected (#13335)
Previously, when manually setting `createdAt` or `updatedAt` in a
`payload.db.*` or `payload.*` operation, the value may have been
ignored. In some cases it was _impossible_ to change the `updatedAt`
value, even when using direct db adapter calls. On top of that, this
behavior sometimes differed between db adapters. For example, mongodb
did accept `updatedAt` when calling `payload.db.updateVersion` -
postgres ignored it.

This PR changes this behavior to consistently respect `createdAt` and
`updatedAt` values for `payload.db.*` operations.

For `payload.*` operations, this also works with the following
exception:
- update operations do no respect `updatedAt`, as updates are commonly
performed by spreading the old data, e.g. `payload.update({ data:
{...oldData} })` - in these cases, we usually still want the `updatedAt`
to be updated. If you need to get around this, you can use the
`payload.db.updateOne` operation instead.

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1210919646303994
2025-08-22 08:41:07 -04:00

70 lines
1.9 KiB
TypeScript

import type { MongooseUpdateQueryOptions } from 'mongoose'
import { buildVersionGlobalFields, type TypeWithID, type UpdateGlobalVersionArgs } from 'payload'
import type { MongooseAdapter } from './index.js'
import { buildQuery } from './queries/buildQuery.js'
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getGlobal } from './utilities/getEntity.js'
import { getSession } from './utilities/getSession.js'
import { transform } from './utilities/transform.js'
export async function updateGlobalVersion<T extends TypeWithID>(
this: MongooseAdapter,
{
id,
global: globalSlug,
locale,
options: optionsArgs = {},
req,
returning,
select,
versionData,
where,
}: UpdateGlobalVersionArgs<T>,
) {
const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })
const whereToUse = where || { id: { equals: id } }
const fields = buildVersionGlobalFields(this.payload.config, globalConfig)
const flattenedFields = buildVersionGlobalFields(this.payload.config, globalConfig, true)
const options: MongooseUpdateQueryOptions = {
...optionsArgs,
lean: true,
new: true,
projection: buildProjectionFromSelect({
adapter: this,
fields: flattenedFields,
select,
}),
session: await getSession(this, req),
// Timestamps are manually added by the write transform
timestamps: false,
}
const query = await buildQuery({
adapter: this,
fields: flattenedFields,
locale,
where: whereToUse,
})
transform({ adapter: this, data: versionData, fields, operation: 'write' })
if (returning === false) {
await Model.updateOne(query, versionData, options)
return null
}
const doc = await Model.findOneAndUpdate(query, versionData, options)
if (!doc) {
return null
}
transform({ adapter: this, data: doc, fields, operation: 'read' })
return doc
}