Files
payload/packages/db-mongodb/src/deleteOne.ts
2024-08-29 04:09:44 +00:00

30 lines
805 B
TypeScript

import type { DeleteOne, Document, PayloadRequest } from 'payload'
import type { MongooseAdapter } from './index.js'
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'
import { withSession } from './withSession.js'
export const deleteOne: DeleteOne = async function deleteOne(
this: MongooseAdapter,
{ collection, req = {} as PayloadRequest, where },
) {
const Model = this.collections[collection]
const options = await withSession(this, req)
const query = await Model.buildQuery({
payload: this.payload,
where,
})
const doc = await Model.findOneAndDelete(query, options).lean()
let result: Document = JSON.parse(JSON.stringify(doc))
// custom id type reset
result.id = result._id
result = sanitizeInternalFields(result)
return result
}