Files
payload/test/plugin-import-export/seed/index.ts
Sasha 564fdb0e17 fix: virtual relationship fields with select (#12266)
Continuation of https://github.com/payloadcms/payload/pull/12265.

Currently, using `select` on new relationship virtual fields:
```
const doc = await payload.findByID({
  collection: 'virtual-relations',
  depth: 0,
  id,
  select: { postTitle: true },
})
```
doesn't work, because in order to calculate `post.title`, the `post`
field must be selected as well. This PR adds logic that sanitizes the
incoming `select` to include those relationships into `select` (that are
related to selected virtual fields)

---------

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
2025-04-30 12:27:04 -04:00

147 lines
3.0 KiB
TypeScript

import type { Payload } from 'payload'
import { devUser } from '../../credentials.js'
import { richTextData } from './richTextData.js'
export const seed = async (payload: Payload): Promise<boolean> => {
payload.logger.info('Seeding data...')
try {
const user = await payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
name: 'name value',
},
})
// create pages
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `Title ${i}`,
group: {
array: [{ field1: 'test' }],
},
},
})
}
for (let i = 0; i < 5; i++) {
const doc = await payload.create({
collection: 'pages',
data: {
title: `Localized ${i}`,
localized: 'en test',
},
locale: 'en',
})
await payload.update({
collection: 'pages',
id: doc.id,
data: {
localized: 'es test',
},
locale: 'es',
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `Array ${i}`,
array: [
{
field1: 'foo',
field2: 'bar',
},
{
field1: 'foo',
field2: 'baz',
},
],
},
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `Array Subfield ${i}`,
array: [
{
field1: 'foo',
field2: 'bar',
},
{
field1: 'foo',
field2: 'baz',
},
],
},
})
}
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',
data: {
title: `hasMany Number ${i}`,
hasManyNumber: [0, 1, 1, 2, 3, 5, 8, 13, 21],
},
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `Blocks ${i}`,
blocks: [
{
blockType: 'hero',
title: 'test',
},
{
blockType: 'content',
richText: richTextData,
},
],
},
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `JSON ${i}`,
},
})
}
for (let i = 0; i < 5; i++) {
await payload.create({
collection: 'pages',
data: {
title: `Jobs ${i}`,
},
})
}
return true
} catch (err) {
console.error(err)
return false
}
}