question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use entity repository in service without injecting

See original GitHub issue

The mikro-orm docs have a way to create a repository https://mikro-orm.io/docs/next/repositories however from the documentations and example there has been no way to use the repository like you would in typeorm

@Repository(Author)
export class CustomAuthorRepository extends EntityRepository<Author> {

  // your custom methods...
  public findAndUpdate(...) {
    // ...
  }

}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
B4nancommented, Sep 4, 2020

I still don’t follow what you mean. So you don’t like the InjectRepository bit, but are fine with using nest DI in general? You can make the repository injectable, the decorator is needed only when you do not have custom implementation.

This is how the repositories are registered, you can use the same way to register your custom repository:

https://github.com/mikro-orm/nestjs/blob/master/src/mikro-orm.providers.ts#L63

{
  provide: CustomAuthorRepository, 
  useFactory: (em: EntityManager) => em.getRepository(Author),
  inject: [EntityManager],
}

Or as long as you name your custom repository the same way as getRepositoryToken() helper would, you don’t need to do anything special:

@Repository()
export class AuthorRepository ... {}

constructor(private repo: AuthorRepository) {}
0reactions
B4nancommented, Feb 11, 2021

Just put it to the class/interface:

export interface IAuthor4 {
  [EntityRepositoryType]: AuthorRepository;
  id: number;
  name: string;
  email: string;
}

export const Author4 = new EntitySchema<IAuthor4>({
  name: 'Author4',
  properties: {
    id: { type: 'number', primary: true },
    name: { type: 'string' },
    email: { type: 'string', unique: true },
  },
  customRepository: () => AuthorRepository,
});

You need both, EntityRepositoryType is for TS inference, customRepository is for the ORM runtime metadata.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject repository in service without DI framework - Stack Overflow
I use JAX-RS (Jersey implementation) and Jetty as an embedded servlet container.I use JPA (Hibernate implementation) and an H2 in-memory ...
Read more >
How *not* to inject services in entities - // thinkbeforecoding
The usual way to work it out is to hold a reference to a data access object or a repository and to load...
Read more >
Services and dependency injection in Drupal 8+
As a developer, it is best practice to access any of the services provided ... Note: It's not possible to inject services to...
Read more >
How not to do dependency injection - the static or singleton ...
You can use an IoC container without doing dependency injection and in ... NET MVC Controller -> Service Layer -> Repository -> Entity...
Read more >
Use dependency injection in .NET Azure Functions
The use of constructor injection requires that you do not use static classes for injected services or for your function classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found