#How to Inject @InjectableProxy from an application into a library

4 messages · Page 1 of 1 (latest)

rich canyon
#

Hello,

In my app I have a Proxy Provider class with @InjectableProxy.
To register it, I'm using the ClsModule.forFeature() to import it in my DatabaseModule.

@InjectableProxy()
export class User {}
@Module({
  imports: [ClsModule.forFeature(User)],
  providers: [DatabaseService]
})
export class DatabaseModule
@Injectable()
export class DatabaseService {
  constructor(private readonly user: User)
}

It's all working fine, but now I want to move my DatabaseModule and DatabaseService into a separate library and keep User into the application.
Any idea on how to still inject User into DatabaseService please ?

tardy heath
#

The solution has really nothing to do with nestjs-cls:

So the database service that depends on the user from the application will be extracted to a library?

You'll need to define an interface (or abstract class) for the User in the database library, because a library can't depend on the application's definition of User.
The application's User will have to implement the interface.

Then, you'll need an injection token for the User in the database library (if you went with the abstrac class route, you can use the abstract class itself.

Next, your database module will need to become a dynamic module that can be passed the injection token for User from the application and assign it (via useExisting) to the injection token in the database module.

#

But instead of the infrastrucuture, you'll be providing an implementation for the User provider