I’m building a media library in Payload CMS (v3) with a Postgres database, and I’d like every image upload to:
Extract the top 5 dominant colours via Cloudinary
Run Amazon Rekognition auto-tagging
Save both sets of data back into my media collection in one atomic operation
So far I’ve tried using the official @jhb.software/payload-cloudinary-plugin and/or an afterChange hook, for example:
hooks: {
afterChange: [
async ({ operation, req, doc, previousDoc }) => {
if (operation === 'create' || operation === 'update') {
// 1) fetch colours
const coloursRes = await cloudinary.api.resource(doc.cloudinaryPublicId, { colors: true, max_results: 5 });
const dominantColours = coloursRes.colors!.slice(0,5).map(c => ({ hex: c[0] }));
// 2) run Rekognition
const tagsRes = await cloudinary.uploader.explicit(doc.cloudinaryPublicId, {
categorization: 'aws_rek_tagging',
auto_tagging: 0.6,
});
const aiTags = tagsRes.tags!.map(t => ({ tag: t }));
// 3) update Payload
await req.payload.db.updateOne({
collection: 'media',
where: { id: { equals: doc.id } },
data: { dominantColours, aiTags },
});
}
}
],
},
The payload-cloudinary-plugin’s uploadOptions only accepts certain options (it errors on categorization / auto_tagging), so I can’t configure both colour + auto-tags at upload time.
If I try to call req.payload.updateOne() in afterChange, I get Postgres foreign-key / null-id errors on the nested colour array.
I also end up with race conditions or hanging transactions when I try to update inside the same hook.
Is there a recommended way to trigger both Cloudinary colour analysis and AWS Rekognition auto-tagging in one upload/update event? and persist those results cleanly back into a Payload CMS collection without transaction deadlocks or null-constraint errors?