1- import type { CollectionSlug , Payload } from 'payload'
2-
31import { fileURLToPath } from 'node:url'
42import path from 'path'
5- import { wait } from 'payload/shared'
63
74import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
8- import { devUser } from '../credentials.js'
95import { DefaultSortCollection } from './collections/DefaultSort/index.js'
106import { DraftsCollection } from './collections/Drafts/index.js'
117import { LocalizedCollection } from './collections/Localized/index.js'
12- import { NonUniqueSortCollection , nonUniqueSortSlug } from './collections/NonUniqueSort/index.js'
8+ import { NonUniqueSortCollection } from './collections/NonUniqueSort/index.js'
139import { OrderableCollection } from './collections/Orderable/index.js'
1410import { OrderableJoinCollection } from './collections/OrderableJoin/index.js'
1511import { PostsCollection } from './collections/Posts/index.js'
12+ import { seed , seedSortable } from './seed.js'
13+
1614const filename = fileURLToPath ( import . meta. url )
1715const dirname = path . dirname ( filename )
1816
@@ -37,6 +35,7 @@ export default buildConfigWithDefaults({
3735 method : 'post' ,
3836 handler : async ( req ) => {
3937 await seedSortable ( req . payload )
38+
4039 return new Response ( JSON . stringify ( { success : true } ) , {
4140 headers : { 'Content-Type' : 'application/json' } ,
4241 status : 200 ,
@@ -50,70 +49,11 @@ export default buildConfigWithDefaults({
5049 defaultLocale : 'en' ,
5150 } ,
5251 onInit : async ( payload ) => {
53- await payload . create ( {
54- collection : 'users' ,
55- data : {
56- email : devUser . email ,
57- password : devUser . password ,
58- } ,
59- } )
60- await seedSortable ( payload )
52+ if ( process . env . SEED_IN_CONFIG_ONINIT !== 'false' ) {
53+ await seed ( payload )
54+ }
6155 } ,
6256 typescript : {
6357 outputFile : path . resolve ( dirname , 'payload-types.ts' ) ,
6458 } ,
6559} )
66-
67- export async function createData (
68- payload : Payload ,
69- collection : CollectionSlug ,
70- // eslint-disable-next-line @typescript-eslint/no-explicit-any
71- data : Record < string , any > [ ] ,
72- ) {
73- for ( const item of data ) {
74- await payload . create ( { collection, data : item } )
75- }
76- }
77-
78- async function seedSortable ( payload : Payload ) {
79- await payload . delete ( { collection : 'orderable' , where : { } } )
80- await payload . delete ( { collection : 'orderable-join' , where : { } } )
81-
82- const joinA = await payload . create ( { collection : 'orderable-join' , data : { title : 'Join A' } } )
83-
84- await createData ( payload , 'orderable' , [
85- { title : 'A' , orderableField : joinA . id } ,
86- { title : 'B' , orderableField : joinA . id } ,
87- { title : 'C' , orderableField : joinA . id } ,
88- { title : 'D' , orderableField : joinA . id } ,
89- ] )
90-
91- await payload . create ( { collection : 'orderable-join' , data : { title : 'Join B' } } )
92-
93- // Create 10 items to be sorted by non-unique field
94- for ( const i of Array . from ( { length : 10 } , ( _ , index ) => index ) ) {
95- let order = 1
96-
97- if ( i > 3 ) {
98- order = 2
99- } else if ( i > 6 ) {
100- order = 3
101- }
102-
103- await payload . create ( {
104- collection : nonUniqueSortSlug ,
105- data : {
106- title : `Post ${ i } ` ,
107- order,
108- } ,
109- } )
110-
111- // Wait 2 seconds to guarantee that the createdAt date is different
112- // await wait(2000)
113- }
114-
115- return new Response ( JSON . stringify ( { success : true } ) , {
116- headers : { 'Content-Type' : 'application/json' } ,
117- status : 200 ,
118- } )
119- }
0 commit comments