Issue with Dependency Injection in NestJS
Currently, I am working on implementing SOLID principles in my NestJS project by decoupling my service layer from TypeOrm.
One of the benefits of this approach is the ability to switch between using an InMemoryRepository
for testing and a TypeOrmRepository
for production.
However, I have encountered a problem with injecting my custom TypeOrm repository as a provider. It seems that the @InjectRepository()
decorator and TypeOrm are not behaving as expected in this specific scenario.
Interestingly, I can inject my In Memory repositories without any issues in my unit tests.
[Nest] 12311 - 18/09/2021 at 06:22:43 [ExceptionHandler] Cannot read property 'save' of undefined
TypeError: Cannot read property 'save' of undefined
at TypeOrmWorkoutRepository.Object.<anonymous>.Repository.save (/Users/arthurmehmetoglu/Development/CorpoSano/back/src/repository/Repository.ts:150:29)
at WorkoutService.create (/Users/arthurmehmetoglu/Development/CorpoSano/back/src/workout/workout.service.ts:30:35)
at WorkoutResolver.create (/Users/arthurmehmetoglu/Development/CorpoSano/back/src/workout/workout.resolver.ts:22:32)
...
For more information and code snippets, please refer to the sections below.
Code Samples
workout.module.ts
...
workout.service.ts
...
workout.service.spec.ts
...
workout-repository.interface.ts
...
in-memory-exercise.repository.ts
...
typeorm-workout.repository.ts
...
Environment
Nest version: 7.6.0
Node version: v14.16.1
Platform: Mac
Additional Resources:
Note: The provided code snippets have been condensed for clarity.