Hi everyone 👋
I’m building a universal logging system in NestJS using TypeORM. My goal is to track and log all changes made to entities, including nested properties and relations.
What I'm currently doing:
I use a TypeORM event subscriber (afterUpdate) to hook into update events.
I compare the old value vs new value using the diff npm library.
I store the changed paths, old values, and new values for auditing purposes.
The limitation:
TypeORM only provides the updated entity in event.entity, and not the full object with its relations or nested structure.
event.databaseEntity (the previous value) also lacks full nested relation data.
To work around this, I do the following:
Manually fetch the full object with nested relations before the update using .findOne({ relations: [...] }).
After the update, fetch the new version of the object.
Compare both versions using diff.
Manually exclude fields like id, createdAt, etc. which are irrelevant for audit logging.
What I'm asking:
Is there a better, cleaner or more efficient way to do this in NestJS with TypeORM?
Specifically to get both before and after states of an entity with all nested relations inside the subscriber or elsewhere in a universal/global context.
Bonus: any way to auto-ignore non-meaningful fields like id, timestamps, etc.
Would love to hear how others solved this or approached it differently 🙏