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

Custom repository Error #14

Closed
hellraisercenobit opened this issue Mar 5, 2018 · 8 comments
Closed

Custom repository Error #14

hellraisercenobit opened this issue Mar 5, 2018 · 8 comments

Comments

@hellraisercenobit
Copy link

With typeorm we can use custom repository to override default one to add extra methods.

For example:

@Component()
@EntityRepository(Jwt)
export class JwtRepository extends Repository<Jwt> {

}

After we juste have to inject it inside controller/model/service etc... like this:

  constructor(
    @OrmRepository('connectionName')
    private readonly jwtRepository: jwtRepository,

But only InjectRepository decorator is available on nestjs, so if we try to inject repo into a service:

@Component()
export class MyService {
  constructor(
    @InjectRepository(Jwt)
    private readonly jwtRepository: JwtRepository,

We got this error for any repo function:

Cannot read property 'findOne' of undefined
    at JwtRepository.Repository.findOne

How it is possible to use custom repo?

@BrunnerLivio
Copy link
Member

After hours of Googling, I finally found a workaround. The solution is still pretty elegant in my opinion, but this should actually be supported out of the box.

The workaround I use, is using a provider.

My image.repository.ts

/**
 * The image repository represents the
 * interface between the appliaction image entity
 * and the database.
 */
@EntityRepository(Image)
export class ImageRepository extends Repository<Image> {
    /**
     * Finds all images and applies the given pagination
     * options
     * @param pagination The pagination options
     */
    async findAll(pagination: PaginationOptionsDto): Promise<[Image[], number]> {
        // Get Images with the pagination options applied
        // from the database.
        return await this
            .createQueryBuilder('image')
            .offset(pagination.offset)
            .limit(pagination.limit)
            .getManyAndCount();
    }
}

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

In the module, simply inject the provider.

E.g.
image.module.ts:

import { ImageRepositoryProvider } from './image.repository';

@Module({
  imports: [
    TypeOrmModule.forFeature([Image])
  ],
  controllers: [ImageController],
  components: [
    ImageRepositoryProvider
  ],
})
/**
 * The Image module, which bundles all
 * operational or processable image related
 * modules, controllers and components
 */
export class ImageModule { }

Now using @Inject('ImageRepository'), you can inject it into your services etc.

@hellraisercenobit
Copy link
Author

oh yeah :) thx a lot @BrunnerLivio ! i'll try it soon to give you my feedback.

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Mar 19, 2018

@BrunnerLivio
Working like a charm!
Thx man!
Maybe it should be documented somewhere to avoid useless issues.

@kamilmysliwiec
Copy link
Member

This feature will be available in the nearest release (here's a pull request #18)

@shusson
Copy link

shusson commented Apr 5, 2018

@kamilmysliwiec this indicates that we shouldn't be creating services from custom repositories, especially if you want to use transactions. Thoughts?

@basvdijk
Copy link

@kamilmysliwiec What would be the correct use of this feature? Do you have an example?

@theduke
Copy link

theduke commented Jul 2, 2018

@kamilmysliwiec yeah some documentation would be appreciated

@regniblod
Copy link

@kamilmysliwiec +1 for documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants