I have a BeforeInsert
and AfterInsert
hook that is not being called. All it does is log hook
. I am using nestjs-graphql.
You can run this example doing npm i
, then sending a mutation (playground is at localhost:3000/graphql)
with body createUser(createInput:{password:"password" email:"test@test.com"})
. It should succeed and log hook
.
The hook (nothing is logged):
@Entity('user')
export default class UserEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
@IsEmail()
email: string;
@Column()
@Length(7, 42)
password: string;
@BeforeInsert()
@AfterInsert()
async validate() {
console.log("hook")
}
}
It's called from a service. The insertion does not throw an error, and here2
is logged:
@Injectable()
export class UserService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: Repository<UserEntity>,
) { }
async createNew(email: string, password: string): Promise<number> {
console.log("here2")
const hashedPassword = "test"
const res = await this.userRepository.insert({ email, password: hashedPassword })
return res.identifiers[0].id
}
}
According to TypeORM's Docs the listener hooks get called before or after their respective operation when using the entity manager or repository's save
method. As insert
and similar operations don't call save
, they don't execute the @Before/After*
methods