Izweb Technologies
Izweb Technologies

Reputation: 158

how to scan all decorators value at runtime in nestjs

I have to collect all decorator value that appears in different place in my app as string and then saving them to database at runtime, i don't have to add them twice (in database and in code),

i have tried to do it but i could not figure out i use

Reflector api from nestjs as following

this.reflector.getAll<string>('access', context.getHandler())

but i could not get context.getHandler() during run time

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
   

  // Here is where i want to save

  await app.listen(3000);

}
bootstrap();

here is my decorator

@HashPermission('access_value')

Please assist

Upvotes: 4

Views: 3031

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70151

For something like this, you'll either need to make use of something like Nest's undocumented DiscoveryService or a package like @golevelup/nestjs-discovery which is a friendly wrapper around Nest's package. You can then make use of methods like this.discoveryService.methodsAndControllerMethodsWithMetaAtKey to get the classes and methods that have that metadata, then you can use the reflector class on each method to get the metadata value.

Upvotes: 7

Related Questions