Skip to content

Commit 678bfc7

Browse files
authored
feat(plugin-import-export): add debug logging option (#12705)
1 parent 7b21270 commit 678bfc7

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

packages/plugin-import-export/src/export/createExport.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable perfectionist/sort-objects */
12
import type { PaginatedDocs, PayloadRequest, Sort, User, Where } from 'payload'
23

34
import { stringify } from 'csv-stringify/sync'
@@ -10,6 +11,10 @@ import { getSelect } from './getSelect.js'
1011

1112
type Export = {
1213
collectionSlug: string
14+
/**
15+
* If true, enables debug logging
16+
*/
17+
debug?: boolean
1318
drafts?: 'no' | 'yes'
1419
exportsCollection: string
1520
fields?: string[]
@@ -42,6 +47,7 @@ export const createExport = async (args: CreateExportArgs) => {
4247
id,
4348
name: nameArg,
4449
collectionSlug,
50+
debug = false,
4551
drafts,
4652
exportsCollection,
4753
fields,
@@ -54,6 +60,17 @@ export const createExport = async (args: CreateExportArgs) => {
5460
req: { locale: localeArg, payload },
5561
req,
5662
} = 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+
5774
const locale = localeInput ?? localeArg
5875
const collectionConfig = payload.config.collections.find(({ slug }) => slug === collectionSlug)
5976
if (!collectionConfig) {
@@ -63,6 +80,10 @@ export const createExport = async (args: CreateExportArgs) => {
6380
const name = `${nameArg ?? `${getFilename()}-${collectionSlug}`}.${format}`
6481
const isCSV = format === 'csv'
6582

83+
if (debug) {
84+
req.payload.logger.info({ message: 'Export configuration:', name, isCSV, locale })
85+
}
86+
6687
const findArgs = {
6788
collection: collectionSlug,
6889
depth: 0,
@@ -77,22 +98,37 @@ export const createExport = async (args: CreateExportArgs) => {
7798
where,
7899
}
79100

101+
if (debug) {
102+
req.payload.logger.info({ message: 'Find arguments:', findArgs })
103+
}
104+
80105
let result: PaginatedDocs = { hasNextPage: true } as PaginatedDocs
81106

82107
if (download) {
108+
if (debug) {
109+
req.payload.logger.info('Starting download stream')
110+
}
83111
const encoder = new TextEncoder()
84112
const stream = new Readable({
85113
async read() {
86114
let result = await payload.find(findArgs)
87115
let isFirstBatch = true
88116

89117
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+
}
90123
const csvInput = result.docs.map((doc) => flattenObject({ doc, fields }))
91124
const csvString = stringify(csvInput, { header: isFirstBatch })
92125
this.push(encoder.encode(csvString))
93126
isFirstBatch = false
94127

95128
if (!result.hasNextPage) {
129+
if (debug) {
130+
req.payload.logger.info('Stream complete - no more pages')
131+
}
96132
this.push(null) // End the stream
97133
break
98134
}
@@ -111,13 +147,22 @@ export const createExport = async (args: CreateExportArgs) => {
111147
})
112148
}
113149

150+
if (debug) {
151+
req.payload.logger.info('Starting file generation')
152+
}
114153
const outputData: string[] = []
115154
let isFirstBatch = true
116155

117156
while (result.hasNextPage) {
118157
findArgs.page += 1
119158
result = await payload.find(findArgs)
120159

160+
if (debug) {
161+
req.payload.logger.info(
162+
`Processing batch ${findArgs.page} with ${result.docs.length} documents`,
163+
)
164+
}
165+
121166
if (isCSV) {
122167
const csvInput = result.docs.map((doc) => flattenObject({ doc, fields }))
123168
outputData.push(stringify(csvInput, { header: isFirstBatch }))
@@ -129,15 +174,24 @@ export const createExport = async (args: CreateExportArgs) => {
129174
}
130175

131176
const buffer = Buffer.from(format === 'json' ? `[${outputData.join(',')}]` : outputData.join(''))
177+
if (debug) {
178+
req.payload.logger.info(`${format} file generation complete`)
179+
}
132180

133181
if (!id) {
182+
if (debug) {
183+
req.payload.logger.info('Creating new export file')
184+
}
134185
req.file = {
135186
name,
136187
data: buffer,
137188
mimetype: isCSV ? 'text/csv' : 'application/json',
138189
size: buffer.length,
139190
}
140191
} else {
192+
if (debug) {
193+
req.payload.logger.info(`Updating existing export with id: ${id}`)
194+
}
141195
await req.payload.update({
142196
id,
143197
collection: exportsCollection,
@@ -151,4 +205,7 @@ export const createExport = async (args: CreateExportArgs) => {
151205
user,
152206
})
153207
}
208+
if (debug) {
209+
req.payload.logger.info('Export process completed successfully')
210+
}
154211
}

packages/plugin-import-export/src/export/download.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { PayloadHandler } from 'payload'
1+
import type { PayloadRequest } from 'payload'
22

33
import { APIError } from 'payload'
44

55
import { createExport } from './createExport.js'
66

7-
export const download: PayloadHandler = async (req) => {
7+
export const download = async (req: PayloadRequest, debug = false) => {
88
let body
99
if (typeof req?.json === 'function') {
1010
body = await req.json()
@@ -20,7 +20,7 @@ export const download: PayloadHandler = async (req) => {
2020

2121
return createExport({
2222
download: true,
23-
input: body.data,
23+
input: { ...body.data, debug },
2424
req,
2525
}) as Promise<Response>
2626
}

packages/plugin-import-export/src/getExportCollection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export const getExportCollection = ({
3636
disableDuplicate: true,
3737
endpoints: [
3838
{
39-
handler: download,
39+
handler: (req) => {
40+
return download(req, pluginConfig.debug)
41+
},
4042
method: 'post',
4143
path: '/download',
4244
},
@@ -63,7 +65,8 @@ export const getExportCollection = ({
6365
return
6466
}
6567
const { user } = req
66-
await createExport({ input: { ...args.data, user }, req })
68+
const debug = pluginConfig.debug
69+
await createExport({ input: { ...args.data, debug, user }, req })
6770
})
6871
} else {
6972
afterChange.push(async ({ doc, operation, req }) => {

packages/plugin-import-export/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export type ImportExportPluginConfig = {
1111
* Defaults to all collections
1212
*/
1313
collections?: string[]
14+
/**
15+
* If true, enables debug logging
16+
*/
17+
debug?: boolean
1418
/**
1519
* Enable to force the export to run synchronously
1620
*/

0 commit comments

Comments
 (0)