Compare commits

...

2 Commits

Author SHA1 Message Date
Dan Ribbens
781d112294 chore: correct comment 2025-04-29 16:46:25 -04:00
Dan Ribbens
aa8779ae8b fix(plugin-import-export): virtual relationship fields do not export 2025-04-29 16:40:42 -04:00
5 changed files with 64 additions and 1 deletions

View File

@@ -98,6 +98,19 @@ export const Pages: CollectionConfig = {
type: 'relationship',
relationTo: 'users',
},
{
name: 'virtualRelationship',
type: 'text',
virtual: 'author.name',
},
{
name: 'virtual',
type: 'text',
virtual: true,
hooks: {
afterRead: [() => 'virtual value'],
},
},
{
name: 'hasManyNumber',
type: 'number',

View File

@@ -10,6 +10,10 @@ export const Users: CollectionConfig = {
read: () => true,
},
fields: [
{
name: 'name',
type: 'text',
},
// Email added by default
// Add more fields as needed
],

View File

@@ -255,6 +255,35 @@ describe('@payloadcms/plugin-import-export', () => {
expect(str.indexOf('createdAt')).toBeLessThan(str.indexOf('updatedAt'))
})
it('should create a CSV file with virtual fields', async () => {
const fields = ['id', 'virtual', 'virtualRelationship']
const doc = await payload.create({
collection: 'exports',
user,
data: {
collectionSlug: 'pages',
fields,
format: 'csv',
where: {
title: { contains: 'Virtual ' },
},
},
})
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 the expected virtual fields
expect(data[0].virtual).toStrictEqual('virtual value')
expect(data[0].virtualRelationship).toStrictEqual('name value')
})
it('should create a file for collection csv from array.subfield', async () => {
let doc = await payload.create({
collection: 'exports',

View File

@@ -131,6 +131,7 @@ export interface UserAuthOperations {
*/
export interface User {
id: string;
name?: string | null;
updatedAt: string;
createdAt: string;
email: string;
@@ -199,6 +200,8 @@ export interface Page {
)[]
| null;
author?: (string | null) | User;
virtualRelationship?: string | null;
virtual?: string | null;
hasManyNumber?: number[] | null;
relationship?: (string | null) | User;
excerpt?: string | null;
@@ -444,6 +447,7 @@ export interface PayloadMigration {
* via the `definition` "users_select".
*/
export interface UsersSelect<T extends boolean = true> {
name?: T;
updatedAt?: T;
createdAt?: T;
email?: T;
@@ -500,6 +504,8 @@ export interface PagesSelect<T extends boolean = true> {
};
};
author?: T;
virtualRelationship?: T;
virtual?: T;
hasManyNumber?: T;
relationship?: T;
excerpt?: T;

View File

@@ -6,11 +6,12 @@ import { richTextData } from './richTextData.js'
export const seed = async (payload: Payload): Promise<boolean> => {
payload.logger.info('Seeding data...')
try {
await payload.create({
const user = await payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
name: 'name value',
},
})
// create pages
@@ -80,6 +81,16 @@ export const seed = async (payload: Payload): Promise<boolean> => {
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
author: user.id,
title: `Virtual ${i}`,
},
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',