Files
payload/packages/db-mongodb/src/updateOne.ts
2024-04-25 10:23:03 -04:00

42 lines
1.1 KiB
TypeScript

import type { UpdateOne } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { MongooseAdapter } from './index.js'
import handleError from './utilities/handleError.js'
import sanitizeInternalFields from './utilities/sanitizeInternalFields.js'
import { withSession } from './withSession.js'
export const updateOne: UpdateOne = async function updateOne(
this: MongooseAdapter,
{ id, collection, data, locale, req = {} as PayloadRequestWithData, where: whereArg },
) {
const where = id ? { id: { equals: id } } : whereArg
const Model = this.collections[collection]
const options = {
...withSession(this, req.transactionID),
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 = JSON.parse(JSON.stringify(result))
result.id = result._id
result = sanitizeInternalFields(result)
return result
}