简体   繁体   English

如何在nestjs中使用自定义class文件维护nestjs的规则

[英]How to use custom class file in nestjs maintaining the rule of nestjs

There is a Crypto class by custom made in nestjs.在 Nestjs 中定制了一个 Crypto class。

import * as bcrypt from 'bcrypt';

export class Crypto {
  constructor() {
  }

  public async hash(target: string, salt: number): Promise<string> {
    return await bcrypt.hash(target, salt);
  }

  public async compareHash(
    target: string,
    hash: string,
  ): Promise<boolean> {
    return await bcrypt.compare(target, hash);
  }
}

And I made this instance in a service of nestjs as below.我在 Nestjs 的服务中创建了这个实例,如下所示。

  public async create(createConfigDto: CreateConfigDto): Promise<IConfig> {
    const crypto = new Crypto();
    const hashedPassword = await crypto.hash(createConfigDto.password, 10);

    const newConfig = await new this.configModel({
      ...createConfigDto,
      password: hashedPassword,
    });
    return newConfig.save();
  }

  public async read() {
      const crypto = new Crypto();
    const hashedPassword = await crypto.compare(createConfigDto.password, hash);
  ...
  }

or I can do this crypto instance outside of create and read method to avoid duplicate call instance.或者我可以在创建和读取方法之外执行此加密实例以避免重复调用实例。
But my major question is about there is more efficient maintaining covention of nestjs for this case.但我的主要问题是,对于这种情况,nestjs 的维护约定更有效。
Could you give me some advice for me?你能给我一些建议吗? Thank you for reading my question.感谢您阅读我的问题。

To make your custom class Crypto a single instance and shared across the entire application, you should use @Injectable() decorator above your custom class要使您的自定义 class Crypto成为单个实例并在整个应用程序中共享,您应该在自定义 class 上方使用@Injectable()装饰器

 import * as bcrypt from 'bcrypt';

@Injectable()
export class Crypto {
  constructor() {
  }

  public async hash(target: string, salt: number): Promise<string> {
    return await bcrypt.hash(target, salt);
  }

  public async compareHash(
    target: string,
    hash: string,
  ): Promise<boolean> {
    return await bcrypt.compare(target, hash);
  }
}

After that, you should register your class (or service) in the module that contains this custom service:之后,您应该在包含此自定义服务的模块中注册您的 class(或服务):

@Module({
  ...
  providers: [Crypto],
})

and finally, to use this service you can add to the constructor of your service最后,要使用此服务,您可以添加到服务的构造函数中

constructor(private crypto: Crypto) {}
    ....
   
    public async create(createConfigDto: CreateConfigDto): Promise<IConfig> {
    const hashedPassword = await this.crypto.hash(createConfigDto.password, 10);

    const newConfig = await new this.configModel({
      ...createConfigDto,
      password: hashedPassword,
    });
    return newConfig.save();
  }

  public async read() {
    const hashedPassword = await this.crypto.compare(createConfigDto.password, hash);
  ...
  }

I recommend you to check this part of the official documentation providers我建议您查看官方文档提供者的这一部分

On the other hand, your custom service look that you may use it across the entire application, for that you can create a shared module and add all custom shared services on it, to understand more about that you can read this topic Shared Module另一方面,您的自定义服务看起来您可以在整个应用程序中使用它,因为您可以创建一个共享模块并在其上添加所有自定义共享服务,以了解更多信息,您可以阅读此主题共享模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM