类型ORM:从订阅者中排除种子



我目前正在使用TypeORM库,并且我在种子和订阅者方面遇到问题 问题是每次我为用户运行种子时,我的订阅者都会被触发并记录新的插入 如何排除来自种子的插入,而只记录通过应用程序UI预先形成的插入

import { EntitySubscriberInterface, EventSubscriber, InsertEvent, UpdateEvent } from 'typeorm';
import { User } from '../users/user.entity';

@EventSubscriber()
export class HistorySubscriber implements EntitySubscriberInterface<User> {
/**
* Indicates that this subscriber only listen to User events.
*/
listenTo() {
return User;
}
/**
* Called before User insertion.
*/
// eslint-disable-next-line no-unused-vars
async afterInsert(event: InsertEvent<User>) {
// this gets called multiple times even with seed
}

}

对于每个数据库操作(查询(,可以指定是否应调用侦听器: 请参阅保存选项和查询生成器。因此,对于种子,请禁用每个数据库操作的侦听器/订阅者。

最新更新