I created a class named: S3Service
which is responsible for uploading and deleting objects from S3 (pretty much images), since I wish to use the "service" (is there a better name?) in other modules, I decided to create a custom module: UtilsModule
where I hope to create a set of reusable shared classes. I managed to export this 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 did import this UtilsModule
into the app module.
@Module({
imports: [
// Other modules here
UtilsModule,
],
})
export class AppModule {}
And then import it into a module which needs to upload or remove objects from S3.
@Module({
imports: [
// Other modules
TypeOrmModule.forFeature([ProfileRepository]),
UtilsModule,
],
controllers: [ProfileController],
providers: [ProfileService],
})
export class ProfileModule {}
And finally inject it using the decorator @Inject
into the desired repository.
@EntityRepository(Profile)
export class ProfileRepository extends Repository<Profile> {
constructor(
@Inject() private s3Service: S3Service,
) {
super();
}
}
Here my application does compile but when I invoke this service via a Post request, a Internal Server Error
is thrown, I started debugging with breakpoints in this "service" but it looks like the uploadObject
function is undefined.
I read this thread and apparently TypeORM repositories are not subject for DI, is there a workaround this? Should I then instantiate this class in the repository?
constructor(@InjectS3() private readonly client: S3) {}
Do you import it like this while dealing with S3? – user10057478