TypeORM挂钩未被触发,包括最小的项目

问题描述 投票:0回答:1

我有一个简单的BeforeInsertAfterInsert钩子没有被调用。它所做的只是日志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
    }
}
sqlite graphql nestjs typeorm
1个回答
1
投票

根据TypeORM's Docs,在使用实体管理器或存储库的save方法时,将在其各自的操作之前或之后调用侦听器挂钩。由于insert和类似操作不会调用save,因此它们不会执行@Before/After*方法

© www.soinside.com 2019 - 2024. All rights reserved.