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
82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import type { MongooseUpdateQueryOptions, UpdateQuery } from 'mongoose'
|
|
import type { UpdateOne } from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
import { buildQuery } from './queries/buildQuery.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 updateOne: UpdateOne = async function updateOne(
|
|
this: MongooseAdapter,
|
|
{
|
|
id,
|
|
collection: collectionSlug,
|
|
data,
|
|
locale,
|
|
options: optionsArgs = {},
|
|
req,
|
|
returning,
|
|
select,
|
|
where: whereArg = {},
|
|
},
|
|
) {
|
|
const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug })
|
|
const where = id ? { id: { equals: id } } : whereArg
|
|
const fields = collectionConfig.fields
|
|
|
|
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,
|
|
}
|
|
|
|
const query = await buildQuery({
|
|
adapter: this,
|
|
collectionSlug,
|
|
fields: collectionConfig.flattenedFields,
|
|
locale,
|
|
where,
|
|
})
|
|
|
|
let result
|
|
|
|
const $inc: Record<string, number> = {}
|
|
let updateData: UpdateQuery<any> = data
|
|
transform({ $inc, adapter: this, data, fields, operation: 'write' })
|
|
if (Object.keys($inc).length) {
|
|
updateData = { $inc, $set: updateData }
|
|
}
|
|
|
|
try {
|
|
if (returning === false) {
|
|
await Model.updateOne(query, updateData, options)
|
|
transform({ adapter: this, data, fields, operation: 'read' })
|
|
return null
|
|
} else {
|
|
result = await Model.findOneAndUpdate(query, updateData, options)
|
|
}
|
|
} catch (error) {
|
|
handleError({ collection: collectionSlug, error, req })
|
|
}
|
|
|
|
if (!result) {
|
|
return null
|
|
}
|
|
|
|
transform({ adapter: this, data: result, fields, operation: 'read' })
|
|
|
|
return result
|
|
}
|