0

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?

Obvious_Grapefruit
  • 640
  • 1
  • 8
  • 21
  • Just avoid cascading deletes - I actually think cascading deletes are _dangerous_ because it will _just let you_ potentially delete _everything_ without any warning if you accidentally delete some god-object which parents everything in your database - but if you disable cascading-deletes you can still delete things - but you need to use explicit `DELETE` on each table, and being explicit is good, especially w.r.t. deleting data. – Dai Aug 06 '22 at 02:40
  • I have a similar system where I store images/videos/uploads in Azure Blob Storage (think: S3 Buckets) - except I use a content-addressable-storage model with immutable data (so a blob's name is its SHA-256 hash (so free de-duping!)) - and my RDBMS references these blobs via a `binary(32)` representation of that SHA-256 hash - but the important thing is that I never actually do hard `DELETE` ops: under normal operation things can only be soft-deleted - we only actually `DELETE` things when legally required. – Dai Aug 06 '22 at 02:43

0 Answers0