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

How to use entity repository in service without injecting #6

Closed
Quadriphobs1 opened this issue Sep 4, 2020 · 6 comments
Closed

How to use entity repository in service without injecting #6

Quadriphobs1 opened this issue Sep 4, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@Quadriphobs1
Copy link

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(...) {
    // ...
  }

}
@Quadriphobs1 Quadriphobs1 added the enhancement New feature or request label Sep 4, 2020
@B4nan
Copy link
Member

B4nan commented Sep 4, 2020

You define the custom repository, and it will be used automatically. If you use @Repository() decorator, you need to make sure the file will be loaded, so the decorator can be executed (without that, the ORM does not know about such repository).

One way to make sure the file will be loaded is to link it from the entity definition. To do so, you can use EntityRepositoryType, that will also allow inference of the type of the repository:

@Entity()
export class Author {

  [EntityRepositoryType]: CustomAuthorRepository;

}

Now this ensures the @Repository() decorator will be executed and custom repository will be registered. The em.getRepository(Author) will be correctly typed to CustomAuthorRepository.

constructor(
  @InjectRepository(Author)
  private readonly articleRepository: CustomAuthorRepository,
) {}

@Quadriphobs1
Copy link
Author

Hmmm... Having to go through the @InjectResitory seems a little bit overly roundtrip to me, I was actually hoping for a way to either register it through the module as a provider or something just as services can be registered using the provider

@B4nan
Copy link
Member

B4nan commented 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) {}

@Quadriphobs1
Copy link
Author

Ohhh that seems better... Having to put the decorator on the repository is what I was actually concerned about... but removing also do the trick...

@B4nan B4nan closed this as completed in 2d8715a Sep 23, 2020
@nikkonrom
Copy link

nikkonrom commented Feb 11, 2021

Is there any way to specify [EntityRepositoryType]: CustomAuthorRepository; while defining entities through EntitySchema? customRepository: () => CustomAuthorRepository, seems to not fixing the problem.

@B4nan
Copy link
Member

B4nan commented 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.

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

No branches or pull requests

3 participants