Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There is a Crypto class by custom made in nestjs.

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.

  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.
Could you give me some advice for me? Thank you for reading my question.

like image 797
DD DD Avatar asked Oct 26 '25 18:10

DD DD


1 Answers

To make your custom class Crypto a single instance and shared across the entire application, you should use @Injectable() decorator above your custom class

 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:

@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

like image 173
Youba Avatar answered Oct 29 '25 08:10

Youba