Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
#posts vanish after enabling drafts
6 messages · Page 1 of 1 (latest)
sorted out, just forced a save of the collection via local api
Sorry for the old question, but when you forced a save of the collection, was it an update or a copy? This didn't work for me.
await payload.update({
collection: slug,
id,
data: {},
draft: true,
})
There's a serious race condition happening with the code above. I've run it many times without success. It only worked when I placed a console.log in node_modules/payload/dist/versions/saveVersions.js to see why it wasn't running. There is also a need for official documentation on how to migrate after setting drafts on collections that already have documents. I'm totally at a loss here.
This worked for me - this way I can enable Drafts on existing collections and migrate the docs with a post from Postman:
import { HttpError } from "internal/interface"
import type { GeneratedTypes } from "payload"
import type { PayloadRequest } from "payload/types"
export const migrateCollectionToDrafts = async (req: PayloadRequest): Promise<void> => {
const {
body: { collectionSlug }
} = req
if (!collectionSlug) throw new HttpError('No collection slug in request.', 400)
try {
const result = await req.payload.update({
collection: collectionSlug as keyof GeneratedTypes['collections'],
where: {
'id': {
exists: true,
},
},
data: {},
})
const updatedDocs = result.docs
const errors = result.errors
if (errors?.length > 0) {
req.payload.logger.error(`${errors.length} error(s) migrating collection ${collectionSlug} to Drafts, see Debug Log.`)
errors.forEach(e => {
req.payload.logger.error(e)
})
}
if (updatedDocs?.length > 0) {
const ids = updatedDocs.map(d => d.id).join(', ')
req.payload.logger.info(`Migrated collection ${collectionSlug} to Drafts, ids: ${ids}.`)
}
} catch (e) {
req.payload.logger.error(`Error migrating collection ${collectionSlug} to Drafts: `+e)
}
}
Base config:
...
endpoints: [
{
path: '/migrateCollectionToDrafts',
method: 'post',
handler: migrateCollectionToDrafts
},
],
...