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
Abuzeid
Abuzeid2y ago
Prisma
Referential actions
Referential actions let you define the update and delete behavior of related models on the database level
Abuzeid
Abuzeid2y ago
you can do that right from prima schema using the onDelete
hyperzone
hyperzoneOP2y ago
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
Abuzeid
Abuzeid2y ago
before deleting it send the api request,
hyperzone
hyperzoneOP2y ago
did you read the question my man? im deleting another model and Color gets deleted because of onDelete Cascade, im not directly deleting it
Abuzeid
Abuzeid2y ago
my answer was too short, query the colors before you do delete the product then send your api and delete
Scot
Scot2y ago
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
BarisP
BarisP2y ago
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.
Scot
Scot2y ago
Do it the other way around but yeah ^
Want results from more Discord servers?
Add your server