1
1
import type { CreateOptions } from 'mongoose'
2
2
3
- import { Types } from 'mongoose'
4
- import { buildVersionCollectionFields , type CreateVersion , type Document } from 'payload'
3
+ import { buildVersionCollectionFields , type CreateVersion } from 'payload'
5
4
6
5
import type { MongooseAdapter } from './index.js'
7
6
8
7
import { getSession } from './utilities/getSession.js'
9
- import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs .js'
8
+ import { transform } from './utilities/transform .js'
10
9
11
10
export const createVersion : CreateVersion = async function createVersion (
12
11
this : MongooseAdapter ,
@@ -27,25 +26,30 @@ export const createVersion: CreateVersion = async function createVersion(
27
26
session : await getSession ( this , req ) ,
28
27
}
29
28
30
- const data = sanitizeRelationshipIDs ( {
31
- config : this . payload . config ,
32
- data : {
33
- autosave,
34
- createdAt,
35
- latest : true ,
36
- parent,
37
- publishedLocale,
38
- snapshot,
39
- updatedAt,
40
- version : versionData ,
41
- } ,
42
- fields : buildVersionCollectionFields (
43
- this . payload . config ,
44
- this . payload . collections [ collectionSlug ] . config ,
45
- ) ,
29
+ const data = {
30
+ autosave,
31
+ createdAt,
32
+ latest : true ,
33
+ parent,
34
+ publishedLocale,
35
+ snapshot,
36
+ updatedAt,
37
+ version : versionData ,
38
+ }
39
+
40
+ const fields = buildVersionCollectionFields (
41
+ this . payload . config ,
42
+ this . payload . collections [ collectionSlug ] . config ,
43
+ )
44
+
45
+ transform ( {
46
+ adapter : this ,
47
+ data,
48
+ fields,
49
+ operation : 'write' ,
46
50
} )
47
51
48
- const [ doc ] = await VersionModel . create ( [ data ] , options , req )
52
+ let [ doc ] = await VersionModel . create ( [ data ] , options , req )
49
53
50
54
const parentQuery = {
51
55
$or : [
@@ -56,13 +60,6 @@ export const createVersion: CreateVersion = async function createVersion(
56
60
} ,
57
61
] ,
58
62
}
59
- if ( data . parent instanceof Types . ObjectId ) {
60
- parentQuery . $or . push ( {
61
- parent : {
62
- $eq : data . parent . toString ( ) ,
63
- } ,
64
- } )
65
- }
66
63
67
64
await VersionModel . updateMany (
68
65
{
@@ -89,13 +86,14 @@ export const createVersion: CreateVersion = async function createVersion(
89
86
options ,
90
87
)
91
88
92
- const result : Document = JSON . parse ( JSON . stringify ( doc ) )
93
- const verificationToken = doc . _verificationToken
89
+ doc = doc . toObject ( )
94
90
95
- // custom id type reset
96
- result . id = result . _id
97
- if ( verificationToken ) {
98
- result . _verificationToken = verificationToken
99
- }
100
- return result
91
+ transform ( {
92
+ adapter : this ,
93
+ data : doc ,
94
+ fields,
95
+ operation : 'read' ,
96
+ } )
97
+
98
+ return doc
101
99
}
0 commit comments