Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lzkill opened this issue Jan 14, 2023 · 5 comments
Open

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

lzkill opened this issue Jan 14, 2023 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@lzkill
Copy link

lzkill commented Jan 14, 2023

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

@kibae kibae added the help wanted Extra attention is needed label Jan 14, 2023
@kibae
Copy link
Owner

kibae commented Jan 14, 2023

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
Copy link
Author

lzkill commented Jan 14, 2023

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
Copy link
Owner

kibae commented Jan 15, 2023

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

@clintonb
Copy link

clintonb commented May 30, 2023

typeorm/typeorm#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
Copy link

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');
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants