Giter Site home page Giter Site logo

Comments (6)

B4nan avatar B4nan commented on September 2, 2024 1

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

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

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

from nestjs.

Quadriphobs1 avatar Quadriphobs1 commented on September 2, 2024

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

from nestjs.

Quadriphobs1 avatar Quadriphobs1 commented on September 2, 2024

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

from nestjs.

nikkonrom avatar nikkonrom commented on September 2, 2024

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

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

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.

from nestjs.

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.