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
96 lines
1.7 KiB
TypeScript
96 lines
1.7 KiB
TypeScript
import { buildVersionGlobalFields, type CreateGlobalVersion } from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
import { getGlobal } from './utilities/getEntity.js'
|
|
import { getSession } from './utilities/getSession.js'
|
|
import { transform } from './utilities/transform.js'
|
|
|
|
export const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(
|
|
this: MongooseAdapter,
|
|
{
|
|
autosave,
|
|
createdAt,
|
|
globalSlug,
|
|
parent,
|
|
publishedLocale,
|
|
req,
|
|
returning,
|
|
snapshot,
|
|
updatedAt,
|
|
versionData,
|
|
},
|
|
) {
|
|
const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })
|
|
|
|
const options = {
|
|
session: await getSession(this, req),
|
|
// Timestamps are manually added by the write transform
|
|
timestamps: false,
|
|
}
|
|
|
|
const data = {
|
|
autosave,
|
|
createdAt,
|
|
latest: true,
|
|
parent,
|
|
publishedLocale,
|
|
snapshot,
|
|
updatedAt,
|
|
version: versionData,
|
|
}
|
|
if (!data.createdAt) {
|
|
data.createdAt = new Date().toISOString()
|
|
}
|
|
|
|
const fields = buildVersionGlobalFields(this.payload.config, globalConfig)
|
|
|
|
transform({
|
|
adapter: this,
|
|
data,
|
|
fields,
|
|
operation: 'write',
|
|
})
|
|
|
|
let [doc] = await Model.create([data], options, req)
|
|
|
|
await Model.updateMany(
|
|
{
|
|
$and: [
|
|
{
|
|
_id: {
|
|
$ne: doc._id,
|
|
},
|
|
},
|
|
{
|
|
parent: {
|
|
$eq: parent,
|
|
},
|
|
},
|
|
{
|
|
latest: {
|
|
$eq: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{ $unset: { latest: 1 } },
|
|
options,
|
|
)
|
|
|
|
if (returning === false) {
|
|
return null
|
|
}
|
|
|
|
doc = doc.toObject()
|
|
|
|
transform({
|
|
adapter: this,
|
|
data: doc,
|
|
fields,
|
|
operation: 'read',
|
|
})
|
|
|
|
return doc
|
|
}
|