Home > other >  NestJs : inject property to an extended class and pass it to super()
NestJs : inject property to an extended class and pass it to super()

Time:01-10

I'm trying to use NestJs and I'm facing an issue that should be easy to fix, but I can't find a clean solution.

Basicaly, I have an abstract class that needs a private connection property, set via the constructor properties. I have another class that extends this abstract Class.

I have create a database.module that initiates the db connection as a provider. This provider is imported in the module(s).

When instantiating the Child class, I need to inject this db connection to the abstract class. And I can not find a propery way to do it. I found some bad solution (basically not using the @Inject functionnality) but this is not a clean way IMO/

Any help would be appreciate.

I tried many things (removing private key word in the Child Class but doesn't work)...

Example of class I want to use

abstract class AbstractClass<T extends Record<string, any>> {
  constructor(private readonly _dbConnection: string, value: T) {
    console.log(value);
  }

  showMeConnection(): string {
    return this._dbConnection;
  }
}

@Injectable()
class Child<T extends Record<string, any>> extends AbstractClass<T> { //<-- error here : Class 'Child<T>' incorrectly extends base class 'AbstractClass<T>'
  constructor(@Inject("DATASOURCE") readonly _dbConnection: string, value: T) { // <--- error here : _dbConnection not used
    super(_dbConnection, value);
  }
}

const t = new Child<number>(5); //<-- doens't work, need _dbConnection, but that shouldn't as it is a private injected proprty...

database provider :

const databaseProviders = [
  {
    provide: "DATASOURCE",
    useFactory: async (): Promise<string> => {
      const dataSource: string= await dbConnection();
      return dataSource;
    },
  },
];

export { databaseProviders };

database module that I import in the Module of the class here above:

@Module({
  providers: [...databaseProviders],
  exports: [...databaseProviders],
})
export class DatabaseModule {}

CodePudding user response:

I don't understand why your AbstractClass is abstract it does not contain any abstract method, it is the first time I see somthing like this. maybe the error is caused by that.

AbstractClass doesn't have to be abstract in your example try to declare it as simple class.

and if AbstractClass contains some abstract methodes that you didn't show in your example, so Chlid class must implement all of them

CodePudding user response:

If you want _dbConnection to be available to child classes and injected by Nest, first you need to not call new yourself, as that will stop all Dependency Injection for that instance and you'll be left managing everything. Next, you should move the @Inject() from the constructor to a property so that child classes can extend the original class and still let Nest handle the injection of the property (just don't use that property inside the constructor, use something like onModuleInit() instead).

Another option would be to make the AbstractClass a mixin instead, a function that takes in options and returns a reference to a class. You can view the AuthGuard from @nestjs/passport for a good example of this.

  • Related