Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何将NestJS配置注入类型化实体?_Typescript_Nestjs_Typeorm_Nestjs Config - Fatal编程技术网

Typescript 如何将NestJS配置注入类型化实体?

Typescript 如何将NestJS配置注入类型化实体?,typescript,nestjs,typeorm,nestjs-config,Typescript,Nestjs,Typeorm,Nestjs Config,让我们简化类型实体用户: @Entity() export class User extends BaseDatabaseEntity { @Column({ length: 255, }) public firstName!: string; @Column({ length: 60 }) @Exclude() public password: string; @BeforeUpdate() @BeforeInsert() private has

让我们简化类型实体
用户

@Entity()
export class User extends BaseDatabaseEntity {
  @Column({
    length: 255,
  })
  public firstName!: string;

  @Column({ length: 60 })
  @Exclude()
  public password: string;

  @BeforeUpdate()
  @BeforeInsert()
  private hashPassword(): void {
    const tmpPassword = hashSync(
      this.password + 'config.auth.password.secret',
      genSaltSync(),
    );
    this.password = tmpPassword;
  }
}

}
我需要用NestJS的配置(来自ConfigService的命名空间)替换
config.auth.password.secret

,但实体不是NestJS结构的一部分,所以我不能像往常一样注入它


如何在TypeORM实体中实现NestJS配置?

实体不是NestJS中可注入提供程序的一部分,因此本质上这是不可能的。不过,您可以将其作为服务代码的一部分,并在插入/更新之前对其进行密码哈希运算。

我也需要它,并找到了解决方法。正如前面所说的,您不能将配置注入实体

我提出了用实体中需要的配置值导出对象的解决方案:

初始化应用程序模块中的配置:

  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [defaultConfig]
    }),
// .....other imports
  ]
defaultConfig
是一个收集和检查配置值的函数。除此之外,它还将值设置为
STATIC\u CONFIG
object

export const STATIC_CONFIG = {
  WEB_APP_URL: '',
  BASE_URL: '',
};

export const defaultConfig = () => {
  const port = +(process.env[ENV_SERVER.PORT] || 3000);
  const production = process.env.NODE_ENV === 'production';
  const retVal = {
    PRODUCTION: production,
    PORT: port,
    BASE_URL: process.env.BASE_URL || 'http://localhost:3000',
    URL_PREFIX: process.env.URL_PREFIX || 'http://localhost:3000',
//    ..... plenty of other values
  }

  if (retVal[ENV_S3.HOST] && !(retVal[ENV_S3.ACCESS] && retVal[ENV_S3.SECRET])) {
    // tslint:disable-next-line:no-console
    console.error('S3 configuration error: no access or secret set; exiting');
    process.exit(1);
  }

  STATIC_CONFIG.WEB_APP_URL = retVal.WEB_APP_URL;
  STATIC_CONFIG.BASE_URL = retVal.BASE_URL;

  return retVal;
};

最后,在我的实体中,我使用
静态配置
对象,例如:

  @Expose({ name: 'image', toPlainOnly: true, groups: [TG_MOBILE] })
  get mobileAppImage() {
    return this.id ? `${STATIC_CONFIG.BASE_URL}/static/image/${this.id}` : undefined;
  }

是的,这正是我想要做的,但我不知道如何使实体成为NestJS的一部分。你能举个例子吗?你不能。我就是这么说的。实体不是
@Injectable()
,也没有计划创建它们。相反,您应该将hasing逻辑移到您的服务中。更深入地解释了为什么我应该从实体中完全删除方法,或者将它们变成这样:
hashPassword(generator:IUserPassGenerator)
?如果您需要访问应该注入的服务,您需要在使用实体的服务中创建逻辑,或者将所需的服务注入到使用实体的服务中,并将所需的服务传递给实体的方法。就我个人而言,我更倾向于前者而不是后者
  @Expose({ name: 'image', toPlainOnly: true, groups: [TG_MOBILE] })
  get mobileAppImage() {
    return this.id ? `${STATIC_CONFIG.BASE_URL}/static/image/${this.id}` : undefined;
  }