mysqltypeormaudit-logging

TypeORM / MySQL - Intercept Update/Insert Queries and set app user ID automatically on 'editedBy' column


I am using TypeORM with MySQL and am setting up automatic auditing of all columns and database tables via MySQL Triggers - not TypeORM's "Logger" feature (unless you have some extra info)...

Without getting bogged down, the MySQL Triggers approach works very well and means no app-side code is required.

The problem: I cannot provide MySQL queries with the logged in app user's ID in a way that does not require we apply it in every query created in this app. We do have a central "CRUD" class, but that is for generic CRUD, so our more "specialist" queries would require special treatment - undesired.

Each of our tables has an int field "editedBy" where we would like to update with the user ID who edited the row (by using our app).

Question: Is there a way to intercept all non-read queries in TypeORM (regardless if its active record or query builder) and be able to update a column in the affected tables ('editedBy' int field)?

This would allow our Triggers solution to be complete.

P.S. I tried out TypeORM's custom logging function:

... typeorm createConnection({ ....
    logger: new MyCustomLogger()
... });

class MyCustomLogger { // 'extend' has issue - works without anyway: extends Logger {

 logQuery(query, parameters, somethingelse) // WORKS
 { ... }

logQuery does appear to fire before the query (I think) is sent to MySQL, but I cannot find a way how to extract the "Json-like" javascript object out of this, to modify each table's "editedBy". It would be great if there was a way to find all tables within this function and adjust editedBy. Happy to try other options... that don't entail updating the many files we have containing database calls.

Thanks


Solution

  • I used a combination of https://github.com/skonves/express-http-context node module to pass user ID to TypeORM's Event Subscribers feature to make the update to data about to be submitted to DB: https://github.com/typeorm/typeorm/blob/master/sample/sample5-subscribers/subscriber/EverythingSubscriber.ts