Search code examples
node.jssequelize.jstypeormtypeorm-datamapper

TypeORM: exclude seed from subscriber


Im currently working with TypeORM library, and Im having issue with seeds & subscribers The problem is every time I run seed for users, my subscriber is triggered and record new inserts How can I exclude inserts that are comming from seed, and only record the ones that are preformed trough 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
    }



}

Solution

  • For each database operation (query) it can be specified if listeners should be called: See SaveOptions and QueryBuilder. So for your seeds, disable listeners/subscribers for each database operation.