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:
@@ -70,7 +70,7 @@ export const flattenObject = ({
|
||||
|
||||
flatten(doc, prefix)
|
||||
|
||||
if (fields) {
|
||||
if (Array.isArray(fields) && fields.length > 0) {
|
||||
const orderedResult: Record<string, unknown> = {}
|
||||
|
||||
const fieldToRegex = (field: string): RegExp => {
|
||||
|
||||
@@ -368,6 +368,36 @@ describe('@payloadcms/plugin-import-export', () => {
|
||||
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 () => {
|
||||
const fields = [
|
||||
'id',
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@payload-config": ["./test/plugin-import-export/config.ts"],
|
||||
"@payload-config": ["./test/_community/config.ts"],
|
||||
"@payloadcms/admin-bar": ["./packages/admin-bar/src"],
|
||||
"@payloadcms/live-preview": ["./packages/live-preview/src"],
|
||||
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
|
||||
|
||||
Reference in New Issue
Block a user