question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom repository Error

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
BrunnerLiviocommented, Mar 14, 2018

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.

3reactions
basvdijkcommented, Jun 17, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Jpa Repository erroring with Fragment ...
I am currently writing an application in spring boot and am building my own custom repository. First things first, ...
Read more >
Synchronizing a custom repository fails with error "Package id ...
Synchronizing a custom repository fails with error "Package id from primary metadata (XXXX), does not match package id from filelists, other ...
Read more >
Unable to upload .deb package in custom repository - Support
Problem: We created a custom product and repository in our Foreman 3.1, ... We are facing that error message when trying to upload...
Read more >
Error trying to open "SLE Custom Repository Management ...
Hi There!! I'm trying to configure out patches to our Custom SUSE Repository and I'm getting an error when I try to open...
Read more >
Get Started with Custom Error Handling in Spring Boot (Java)
So, clone the repository with the following command: git clone https://github.com/Tonel/spring-boot-custom-error-handling.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found