kibae / typeorm-auditing

TypeORM Auditing: Create history tables and manage changes of entity automatically.
https://www.npmjs.com/package/typeorm-auditing
MIT License
25 stars 3 forks source link

Question: how to inject additional info like request userId? #11

Open lzkill opened 1 year ago

lzkill commented 1 year ago

The docs say something about cuatom vars in the auditing entity, but how can I populate such a variable with the request userId?

kibae commented 1 year ago

Hello, @lzkill In general, it is assumed that the origin entity has the information of the first creator and the last updater.

However, I hope that your idea will come true. I'll think about a method, but it will require additional code to use. Please let me know if you have any good ideas for implementation. 🙏

lzkill commented 1 year ago

Hi, @kibae!

What I see is people adding non-column attributes to the entity and populating it before save. This way the additional info is available at the subscriber.

I've also seen something using the event.queryRunner.data, like here.

kibae commented 1 year ago

Oh, I'll take a look. Thanks @lzkill 😀

clintonb commented 1 year ago

https://github.com/typeorm/typeorm/issues/4537 demonstrates how to pass metadata that can be used within the subscriber. This, combined, with updating AuditingSubscriber.saveHistory() to use the entity fields might solve this problem without adding additional columns to the audited data model.

noobogami commented 3 months ago

you can use AsyncDataStorage or (if you are using NestJS) CLS. look at this answer. something like this:

  beforeUpdate(event: UpdateEvent<BaseEntity>) {
    event.entity.updatedBy = this.cls.get('user');
  }