Giter VIP home page Giter VIP logo

Comments (11)

MattIzSpooky avatar MattIzSpooky commented on May 22, 2024 4

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.

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024 4

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

from typeorm.

xmlking avatar xmlking commented on May 22, 2024 2

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,
  ) {
  }
}

from typeorm.

xmlking avatar xmlking commented on May 22, 2024 1

Looks like custom repositories are injected same way like entities

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

from typeorm.

et avatar et commented on May 22, 2024 1

@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.

from typeorm.

xmlking avatar xmlking commented on May 22, 2024

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

from typeorm.

andrzepakula avatar andrzepakula commented on May 22, 2024

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 .

from typeorm.

mentos1386 avatar mentos1386 commented on May 22, 2024

@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

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024
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?

from typeorm.

Roms1383 avatar Roms1383 commented on May 22, 2024

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 😿

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024

https://docs.nestjs.com/techniques/database#custom-repository

from typeorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.