我有一个简单的BeforeInsert
和AfterInsert
钩子没有被调用。它所做的只是日志hook
。我正在使用nestjs-graphql,但我不认为这是问题。 Here是带有示例的最小Github回购。您可以通过执行npm i
然后发送一个变异来运行它(操场位于localhost:3000/graphql)
,主体为createUser(createInput:{password:"password" email:"[email protected]"})
。它应该成功并登录hook
。
Here是该钩子,未记录任何内容:
@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")
}
}
从服务here中调用。插入不会引发错误,并且会记录here2
:
@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
}
}
根据TypeORM's Docs,在使用实体管理器或存储库的save
方法时,将在其各自的操作之前或之后调用侦听器挂钩。由于insert
和类似操作不会调用save
,因此它们不会执行@Before/After*
方法