ERROR
RepositoryNotFoundError: No repository for "UserQueryRepository" was found. Looks like this entity is not registered in current "default" connection?
VERSION
- @nestjs/typeorm - 7.1.5
- @nestjs/common - 7.6.15
- @nestjs/core - 7.6.15
- nodejs - 12.21.0
- typeorm - 0.2.34
DESCRIPTION OF THE PROBLEM
I have 2 projects: common & microservice-1
Common package has QueryService & QueryRepository and microservice-1 should extend from those files.
So in microservice-1 I have UserQueryService which extends from QueryService and UserQueryRepository which extends from QueryRepository.
query.repository.ts
import { FindManyOptions, Repository } from 'typeorm';
export class QueryRepository<Entity> extends Repository<Entity> {
findAll(options?: FindManyOptions<Entity>): Promise<Entity[]> {
return this.find(options);
}
}
query.service.ts
import { FindManyOptions } from 'typeorm';
import { QueryRepository } from '../repository';
export class QueryService<Entity> {
constructor(protected readonly repository: QueryRepository<Entity>) {}
findById(id: string): Promise<Entity> {
return this.repository.findOne(id);
}
findAll(options?: FindManyOptions<Entity>): Promise<Entity[]> {
return this.repository.findAll(options);
}
}
user.query.repository.ts
import { QueryRepository } from '@project/common';
import { EntityRepository, Repository } from 'typeorm';
import { UserEntity } from '../entity';
@EntityRepository(UserEntity)
export class UserQueryRepository
extends QueryRepository<UserEntity> { // -> throws error
// extends Repository<UserEntity> { // -> works without extending from common
}
user.query.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { QueryService } from '@project/common';
import { UserEntity } from '../entity';
import { UserQueryRepository } from '../repository';
@Injectable()
export class UserQueryService extends QueryService<UserEntity> {
constructor(
@InjectRepository(UserQueryRepository)
repository: UserQueryRepository,
) {
super(repository);
}
getAllUsers(): Promise<UserEntity[]> {
return this.findAll();
}
}
user.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserController } from '../controller';
import { UserQueryRepository } from '../repository';
import { UserQueryService } from '../service';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'pass',
database: 'dbname',
autoLoadEntities: true,
synchronize: true,
}),
TypeOrmModule.forFeature([UserQueryRepository]),
],
controllers: [UserController],
providers: [UserQueryService],
})
export class UserModule {}