Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manually registered subscribers not called from transactional #3857

Closed
zoltan-mihalyi opened this issue Dec 18, 2022 · 1 comment
Closed

Comments

@zoltan-mihalyi
Copy link

Describe the bug
When i register the subscriber manually using em.getEventManager().registerSubscriber(this) the event handlers are not called when events are happening in a transactional callback.

To Reproduce
Steps to reproduce the behavior:

  1. Make the following NestJS service:
import type { EventSubscriber } from '@mikro-orm/core';
import { EntityManager } from '@mikro-orm/postgresql';
import { Injectable } from '@nestjs/common';
import User from '../entities/User';

@Injectable()
export default class MyService implements EventSubscriber<User> {
    public constructor(private em: EntityManager) {
        em.getEventManager().registerSubscriber(this);
        console.log('subscribed');
        this.test();
    }

    public onFlush() {
        console.log('onFlush');
    }

    public async test() {
        await this.em.transactional(async (em) => {
            const user = this.em.getRepository(User).create({});
            em.persist(user);
        });
    }
}
  1. Instantiate the service (either manually or by NestJS)
  2. "subscribed" is logged, but "onFlush" is not.

Expected behavior
Event handlers should be called and onFlush

Additional context
I figured out that in EntityManager.ts, there is this code:

   // in transactional:
    const fork = em.fork({
      clear: false, // state will be merged once resolves
      flushMode: options.flushMode,
      freshEventManager: true,
    });
    
    //in fork:
    const eventManager = options.freshEventManager ? new EventManager(em.config.get('subscribers')) : em.eventManager;

which means that the fresh event manager uses subscribers from config, but ignores subscribers registered via registerSubscriber()

Versions

Dependency Version
node 16.13.2
typescript 4.6.2
mikro-orm 5.4.2
your-driver postgresql
@B4nan B4nan closed this as completed in 0e523b3 Dec 18, 2022
@zoltan-mihalyi
Copy link
Author

For the record: it was fixed in commit: 0e523b3

And will be released in v5.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant