-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Comments
You define the custom repository, and it will be used automatically. If you use One way to make sure the file will be loaded is to link it from the entity definition. To do so, you can use @Entity()
export class Author {
[EntityRepositoryType]: CustomAuthorRepository;
} Now this ensures the constructor(
@InjectRepository(Author)
private readonly articleRepository: CustomAuthorRepository,
) {} |
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 |
I still don't follow what you mean. So you don't like the 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 @Repository()
export class AuthorRepository ... {}
constructor(private repo: AuthorRepository) {} |
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... |
Is there any way to specify |
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, |
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
The text was updated successfully, but these errors were encountered: