javascriptnestjstypeormnode.js-typeormnestjs-config

TypeORM / NestJS - @BeforeUpdate hook not working


I'm having problems using TypeOrm hook "BeforeUpdate"

I'm trying to update the user entity password, by passing a simple string and calling the save method to trigger the beforeUpdate hook and then hash the password, but this hook is not working while calling the save method.

This is what i have

user.service.ts

async update(id: number, updateUserDto: UpdateUserDto) {
  const roles =
    updateUserDto.roles &&
    (await Promise.all(
      updateUserDto.roles.map((name) => this.preloadRoleByName(name))
    ));
  const user = await this.userRepository.findOneOrFail(id);
  if (!user) {
    throw new NotFoundException(`User with ID #${id} not found`);
  }
  const toSaveUser = {
    ...user,
    ...updateUserDto,
    roles,
  };
  return await this.userRepository.save(toSaveUser);
}

user.entity.ts

.
.
.
@Column()
@Exclude()
password: string;

@BeforeInsert()
@BeforeUpdate()
private async hashPassword() {
  const rounds = 10;
  const salt = await bcrypt.genSalt(rounds);
  this.password = await bcrypt.hash(this.password, salt);
}

user.controller.ts

@Patch(":id")
@UseInterceptors(ClassSerializerInterceptor)
async update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
 return await this.usersService.update(+id, updateUserDto);
}

What I'm doing wrong?

BeforeInsert hook works or if I call userRepository.preload() method to update it works but it doesn't replace the relationship of the role, that's why I take this approach.

Any ideas?


Solution

  • Something to be aware of when using the triggers is that they might require instances of the entity to run.

    Problem:

    const user = await this.userRepository.findOneOrFail(id); // entity instance
    const toSaveUser = { ...user, ...updateUserDto, roles }; // plain object
    return await this.userRepository.save(toSaveUser); // not running trigger
    

    Solution:

    const user = await this.userRepository.findOneOrFail(id); // entity instance
    // still entity instance
    const toSaveUser = this.userRepository.create({
      ...user,
      ...updateUserDto,
      roles,
    });
    return await this.userRepository.save(toSaveUser); // running trigger
    
    

    And you should be good.

    Since you are using repository.save() with an arbitrary object in your code, the trigger does not run. If we instead use repository.create() to create the instance, the trigger will now run.

    The reason why your solution worked with repository.preload() is because it returns an instance. Repository api examples