Reputation: 405
I want to find a table row by request parameter. I know how to do it in service but I'm trying to do it also in decorator.
My decorator:
import { BadRequestException, createParamDecorator, ExecutionContext } from '@nestjs/common';
export const GetEvent = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const { eventId } = request.params;
// Something like in service:
// const event = await this.eventModel.findByPk(eventId);
// return event;
});
I know that it's impossible to inject service in decorator but maybe some hacks to make database requests before calling service methods?
Upvotes: 10
Views: 5146
Reputation: 70111
Theoretically, you could use a pacakge directly (like if you use TypeORM you could use the typeorm
package), but there's a few things to note:
Upvotes: 13