I remember that middlewares often follow the onion layer behavior that we can execute codes after the handler function completed it will traverse back the layer. But, I this code is not following that behavior. How do I make sure that the part after next() is running after the handler (controller) function complete?
@Injectable()
export class TransactionMiddleware implements NestMiddleware {
constructor(private dbService: DBAuth) {}
async use(req: Request, res: Response, next: NextFunction) {
await this.dbService.db.transaction(async (tx) => {
this.dbService.tx = tx;
// TODO: how to not let this block finishes before the `next()` function completed.
next();
console.log('this runs before code inside next function');
});
}
}