vendredi 5 août 2022

Deleting files associated with a database record, including when the record is deleted with a cascading delete

I'm building an application with AdonisJS.

A User can create Blogs which have Posts which have PostImages. These are tables in a PostgreSQL database, with cascading deletes on the foreign keys.

(the blog analogy is just for simplified explanation—in reality, it's a bit more complicated)

The post images are stored in an S3 bucket, with the URL stored in the PostImage table.

Of course, it is not ideal to have orphaned images, so when a photo is deleted in the application, an S3 delete operation takes place.

However, a record in PostImage can also be deleted by way of a cascading delete when a Post, Blog, User or something else is deleted.

It feels like poor design to have delete operations in every endpoint that is related to PostImage - this could get out of hand with distant relationships. It also feels flimsy, with a possibility of orphaned files if the operation isn't carried out on every relation.

I considered using model hooks, but due to cascading deletes being an operation that takes place at the DB-level, I don't think the related models would come into it.

Is there a better (more robust) way to do this, in AdonisJS or just generally?

Aucun commentaire:

Enregistrer un commentaire