## Description This is the beta (v3) PR for the v2 PR [here](https://github.com/payloadcms/payload/pull/6857) Addresses #6800, #5108 - [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] Bug fix (non-breaking change which fixes an issue) ## 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
33 lines
993 B
TypeScript
33 lines
993 B
TypeScript
import type { Create, Document, PayloadRequest } from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
import { handleError } from './utilities/handleError.js'
|
|
import { withSession } from './withSession.js'
|
|
|
|
export const create: Create = async function create(
|
|
this: MongooseAdapter,
|
|
{ collection, data, req = {} as PayloadRequest },
|
|
) {
|
|
const Model = this.collections[collection]
|
|
const options = await withSession(this, req)
|
|
let doc
|
|
try {
|
|
;[doc] = await Model.create([data], options)
|
|
} catch (error) {
|
|
handleError({ collection, error, req })
|
|
}
|
|
|
|
// doc.toJSON does not do stuff like converting ObjectIds to string, or date strings to date objects. That's why we use JSON.parse/stringify here
|
|
const result: Document = JSON.parse(JSON.stringify(doc))
|
|
const verificationToken = doc._verificationToken
|
|
|
|
// custom id type reset
|
|
result.id = result._id
|
|
if (verificationToken) {
|
|
result._verificationToken = verificationToken
|
|
}
|
|
|
|
return result
|
|
}
|