node.js - TypeORM:从订阅者中排除种子

标签 node.js sequelize.js typeorm typeorm-datamapper

我目前正在使用 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
    }



}

最佳答案

对于每个数据库操作(查询),可以指定是否应调用监听器:参见 SaveOptionsQueryBuilder 。因此,对于您的种子,请为每个数据库操作禁用监听器/订阅器。

关于node.js - TypeORM:从订阅者中排除种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59747965/

相关文章:

node.js - 使用 sequelize 在 nodejs 和 Postgres 中插入时出错

sqlite - 我想根据今天的日期而不是根据时间来获取数据(sequelize 和 sqlite)

graphql - 如何修复 EntitySchema 和 Graphql 中的 'Cannot read property ' joinColumns' of undefined' 错误

postgresql - TypeORM 和 Postgres 竞争的命名风格

typescript - 类型错误 : null value in column violates not-null: allow null values ​in a fk

node.js - 尝试使用 this.refs.value 时,使用 this.refs 已被弃用错误

node.js - Mongoose findOne 不适用于特定查询,但适用于其他查询

javascript - Node.js 中未使用 RS256 算法生成 jsonwebtoken token

node.js - 尝试将 Node 服务器(Express)与 Postgres 后端(Sequelize)同步时出现 DataType 错误

javascript - 在 Electron 中调试 : how can I access render-scope objects from the console?