Files
payload/packages/db-mongodb/src/updateGlobalVersion.ts
James Mikrut f96cf593ce feat: add jsonParse flag to mongooseAdapter that preserves existing, untracked MongoDB data types (#7338)
## 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
2024-07-24 14:02:06 -04:00

45 lines
1.1 KiB
TypeScript

import type { UpdateGlobalVersionArgs } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'
import type { MongooseAdapter } from '.'
import sanitizeInternalFields from './utilities/sanitizeInternalFields'
import { withSession } from './withSession'
export async function updateGlobalVersion<T extends TypeWithID>(
this: MongooseAdapter,
{
id,
global,
locale,
req = {} as PayloadRequest,
versionData,
where,
}: UpdateGlobalVersionArgs<T>,
) {
const VersionModel = this.versions[global]
const whereToUse = where || { id: { equals: id } }
const options = {
...(await withSession(this, req)),
lean: true,
new: true,
}
const query = await VersionModel.buildQuery({
locale,
payload: this.payload,
where: whereToUse,
})
let doc = await VersionModel.findOneAndUpdate(query, versionData, options)
doc = this.jsonParse ? JSON.parse(JSON.stringify(doc)) : doc
const verificationToken = doc._verificationToken
if (verificationToken) {
doc._verificationToken = verificationToken
}
return sanitizeInternalFields(doc)
}