Files
payloadcms/test/helpers/reInit.ts
Jarrod Flesch 0711f880ff chore!: simplify api handler (#6910)
Removes PayloadRequestWithData in favour of just PayloadRequest with
optional types for `data` and `locale`

`addDataAndFileToRequest` and `addLocalesToRequestFromData` now takes in
a single argument instead of an object

```ts
// before
await addDataAndFileToRequest({ request: req })
addLocalesToRequestFromData({ request: req })

// current
await addDataAndFileToRequest(req)
addLocalesToRequestFromData(req)
```

---------

Co-authored-by: Paul Popus <paul@nouance.io>
2024-07-02 09:47:03 -04:00

45 lines
1.1 KiB
TypeScript

import type { Endpoint, PayloadHandler } from 'payload'
import { addDataAndFileToRequest } from '@payloadcms/next/utilities'
import httpStatus from 'http-status'
import { path } from './reInitializeDB.js'
import { seedDB } from './seed.js'
const handler: PayloadHandler = async (req) => {
process.env.SEED_IN_CONFIG_ONINIT = 'true'
await addDataAndFileToRequest(req)
const { data, payload } = req
try {
await seedDB({
_payload: payload,
collectionSlugs: payload.config.collections.map(({ slug }) => slug),
seedFunction: payload.config.onInit,
snapshotKey: String(data.snapshotKey),
// uploadsDir can be string or stringlist
uploadsDir: data.uploadsDir as string | string[],
})
return Response.json(
{
message: 'Database reset and onInit run successfully.',
},
{
status: httpStatus.OK,
},
)
} catch (err) {
payload.logger.error(err)
return Response.json(err, {
status: httpStatus.BAD_REQUEST,
})
}
}
export const reInitEndpoint: Endpoint = {
path,
method: 'post',
handler,
}