Files
payloadcms/test/admin/seed.ts
2024-04-04 20:07:59 -04:00

131 lines
3.0 KiB
TypeScript

import type { Payload } from 'payload'
import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { seedDB } from '../helpers/seed.js'
import {
collectionSlugs,
customIdCollectionId,
customViews1CollectionSlug,
customViews2CollectionSlug,
geoCollectionSlug,
noApiViewCollectionSlug,
postsCollectionSlug,
usersCollectionSlug,
} from './slugs.js'
export const seed = async (_payload) => {
if (_payload.db.name === 'mongoose') {
await Promise.all(
_payload.config.collections.map(async (coll) => {
await new Promise((resolve, reject) => {
_payload.db?.collections[coll.slug]?.ensureIndexes(function (err) {
if (err) reject(err)
resolve(true)
})
})
}),
)
}
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,
}),
],
false,
)
}
export async function clearAndSeedEverything(_payload: Payload) {
return await seedDB({
snapshotKey: 'adminTest',
collectionSlugs,
_payload,
seedFunction: seed,
})
}