toss / nestjs-aop

A way to gracefully apply AOP to nestjs
MIT License
213 stars 24 forks source link

[Feature]:useFactory 를 통한 LazyDecorator 공급자 등록 기능 #20

Closed KIMBEOBWOO closed 1 year ago

KIMBEOBWOO commented 1 year ago

Overview

{
  provide: TRANSACTION_DECORATOR,
  useFactory: (dataSource: DataSource) => {
    const clsTransactionDacorator = new ClsHookedTransactinoDecorator(
      dataSource,
    );

    return clsTransactionDacorator;
  },
  inject: [getDataSourceToken(POSTGRES_CONNECTION)],
},

위와 같이 LazyDecorator 를 모듈 공급자로 등록하고, 등록할 LazyDecorator 는 아래와 같이 @Aspect 를 구성했습니다.

@Aspect('TRANSACTION_DECORATOR')
export class ClsHookedTransactinoDecorator
  implements LazyDecorator<any, TransactionOptions>

하지만 해당 라이브러리의 lookupLazyDecorators 메서드에서

const aspect = this.reflector.get<string>(ASPECT, metatype);

의 결과가 undefined 라서 AOP 대상으로 찾아지지 않아 AOP 를 적용 할 수 없습니다. useFactory 로 인해 Aspect 메타데이터가 사라져서 그런것으로 이해됩니다. 혹시 제가 잘못 이해해 사용한 부분이 있다거나, 그렇지 않다면 향후 지원해주실 계획이 있는지 궁금합니다!

Describe the solution you'd like

<<LazyDecorator 를 모듈 공급자로 등록한 파일>>
  providers: [
    {
      provide: TRANSACTION_DECORATOR,
      useFactory: clsTransactionDacoratorFactory,
      inject: [getDataSourceToken(POSTGRES_CONNECTION)],
    },
  ],
})

Reflect.defineMetadata(
  ASPECT,
  TRANSACTION_DECORATOR,
  clsTransactionDacoratorFactory,
);

팩토리 메서드를 타깃으로 직접 메타데이터를 설정하는 방법

Additional context

WhiteKiwi commented 1 year ago

제보 감사드립니다 metatype 또는 instace 의 prototype에서 뽑아오도록 수정하면 될 것 같습니다 수정해보겠습니다 :)

WhiteKiwi commented 1 year ago

~오늘중으로 2.0.3에 나갈 예정입니다~ 2.0.3으로 배포나갔습니다. 확인부탁드려요 :)

KIMBEOBWOO commented 1 year ago

빠른 수정 감사합니다! 😄 혹시 npm 패키지 업데이트도 가능할까요??

WhiteKiwi commented 1 year ago

헙 이럴수가 npm publish가 안되었네요 지금 해두겠슴다 확인 감사합니다 :done: