03 / 07

What is Reflector and how does it work with SetMetadata in NestJS?

SetMetadata(key, value) attaches metadata to a class or method using Reflect.defineMetadata. Reflector is a helper injected into guards and interceptors to read that metadata using getAllAndOverride or getAllAndMerge. This is the standard pattern for RBAC guards and feature flags.

SetMetadata + Reflector pattern for RBAC
Reflector methods:
  1. 1

    get(key, target) — reads metadata from a single target.

  2. 2

    getAllAndOverride(key, targets) — returns first defined value across targets; method overrides class.

  3. 3

    getAllAndMerge(key, targets) — merges arrays/objects from all targets.