I'm trying to create a Custom Logger (of TypeORM) that logs every "UPDATE", "CREATE" and "DELETE" made in the database. So far I got it to work but now I wanna add the option to know which user made that request. For these requests you always need to be logged in, so I'm trying to access the header of the request in order to the the JWT Token but queryRunner.data is empty.
So far I have this code:
public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): any {
const type = query.substring(0, 6);
if ((type === 'INSERT' || type === 'UPDATE' || type === 'DELETE') && !query.includes('INSERT INTO "log"')) {
const log = new Log();
console.log(queryRunner.data);
const requestUrl = queryRunner && queryRunner.data.request ? `(${queryRunner.data.request.url}) ` : '';
console.log(`${requestUrl}executing query: `);
log.query = query;
log.parameters = parameters?.toString();
log.type = type;
getRepository(Log).save(log).then();
}
}
As you can see I added the bits of code that TypeORM display in their documentation to show the URL where the request was sent from (I think). But It's always empty...
Not sure if the reason why it's empty is because I'm locally.
If you got any ideas please let me know.