refactor(prisma): PrismaService 캐스팅 제거 + abstract class DI 토큰 (P2-2)#121
Conversation
…ctory (P2-2)
기존: PrismaClient 상속 + 생성자 return + 'as PrismaService' 캐스팅 (타입 함정).
신규: abstract class extends PrismaClient + useFactory.
- prisma.service.ts
- createExtendedPrismaClient() factory 함수
- PrismaService: abstract class extends PrismaClient
- abstract 라 `new PrismaService()` compile error → 잘못된 인스턴스화 원천 차단
- extends PrismaClient 로 model accessor / $connect 등 API 타입 노출
- declaration merging 불필요, ESLint 룰 disable 0, 캐스팅 0
- 생성자 return 트릭 / 'as PrismaService' / dead 메서드 모두 제거
- prisma.module.ts
- useFactory 로 PrismaService 토큰에 factory 결과 등록
- 모듈이 OnModuleInit/OnModuleDestroy 구현 (라이프사이클 owner)
- prisma.service.spec.ts
- factory 직접 호출 + softDelete 자동 필터 검증
- PrismaModule init/destroy 시 $connect/$disconnect 호출 검증
- PrismaService 토큰 주입 인스턴스의 모델 accessor 노출 검증
callsite 영향 0 (33 파일 그대로) — abstract class 의 TS 타입 그대로, NestJS DI 가 factory
결과를 토큰에 매핑.
검토 과정에서 (a) Symbol+@Inject 33 곳, (a') class+interface merging+rule disable,
(a'') abstract class extends 셋 비교 후 (a'') 가 disable/캐스팅/매직 패턴 모두 0 인 최적안임을
확인.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Coverage report
Test suite run success1241 tests passing in 144 suites. Report generated by 🧪jest coverage report action from c89ab19 |
Summary
P2-2.
PrismaService의 생성자 return 트릭 +as PrismaService캐스팅이 만들던 타입 함정을 제거. abstract class DI 토큰 +useFactory로 정공법 정리.검토 과정에서 (a) Symbol+@Inject 33 곳 / (a') class+interface declaration merging+ESLint rule disable / (a'') abstract class extends PrismaClient 셋 비교 후, (a'') 가 disable / 캐스팅 / 매직 패턴 모두 0 인 진짜 최적안임을 확인.
Scope
prisma.service.ts
Before
문제:
as PrismaService— extended client 가 PrismaService 라고 거짓 선언new PrismaService()가 다른 객체 반환 (JS 트릭, 가독성 저하)After
prisma.module.ts
라이프사이클이 모듈로 이동 — owner 명확화.
prisma.service.spec.ts
Test.createTestingModule({ imports: [PrismaModule] })로 모듈 인스턴스화 후 init/close 시$connect/$disconnect호출 검증비교표
@Inject(PRISMA_SERVICE)추가no-unsafe-declaration-merging필요new PrismaService()차단Impact
prisma.account.findMany(...)같은 호출이 그대로 작동prisma.service.ts100/100/100/100 유지Test plan
prisma.service.spec신규 4 케이스 — factory / softDelete / 모듈 라이프사이클 / 토큰 주입 모두 통과