import { Connection , EntitySubscriberInterface , EventSubscriber , InsertEvent } from 'typeorm' ; import { User } from '../user.entity' ; import * as bcrypt from 'bcrypt' ; import { ConfigService } from '@nestjs/config' ; @ EventSubscriber ( ) export class UserSubscriber implements EntitySubscriberInterface < User > { private readonly bcryptSalt : number ; constructor ( connection , private readonly configService ) { connection . subscribers . push ( this ) ; this . bcryptSalt = configService . get < number > ( 'bcryptSalt' ) ; } listenTo ( ) { return User ; } async beforeInsert ( event ) { const { password } = event . entity ; event . entity . password = await bcrypt . hash ( password , this . bcryptSalt ) ; } }