Prisma middleware delete
I have 3 models - Category, Product and Color.
Whenever a category gets deleted, its products also get deleted(Cascade) and each product's colors get deleted.
Each color has an image saved in a bucket that I want to delete whenever a Color gets deleted, how can I write an event or middleware that get triggered whenever a Color gets deleted whenever a Category or Product get deleted? I tried doing $use but when deleting a Product I don't get params related to the colors
9 Replies
Prisma
Referential actions
Referential actions let you define the update and delete behavior of related models on the database level
you can do that right from prima schema using the onDelete
I already have an onDelete Cascade, I need the Color object before it gets deleted to send an api request somewhere else with a property
before deleting it send the api request,
did you read the question my man?
im deleting another model and Color gets deleted because of onDelete Cascade, im not directly deleting it
my answer was too short, query the colors before you do delete the product then send your api and delete
not familiar with prisma but could do it as part of the api request for deleting?
how big are these tables? if their not huge you could just have a background job that queries the tables and deletes any unused color
In the procedure where you make db delete the category, before doing that, handle the bucket deleting image stuff, then remove the category, or product.
Do it the other way around but yeah ^