fix(plugin-import-export): export all available fields by default (#12731)

When making an export of a collection, and no fields are selected then
you get am empty CSV. The intended behavior is that all data is exported
by default.

This fixes the issue that from the admin UI, when the fields selector
the resulting CSV has no columns.
This commit is contained in:
Dan Ribbens
2025-06-10 12:03:26 -04:00
committed by GitHub
parent a43d1a685f
commit c4e5831fbb
3 changed files with 32 additions and 2 deletions

View File

@@ -70,7 +70,7 @@ export const flattenObject = ({
flatten(doc, prefix) flatten(doc, prefix)
if (fields) { if (Array.isArray(fields) && fields.length > 0) {
const orderedResult: Record<string, unknown> = {} const orderedResult: Record<string, unknown> = {}
const fieldToRegex = (field: string): RegExp => { const fieldToRegex = (field: string): RegExp => {

View File

@@ -368,6 +368,36 @@ describe('@payloadcms/plugin-import-export', () => {
expect(data[0].blocks_1_blockType).toStrictEqual('content') expect(data[0].blocks_1_blockType).toStrictEqual('content')
}) })
it('should create a csv of all fields when fields is empty', async () => {
const doc = await payload.create({
collection: 'exports',
user,
data: {
collectionSlug: 'pages',
fields: [],
format: 'csv',
where: {
title: { contains: 'Title ' },
},
},
})
const exportDoc = await payload.findByID({
collection: 'exports',
id: doc.id,
})
expect(exportDoc.filename).toBeDefined()
const expectedPath = path.join(dirname, './uploads', exportDoc.filename as string)
const data = await readCSV(expectedPath)
// Assert that the csv file contains fields even when the specific fields were not given
expect(data[0].id).toBeDefined()
expect(data[0].title).toBeDefined()
expect(data[0].createdAt).toBeDefined()
expect(data[0].createdAt).toBeDefined()
})
it('should run custom toCSV function on a field', async () => { it('should run custom toCSV function on a field', async () => {
const fields = [ const fields = [
'id', 'id',

View File

@@ -31,7 +31,7 @@
} }
], ],
"paths": { "paths": {
"@payload-config": ["./test/plugin-import-export/config.ts"], "@payload-config": ["./test/_community/config.ts"],
"@payloadcms/admin-bar": ["./packages/admin-bar/src"], "@payloadcms/admin-bar": ["./packages/admin-bar/src"],
"@payloadcms/live-preview": ["./packages/live-preview/src"], "@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"], "@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],