Removes PayloadRequestWithData in favour of just PayloadRequest with
optional types for `data` and `locale`
`addDataAndFileToRequest` and `addLocalesToRequestFromData` now takes in
a single argument instead of an object
```ts
// before
await addDataAndFileToRequest({ request: req })
addLocalesToRequestFromData({ request: req })
// current
await addDataAndFileToRequest(req)
addLocalesToRequestFromData(req)
```
---------
Co-authored-by: Paul Popus <paul@nouance.io>
33 lines
1001 B
TypeScript
33 lines
1001 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 = withSession(this, req.transactionID)
|
|
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
|
|
}
|