import type { EntitySubscriberInterface , InsertEvent , UpdateEvent , } from 'typeorm' ; import { EventSubscriber } from 'typeorm' ; import { generateHash } from '../common/utils' ; import { UserEntity } from '../modules/user/user.entity' ; @ EventSubscriber ( ) export class UserSubscriber implements EntitySubscriberInterface < UserEntity > { listenTo ( ) { return UserEntity ; } beforeInsert ( event ) { if ( event . entity . password ) { event . entity . password = generateHash ( event . entity . password ) ; } } beforeUpdate ( event ) { const entity = event . entity as UserEntity ; if ( entity . password !== event . databaseEntity . password ) { entity . password = generateHash ( entity . password ! ) ; } } }