1
+ /* eslint-disable perfectionist/sort-objects */
1
2
import type { PaginatedDocs , PayloadRequest , Sort , User , Where } from 'payload'
2
3
3
4
import { stringify } from 'csv-stringify/sync'
@@ -10,6 +11,10 @@ import { getSelect } from './getSelect.js'
10
11
11
12
type Export = {
12
13
collectionSlug : string
14
+ /**
15
+ * If true, enables debug logging
16
+ */
17
+ debug ?: boolean
13
18
drafts ?: 'no' | 'yes'
14
19
exportsCollection : string
15
20
fields ?: string [ ]
@@ -42,6 +47,7 @@ export const createExport = async (args: CreateExportArgs) => {
42
47
id,
43
48
name : nameArg ,
44
49
collectionSlug,
50
+ debug = false ,
45
51
drafts,
46
52
exportsCollection,
47
53
fields,
@@ -54,6 +60,17 @@ export const createExport = async (args: CreateExportArgs) => {
54
60
req : { locale : localeArg , payload } ,
55
61
req,
56
62
} = args
63
+
64
+ if ( debug ) {
65
+ req . payload . logger . info ( {
66
+ message : 'Starting export process with args:' ,
67
+ collectionSlug,
68
+ drafts,
69
+ fields,
70
+ format,
71
+ } )
72
+ }
73
+
57
74
const locale = localeInput ?? localeArg
58
75
const collectionConfig = payload . config . collections . find ( ( { slug } ) => slug === collectionSlug )
59
76
if ( ! collectionConfig ) {
@@ -63,6 +80,10 @@ export const createExport = async (args: CreateExportArgs) => {
63
80
const name = `${ nameArg ?? `${ getFilename ( ) } -${ collectionSlug } ` } .${ format } `
64
81
const isCSV = format === 'csv'
65
82
83
+ if ( debug ) {
84
+ req . payload . logger . info ( { message : 'Export configuration:' , name, isCSV, locale } )
85
+ }
86
+
66
87
const findArgs = {
67
88
collection : collectionSlug ,
68
89
depth : 0 ,
@@ -77,22 +98,37 @@ export const createExport = async (args: CreateExportArgs) => {
77
98
where,
78
99
}
79
100
101
+ if ( debug ) {
102
+ req . payload . logger . info ( { message : 'Find arguments:' , findArgs } )
103
+ }
104
+
80
105
let result : PaginatedDocs = { hasNextPage : true } as PaginatedDocs
81
106
82
107
if ( download ) {
108
+ if ( debug ) {
109
+ req . payload . logger . info ( 'Starting download stream' )
110
+ }
83
111
const encoder = new TextEncoder ( )
84
112
const stream = new Readable ( {
85
113
async read ( ) {
86
114
let result = await payload . find ( findArgs )
87
115
let isFirstBatch = true
88
116
89
117
while ( result . docs . length > 0 ) {
118
+ if ( debug ) {
119
+ req . payload . logger . info (
120
+ `Processing batch ${ findArgs . page + 1 } with ${ result . docs . length } documents` ,
121
+ )
122
+ }
90
123
const csvInput = result . docs . map ( ( doc ) => flattenObject ( { doc, fields } ) )
91
124
const csvString = stringify ( csvInput , { header : isFirstBatch } )
92
125
this . push ( encoder . encode ( csvString ) )
93
126
isFirstBatch = false
94
127
95
128
if ( ! result . hasNextPage ) {
129
+ if ( debug ) {
130
+ req . payload . logger . info ( 'Stream complete - no more pages' )
131
+ }
96
132
this . push ( null ) // End the stream
97
133
break
98
134
}
@@ -111,13 +147,22 @@ export const createExport = async (args: CreateExportArgs) => {
111
147
} )
112
148
}
113
149
150
+ if ( debug ) {
151
+ req . payload . logger . info ( 'Starting file generation' )
152
+ }
114
153
const outputData : string [ ] = [ ]
115
154
let isFirstBatch = true
116
155
117
156
while ( result . hasNextPage ) {
118
157
findArgs . page += 1
119
158
result = await payload . find ( findArgs )
120
159
160
+ if ( debug ) {
161
+ req . payload . logger . info (
162
+ `Processing batch ${ findArgs . page } with ${ result . docs . length } documents` ,
163
+ )
164
+ }
165
+
121
166
if ( isCSV ) {
122
167
const csvInput = result . docs . map ( ( doc ) => flattenObject ( { doc, fields } ) )
123
168
outputData . push ( stringify ( csvInput , { header : isFirstBatch } ) )
@@ -129,15 +174,24 @@ export const createExport = async (args: CreateExportArgs) => {
129
174
}
130
175
131
176
const buffer = Buffer . from ( format === 'json' ? `[${ outputData . join ( ',' ) } ]` : outputData . join ( '' ) )
177
+ if ( debug ) {
178
+ req . payload . logger . info ( `${ format } file generation complete` )
179
+ }
132
180
133
181
if ( ! id ) {
182
+ if ( debug ) {
183
+ req . payload . logger . info ( 'Creating new export file' )
184
+ }
134
185
req . file = {
135
186
name,
136
187
data : buffer ,
137
188
mimetype : isCSV ? 'text/csv' : 'application/json' ,
138
189
size : buffer . length ,
139
190
}
140
191
} else {
192
+ if ( debug ) {
193
+ req . payload . logger . info ( `Updating existing export with id: ${ id } ` )
194
+ }
141
195
await req . payload . update ( {
142
196
id,
143
197
collection : exportsCollection ,
@@ -151,4 +205,7 @@ export const createExport = async (args: CreateExportArgs) => {
151
205
user,
152
206
} )
153
207
}
208
+ if ( debug ) {
209
+ req . payload . logger . info ( 'Export process completed successfully' )
210
+ }
154
211
}
0 commit comments