fix(plugin-import-export): csv export column order (#12258)
### What? The order of fields, when specified for the create export function was not used for constructing the data. Now the fields order will be used. ### Why? This is important to building CSV data for consumption in other systems. ### How? Adds logic to handle ordering the field values assigned to the export data prior to building the CSV.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { CollectionSlug, Payload } from 'payload'
|
||||
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
@@ -221,6 +222,39 @@ describe('@payloadcms/plugin-import-export', () => {
|
||||
expect(data[0].array_1_field2).toStrictEqual('baz')
|
||||
})
|
||||
|
||||
it('should create a CSV file with columns matching the order of the fields array', async () => {
|
||||
const fields = ['id', 'group.value', 'group.array.field1', 'title', 'createdAt', 'updatedAt']
|
||||
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 buffer = fs.readFileSync(expectedPath)
|
||||
const str = buffer.toString()
|
||||
|
||||
// Assert that the header row matches the fields array
|
||||
expect(str.indexOf('id')).toBeLessThan(str.indexOf('title'))
|
||||
expect(str.indexOf('group_value')).toBeLessThan(str.indexOf('title'))
|
||||
expect(str.indexOf('group_value')).toBeLessThan(str.indexOf('group_array'))
|
||||
expect(str.indexOf('title')).toBeLessThan(str.indexOf('createdAt'))
|
||||
expect(str.indexOf('createdAt')).toBeLessThan(str.indexOf('updatedAt'))
|
||||
})
|
||||
|
||||
it('should create a file for collection csv from array.subfield', async () => {
|
||||
let doc = await payload.create({
|
||||
collection: 'exports',
|
||||
|
||||
Reference in New Issue
Block a user