NestJs Inject Service Dependency in Sequelize Logging function

Viewed 74

I am trying to make some logs about my NestJs app, I had a simple interceptor which add the request and the response to the database (Logs Table contain id, request, response, queries) based on request unique identifier (UUID), the problem is: Sequelize logging, How can I Log the queries that belongs to each request?

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {


        const [req] = context.getArgs();

        const id = randomUUID();
        Log.create({
            id,
            request: req.body
        })

        return next
            .handle()
            .pipe(
                tap((data) => {
                    Log.update({
                        response: data
                    }, {where: { id }})
                    return data;
                })
            );
    }
}

App Module imports:

  imports: [
      CatsModule,
      SequelizeModule.forRoot({
        ...config.get('db'),
        logging: (e) => {
        // get the request and insert the queries into Logs Table based on the UUID
        },
          logQueryParameters: true
      }),
      LoggingModule
  ],
0 Answers
Related