Files
payloadcms/packages/db-mongodb/src/updateMany.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

116 lines
2.8 KiB
TypeScript

import type { MongooseUpdateQueryOptions } from 'mongoose'
import { flattenWhereToOperators, type UpdateMany } from 'payload'
import type { MongooseAdapter } from './index.js'
import { buildQuery } from './queries/buildQuery.js'
import { buildSortParam } from './queries/buildSortParam.js'
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getCollection } from './utilities/getEntity.js'
import { getSession } from './utilities/getSession.js'
import { handleError } from './utilities/handleError.js'
import { transform } from './utilities/transform.js'
export const updateMany: UpdateMany = async function updateMany(
this: MongooseAdapter,
{
collection: collectionSlug,
data,
limit,
locale,
options: optionsArgs = {},
req,
returning,
select,
sort: sortArg,
where,
},
) {
let hasNearConstraint = false
if (where) {
const constraints = flattenWhereToOperators(where)
hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))
}
const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug })
let sort: Record<string, unknown> | undefined
if (!hasNearConstraint) {
sort = buildSortParam({
adapter: this,
config: this.payload.config,
fields: collectionConfig.flattenedFields,
locale,
sort: sortArg || collectionConfig.defaultSort,
timestamps: true,
})
}
const options: MongooseUpdateQueryOptions = {
...optionsArgs,
lean: true,
new: true,
projection: buildProjectionFromSelect({
adapter: this,
fields: collectionConfig.flattenedFields,
select,
}),
session: await getSession(this, req),
// Timestamps are manually added by the write transform
timestamps: false,
}
let query = await buildQuery({
adapter: this,
collectionSlug,
fields: collectionConfig.flattenedFields,
locale,
where,
})
transform({ adapter: this, data, fields: collectionConfig.fields, operation: 'write' })
try {
if (typeof limit === 'number' && limit > 0) {
const documentsToUpdate = await Model.find(
query,
{},
{ ...options, limit, projection: { _id: 1 }, sort },
)
if (documentsToUpdate.length === 0) {
return null
}
query = { _id: { $in: documentsToUpdate.map((doc) => doc._id) } }
}
await Model.updateMany(query, data, options)
} catch (error) {
handleError({ collection: collectionSlug, error, req })
}
if (returning === false) {
return null
}
const result = await Model.find(
query,
{},
{
...options,
sort,
},
)
transform({
adapter: this,
data: result,
fields: collectionConfig.fields,
operation: 'read',
})
return result
}