nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
65.82k stars 7.47k forks source link

[@nestjs/typeorm] `UseRepositories` helper function to make typeorm feature imports more explicit #10477

Closed proohit closed 1 year ago

proohit commented 1 year ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

Not a problem, but a suggested improvement of the developer experience for the typeorm integration. It is for one cumbersome how to use and not very clear what TypeOrmModule.forFeature does.

Describe the solution you'd like

//use-repositories.ts

import { TypeOrmModule } from '@nestjs/typeorm';
import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type';

const UseRepositories = (entities?: EntityClassOrSchema[]) =>
  TypeOrmModule.forFeature(entities);

export default UseRepositories;

Teachability, documentation, adoption, migration strategy

Possibly export this in @nestjs/typeorm and use it for imports in a @Module:

// some-entity.module.ts

@Module({
  imports: [UseRepositories([SomeEntity, SomeOtherEntity])],
  controllers: [SomeEntityController],
  providers: [SomeEntityService],
})
export class SomeEntityModule {}

Now repositories for SomeEntity and SomeOtherEntity can be injected:

// some-entity.service.ts

@Injectable()
export class SomeEntityService {
  constructor(
    @InjectRepository(SomeEntity)
    private readonly someEntityRepository: Repository<SomeEntity>,
    @InjectRepository(SomeOtherEntity)
    private readonly someOtherEntityRepository: Repository<SomeOtherEntity>,
  ) {}
}

What is the motivation / use case for changing the behavior?

I think the most common use case of TypeOrmModule.forFeature is to be able to inject repositories, so I would suggest a helper function to make these imports more explicit.

micalevisk commented 1 year ago

you can make the helper function in your side. I don't see any reason to move that to @nestjs/typeorm. It will just increase the API surface.

We could instead improve the doc around TypeOrmModule.forFeature to clarify what it does.

proohit commented 1 year ago

@micalevisk thanks for your blazingly fast answer :D .

I did it on my side and use it actively, yet still couldn't help but think that this a very common use case, so why not make it a 'standard'. I agree however that this would be an additional point for potential breaking changes, which makes maintainability a little harder.

micalevisk commented 1 year ago

I'm not sure on that. I'm pretty familiar with .forFeature. If I have a helper like that I wouldn't know what it does. And .forFeature follows a well-documented convention, so, to me, it's easier to understand it rather than UseRepositories (even tho this one has a good name)

Anyway, if you don't want to wait for Kamil's response, go ahead and open a PR at https://github.com/nestjs/typeorm :cat:

kamilmysliwiec commented 1 year ago

Thanks for your suggestion!

There are no plans to implement it in the foreseeable future.

If you think your request could live outside Nest's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.