nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inject ConfigService into an Entity or Subscriber

asijoumi opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Hi,

I would like to know how can I inject the ConfigService into en Entity or Subscriber.

I tried with 2 different ways :

@Entity()
@Injectable()
export class User extends BaseEntity {
  constructor(private readonly configService: ConfigService) {
    super();
  }
}

But, configService is undefined.

The second way was to create a subscriber :

import { Inject } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { EntitySubscriberInterface, EventSubscriber } from 'typeorm';
import { User } from '../models/entities/user';

@EventSubscriber()
export class UserSubscriber implements EntitySubscriberInterface<User> {
  @Inject(ConfigService) private readonly configService: ConfigService;

  listenTo() {
    return User;
  }

  afterLoad(entity: User) {
    console.log(this.configService);
    console.log(`AFTER ENTITY LOADED: `, entity);
  }
}

But still the same problem.

Do you now how can I access to configService ?

PS : I need configService because some secrets are provided during the startup of application.

Minimum reproduction code

No

Steps to reproduce

No response

Expected behavior

Have configService

Package version

10.0.1

NestJS version

10.0.0

Node.js version

20.5.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

those subscriber classes are managed by typeorm, not by nestjs. So there's no way to make them injectable AFIAK

maybe you could find a work around tho

The other way was to get the NestApplication instance (like we can do with Java Spring). Is that possible with Nest ?

I actually have a Rest API working fine.

The last solution (but I didn't really approved) is a static class with the necessary variables..