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

[BUG] Custom Inject Decorator Issue #2710

Closed
KimTaeHyung91 opened this issue May 20, 2024 · 2 comments
Closed

[BUG] Custom Inject Decorator Issue #2710

KimTaeHyung91 opened this issue May 20, 2024 · 2 comments
Assignees

Comments

@KimTaeHyung91
Copy link

Describe the bug

I am using Mikro-ORM and am trying to use it by injecting the EntityManager object through DI, but it can only be injected based on property, so I am trying to create a separate decorator (like JPA's PersistenceContext) and use it, but instead of EntityManager, MikroOrmRegistry is returned. How can I solve this?

To Reproduce

// adapter
@Injectable()
export class EntityPersistenceAdapter {
  constructor(
    private readonly mapper: PostMapper,
    @PersistenceContext()
    private readonly em: EntityManager
  ) {}
}
// custom decorator
export const PersistenceContext = (
  contextName?: string,
): ParameterDecorator => {
  return Inject(
    MikroOrmRegistry,
    (registry: MikroOrmRegistry) => registry.get(contextName)?.em,
  ) as ParameterDecorator;
};

Expected behavior

// expect
console.log(
      'em instanceof EntityManager',
      this.em instanceof EntityManager,
    ); // true
// runtime
console.log(
      'em instanceof EntityManager',
      this.em instanceof EntityManager,
    ); // false

console.log(
      'em instanceof MikroOrmRegistry',
      this.em instanceof MikroOrmRegistry,
    ); // true

Code snippets

No response

Repository URL example

No response

OS

macOS

Node version

v20.13.1

Library version

v7.69.4

Additional context

No response

@Romakita
Copy link
Collaborator

Hello @KimTaeHyung91

Because isn't possible to hive custom resolver forv Inject decorator while it's used as constructor param decorator.
This is not a bug. And I don't want to implement that on constructor level. It will commlexify the DI.

Please just use the decorator Inject on propertuy and use $onInit hook instead the constructor to initialize your logic

Please this issue if all is fine.

See you

Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

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

No branches or pull requests

2 participants