In my project, I have developed a class called S3Service
that handles the task of uploading and deleting objects (such as images) from S3. Since I intend to utilize this "service" in various modules, I decided to create a custom module named UtilsModule
where I plan to house a collection of reusable shared classes. Subsequently, I successfully exported the S3Service
class from my UtilsModule
.
@Injectable()
export class S3Service {
constructor(@InjectS3() private readonly client: S3) {}
async removeObject(): Promise<S3.DeleteObjectOutput> {}
async uploadObject(): Promise<S3.ManagedUpload.SendData> {}
}
@Module({
providers: [S3Service],
exports: [S3Service],
})
export class UtilsModule {}
I proceeded to import the UtilsModule
into the app module.
@Module({
imports: [
// Other modules here
UtilsModule,
],
})
export class AppModule {}
Then, I imported it into a module that requires functionalities to upload or remove objects from S3.
@Module({
imports: [
// Other modules
TypeOrmModule.forFeature([ProfileRepository]),
UtilsModule,
],
controllers: [ProfileController],
providers: [ProfileService],
})
export class ProfileModule {}
Subsequently, I injected it using the @Inject
decorator into the desired repository.
@EntityRepository(Profile)
export class ProfileRepository extends Repository<Profile> {
constructor(
@Inject() private s3Service: S3Service,
) {
super();
}
}
Although there were no compilation errors with my application, I encountered an Internal Server Error
when invoking this service via a Post request. Upon debugging, I noticed that the uploadObject
function seems to be returning as undefined.
After doing some research online, I stumbled upon this thread which mentioned that TypeORM repositories may not support Dependency Injection. Is there a workaround for this issue? Should I consider instantiating this class within the repository instead?