TypeORM:从订阅者中排除种子
TypeORM: exclude seed from subscriber
我目前正在使用 TypeORM 库,我遇到了种子和订阅者的问题
问题是每次我 运行 为用户播种时,我的订阅者都会被触发并记录新的插入
我怎样才能排除来自种子的插入,并且只记录那些通过 App 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
}
}
对于每个数据库操作(查询),都可以指定是否应调用侦听器:请参阅 SaveOptions and QueryBuilder。因此,对于您的种子,请为每个数据库操作禁用 listeners/subscribers。
我目前正在使用 TypeORM 库,我遇到了种子和订阅者的问题 问题是每次我 运行 为用户播种时,我的订阅者都会被触发并记录新的插入 我怎样才能排除来自种子的插入,并且只记录那些通过 App 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
}
}
对于每个数据库操作(查询),都可以指定是否应调用侦听器:请参阅 SaveOptions and QueryBuilder。因此,对于您的种子,请为每个数据库操作禁用 listeners/subscribers。