## Description Preserves external data structures stored in MongoDB by avoiding the use of `JSON.parse(JSON.stringify(mongooseDoc))`. - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] New feature (non-breaking change which adds functionality) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import type { UpdateOne } from 'payload/database'
|
|
import type { PayloadRequest } from 'payload/types'
|
|
|
|
import type { MongooseAdapter } from '.'
|
|
|
|
import handleError from './utilities/handleError'
|
|
import sanitizeInternalFields from './utilities/sanitizeInternalFields'
|
|
import { withSession } from './withSession'
|
|
|
|
export const updateOne: UpdateOne = async function updateOne(
|
|
this: MongooseAdapter,
|
|
{ id, collection, data, locale, req = {} as PayloadRequest, where: whereArg },
|
|
) {
|
|
const where = id ? { id: { equals: id } } : whereArg
|
|
const Model = this.collections[collection]
|
|
const options = {
|
|
...(await withSession(this, req)),
|
|
lean: true,
|
|
new: true,
|
|
}
|
|
|
|
const query = await Model.buildQuery({
|
|
locale,
|
|
payload: this.payload,
|
|
where,
|
|
})
|
|
|
|
let result
|
|
try {
|
|
result = await Model.findOneAndUpdate(query, data, options)
|
|
} catch (error) {
|
|
handleError(error, req)
|
|
}
|
|
|
|
result = this.jsonParse ? JSON.parse(JSON.stringify(result)) : result
|
|
|
|
return sanitizeInternalFields(result)
|
|
}
|