The mongodb adapter `updateOne` method accepts an `options` argument that allows query options to be passed to mongoose. This parameter was added in https://github.com/payloadcms/payload/pull/8397 to support the `upsert` operation. This `options` parameter can also be useful when using the database adaptor directly without going through the local api. It is true that the Mongoose models could be used directly in such situations, but the adapter methods include a lot of useful functionality, like for instance the sanitization of document and relationship ids, so it is desirable to be able to use the adapter functions while still being able to provide mongoose query options (e.g. `{timestamps: false}`). This PR adds the same options parameter to the other update methods of the mongodb adapter.
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import type { QueryOptions } from 'mongoose'
|
|
|
|
import {
|
|
buildVersionGlobalFields,
|
|
type PayloadRequest,
|
|
type TypeWithID,
|
|
type UpdateGlobalVersionArgs,
|
|
} from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
|
|
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
|
|
import { withSession } from './withSession.js'
|
|
|
|
export async function updateGlobalVersion<T extends TypeWithID>(
|
|
this: MongooseAdapter,
|
|
{
|
|
id,
|
|
global: globalSlug,
|
|
locale,
|
|
options: optionsArgs = {},
|
|
req = {} as PayloadRequest,
|
|
select,
|
|
versionData,
|
|
where,
|
|
}: UpdateGlobalVersionArgs<T>,
|
|
) {
|
|
const VersionModel = this.versions[globalSlug]
|
|
const whereToUse = where || { id: { equals: id } }
|
|
const fields = buildVersionGlobalFields(
|
|
this.payload.config,
|
|
this.payload.config.globals.find((global) => global.slug === globalSlug),
|
|
)
|
|
|
|
const options: QueryOptions = {
|
|
...optionsArgs,
|
|
...(await withSession(this, req)),
|
|
lean: true,
|
|
new: true,
|
|
projection: buildProjectionFromSelect({ adapter: this, fields, select }),
|
|
}
|
|
|
|
const query = await VersionModel.buildQuery({
|
|
locale,
|
|
payload: this.payload,
|
|
where: whereToUse,
|
|
})
|
|
|
|
const sanitizedData = sanitizeRelationshipIDs({
|
|
config: this.payload.config,
|
|
data: versionData,
|
|
fields,
|
|
})
|
|
|
|
const doc = await VersionModel.findOneAndUpdate(query, sanitizedData, options)
|
|
|
|
const result = JSON.parse(JSON.stringify(doc))
|
|
|
|
const verificationToken = doc._verificationToken
|
|
|
|
// custom id type reset
|
|
result.id = result._id
|
|
if (verificationToken) {
|
|
result._verificationToken = verificationToken
|
|
}
|
|
return result
|
|
}
|