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 inject a custom repository #39

Closed
andrzepakula opened this issue Jul 8, 2018 · 11 comments
Closed

How to inject a custom repository #39

andrzepakula opened this issue Jul 8, 2018 · 11 comments
Labels

Comments

@andrzepakula
Copy link

andrzepakula commented Jul 8, 2018

[x] Support request

Hi. How to inject a custom repository http://typeorm.io/#/custom-repository ? And second question this indicates that we shouldn't be creating services from custom repositories, especially if you want to use transactions. What do you think
? Best Regards

@xmlking
Copy link

xmlking commented Jul 8, 2018

So far I am implementing like this.
Wanted to validate with @kamilmysliwiec if this is the intended way of use for custom repositories!

project.repository.ts

import { Connection, EntityRepository } from 'typeorm';
import { Project } from './project.entity';
import { Injectable } from '@nestjs/common';
import { AuditMongoRepository } from '../core/repositories/audit-mongo.repository';

@Injectable()
@EntityRepository(Project)
export class ProjectRepository extends AuditMongoRepository<Project> {
}

export const ProjectRepositoryProvider = {
  provide: 'ProjectRepository',
  useFactory: (connection: Connection) => connection.getCustomRepository(ProjectRepository),
  inject: [Connection],
};

audit-mongo.repository.ts

export abstract class AuditMongoRepository<Entity> extends MongoRepository<Entity> {
// overwrite some methods for audit
}

project.module.ts

@Module({
  imports: [
    SharedModule,
    TypeOrmModule.forFeature([Project])],
  providers: [
    ProjectService,
    ProjectRepositoryProvider,
  ],
  controllers: [ProjectController],
})
export class ProjectModule {}

project.service.ts

@Injectable()
export class ProjectService  {
  private readonly logger = new Logger(ProjectService.name);
  constructor(
    private readonly projectRepository: ProjectRepository,
  ) {
  }
}

@xmlking
Copy link

xmlking commented Jul 8, 2018

I also see following implementation, but it doesn't work for me :(

https://github.com/QuentinouLeTwist/nestjs-vuejs-boilerplate/tree/master/backend/src/modules/api/user

@andrzepakula
Copy link
Author

andrzepakula commented Jul 8, 2018

I think that this is not the way to solve this problem, because @kamilmysliwiec did not respond to the next comments in the thread #14. Functionality is theoretically merged, but it is hard for me to understand how to implement it only after the changes .. A more important problem is that the TypeOrm documentation does not recommend the global registration of custom repositories, as they are not a single instance .

@xmlking
Copy link

xmlking commented Jul 16, 2018

Looks like custom repositories are injected same way like entities

https://github.com/evebook/api/tree/master/src/modules/character

@mentos1386
Copy link
Contributor

mentos1386 commented Jul 17, 2018

@xmlking Yeah, they are injected same way as entities. It was added with this PR #12

It should probably be noted in documentation that there is a way of injecting them...

EDIT: It is documented in the docs https://docs.nestjs.com/techniques/database under Repository Pattern

@kamilmysliwiec
Copy link
Member

EDIT: It is documented in the docs https://docs.nestjs.com/techniques/database under Repository Pattern

So is there anything that still should be clarified?

@et
Copy link

et commented Jul 27, 2018

@kamilmysliwiec - that only demonstrates how to set up the entity.
I am very grateful for @xmlking sharing the custom repository example. As obvious as the implementation is to do it, it wasn't clear to me how to actually achieve it. Maybe having something in the nestjs samples folder would help.

@MattIzSpooky
Copy link

MattIzSpooky commented Aug 13, 2018

Hello everyone, I ran into the same issue and I finally figured it out how to do it. I don't agree with the first answer given because you aren't supposed to make the repository a provider/service as is written here. This is not documented so that's why I'd like to share how I did it.

Edit: I created a separate issue that can be found here. You can find the answer there.

@kamilmysliwiec
Copy link
Member

Just created an issue in the documentation repository. I'll use @MattIzSpooky example and share solution very soon :)

@Roms1383
Copy link

Looks like custom repositories are injected same way like entities

https://github.com/evebook/api/tree/master/src/modules/character

Sadly this link is broken 😿

@kamilmysliwiec
Copy link
Member

@nestjs nestjs locked as resolved and limited conversation to collaborators Mar 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants