chore: optimize test seed payload.create calls and run them in parallel to reduce MongoDB errors

This commit is contained in:
Alessio Gravili
2024-03-25 23:58:55 -04:00
parent 8a5a08cbe1
commit 56f9c88251
4 changed files with 258 additions and 209 deletions

View File

@@ -1,6 +1,7 @@
import type { Payload } from 'payload'
import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { seedDB } from '../helpers/seed.js'
import {
collectionSlugs,
@@ -13,73 +14,102 @@ import {
usersCollectionSlug,
} from './slugs.js'
export async function clearAndSeedEverything(_payload: Payload) {
export async function clearAndSeedEverything(_payload: Payload, parallel: boolean = false) {
return await seedDB({
snapshotKey: 'adminTest',
shouldResetDB: true,
collectionSlugs,
_payload,
seedFunction: async (_payload) => {
await Promise.all([
_payload.create({
collection: usersCollectionSlug,
data: {
email: devUser.email,
password: devUser.password,
},
}),
...[...Array(11)].map(() => {
void _payload.create({
collection: postsCollectionSlug,
data: {
title: 'Title',
description: 'Description',
},
})
}),
_payload.create({
collection: customViews1CollectionSlug,
data: {
title: 'Custom View',
},
}),
_payload.create({
collection: customViews2CollectionSlug,
data: {
title: 'Custom View',
},
}),
_payload.create({
collection: geoCollectionSlug,
data: {
point: [7, -7],
},
}),
_payload.create({
collection: geoCollectionSlug,
data: {
point: [5, -5],
},
}),
_payload.create({
collection: noApiViewCollectionSlug,
data: {},
}),
_payload.create({
collection: 'customIdTab',
data: {
id: customIdCollectionId,
title: 'Hello world title',
},
}),
_payload.create({
collection: 'customIdRow',
data: {
id: customIdCollectionId,
title: 'Hello world title',
},
}),
])
await executePromises(
[
() =>
_payload.create({
collection: usersCollectionSlug,
data: {
email: devUser.email,
password: devUser.password,
},
depth: 0,
overrideAccess: true,
}),
...[...Array(11)].map(
() => () =>
_payload.create({
collection: postsCollectionSlug,
data: {
title: 'Title',
description: 'Description',
},
depth: 0,
overrideAccess: true,
}),
),
() =>
_payload.create({
collection: customViews1CollectionSlug,
data: {
title: 'Custom View',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: customViews2CollectionSlug,
data: {
title: 'Custom View',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: geoCollectionSlug,
data: {
point: [7, -7],
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: geoCollectionSlug,
data: {
point: [5, -5],
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: noApiViewCollectionSlug,
data: {},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: 'customIdTab',
data: {
id: customIdCollectionId,
title: 'Hello world title',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: 'customIdRow',
data: {
id: customIdCollectionId,
title: 'Hello world title',
},
depth: 0,
overrideAccess: true,
}),
],
parallel,
)
},
})
}

View File

@@ -185,135 +185,137 @@ export async function clearAndSeedEverything(_payload: Payload, parallel: boolea
.replace(/"\{\{RICH_TEXT_DOC_ID\}\}"/g, `${formattedRichTextDocID}`),
)
await executePromises([
() =>
_payload.create({
collection: usersSlug,
depth: 0,
data: {
email: devUser.email,
password: devUser.password,
},
overrideAccess: true,
}),
() =>
_payload.create({
collection: collapsibleFieldsSlug,
data: collapsibleDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: conditionalLogicSlug,
data: conditionalLogicDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: groupFieldsSlug,
data: groupDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: selectFieldsSlug,
data: selectsDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: radioFieldsSlug,
data: radiosDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: tabsFieldsSlug,
data: tabsDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: pointFieldsSlug,
data: pointDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: dateFieldsSlug,
data: dateDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: codeFieldsSlug,
data: codeDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: jsonFieldsSlug,
data: jsonDoc,
depth: 0,
overrideAccess: true,
}),
await executePromises(
[
() =>
_payload.create({
collection: usersSlug,
depth: 0,
data: {
email: devUser.email,
password: devUser.password,
},
overrideAccess: true,
}),
() =>
_payload.create({
collection: collapsibleFieldsSlug,
data: collapsibleDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: conditionalLogicSlug,
data: conditionalLogicDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: groupFieldsSlug,
data: groupDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: selectFieldsSlug,
data: selectsDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: radioFieldsSlug,
data: radiosDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: tabsFieldsSlug,
data: tabsDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: pointFieldsSlug,
data: pointDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: dateFieldsSlug,
data: dateDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: codeFieldsSlug,
data: codeDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: jsonFieldsSlug,
data: jsonDoc,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: blockFieldsSlug,
data: blocksDocWithRichText,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: blockFieldsSlug,
data: blocksDocWithRichText,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: lexicalFieldsSlug,
data: lexicalDocWithRelId,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: lexicalMigrateFieldsSlug,
data: lexicalMigrateDocWithRelId,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: lexicalFieldsSlug,
data: lexicalDocWithRelId,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: lexicalMigrateFieldsSlug,
data: lexicalMigrateDocWithRelId,
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: numberFieldsSlug,
data: { number: 2 },
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: numberFieldsSlug,
data: { number: 3 },
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: numberFieldsSlug,
data: numberDoc,
depth: 0,
overrideAccess: true,
}),
])
() =>
_payload.create({
collection: numberFieldsSlug,
data: { number: 2 },
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: numberFieldsSlug,
data: { number: 3 },
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: numberFieldsSlug,
data: numberDoc,
depth: 0,
overrideAccess: true,
}),
],
parallel,
)
},
shouldResetDB: true,
snapshotKey: 'fieldsTest',
uploadsDir: path.resolve(dirname, './collections/Upload/uploads'),
})

View File

@@ -3,7 +3,7 @@
*/
export async function executePromises<T extends Array<() => Promise<any>>>(
promiseFns: T,
parallel: boolean = false,
parallel: boolean,
): Promise<{ [K in keyof T]: Awaited<ReturnType<T[K]>> }> {
if (parallel) {
// Parallel execution with Promise.all and maintain proper typing

View File

@@ -1,14 +1,14 @@
import { type Payload } from 'payload'
import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { seedDB } from '../helpers/seed.js'
import { titleToDelete } from './shared.js'
import { collectionSlugs, draftCollectionSlug } from './slugs.js'
export async function clearAndSeedEverything(_payload: Payload) {
export async function clearAndSeedEverything(_payload: Payload, parallel: boolean = false) {
return await seedDB({
snapshotKey: 'versionsTest',
shouldResetDB: true,
collectionSlugs,
_payload,
seedFunction: async (_payload) => {
@@ -20,25 +20,34 @@ export async function clearAndSeedEverything(_payload: Payload) {
},
]
await Promise.all([
_payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
}),
_payload.create({
collection: draftCollectionSlug,
data: {
blocksField,
description: 'Description',
radio: 'test',
title: 'Draft Title',
},
draft: true,
}),
])
await executePromises(
[
() =>
_payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: draftCollectionSlug,
data: {
blocksField,
description: 'Description',
radio: 'test',
title: 'Draft Title',
},
depth: 0,
overrideAccess: true,
draft: true,
}),
],
parallel,
)
const { id: manyDraftsID } = await _payload.create({
collection: draftCollectionSlug,
@@ -48,6 +57,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
radio: 'test',
title: 'Title With Many Versions',
},
depth: 0,
overrideAccess: true,
draft: true,
})
@@ -58,6 +69,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
data: {
title: `Title With Many Versions ${i + 2}`,
},
depth: 0,
overrideAccess: true,
})
}
@@ -70,6 +83,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
radio: 'test',
title: 'Published Title',
},
depth: 0,
overrideAccess: true,
draft: false,
})
@@ -80,6 +95,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
description: 'Description',
title: titleToDelete,
},
depth: 0,
overrideAccess: true,
draft: true,
})
},