Custom Repo without extends error: No repository for was found. Looks like this entity is not registered in current "default" connection?

When I use Custom Repository class without any extension then I run into the error: No repository for "MasterDataRepo" was found. Looks like this entity is not registered in current "default" connection?

@EntityRepository()
export class MasterDataRepo {

    constructor(private manager: EntityManager) {
    }

    getDriveryByTerminal(terminalCode: string):Promise<Driver> {
        return this.manager.findOne(Driver, { terminalCode });
    }
}

The app works fine if I create repository class after extending Repository

@EntityRepository(Driver)
export class MasterDataRepo extends Repository<Driver> {

        getDriveryByTerminal(terminalCode: string):Promise<Driver> {
        return this.manager.findOne(Driver, { terminalCode });

        }
}

My MasterDataModule is

import { Module } from '@nestjs/common';
import { MasterDataController } from './MasterData.controller';
import { MasterDataService } from './masterData.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MasterDataRepo } from './MasterData.Repo';

@Module({
  imports: [
    TypeOrmModule.forFeature([MasterDataRepo])
  ],
  controllers: [MasterDataController],
  providers: [MasterDataService],
})
export class MasterDataModule {}

and AppModule is

import { Module } from '@nestjs/common';
import { TasksModule } from './tasks/tasks.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeOrmConfig } from './config/typeorm.config';
import { MasterDataModule } from './masterData/MasterData.module';
import { Connection } from 'typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot(typeOrmConfig),
    TasksModule,
    MasterDataModule
  ],
})
export class AppModule {
  constructor(private connection: Connection) {}
}

1 answer

  • answered 2020-07-29 19:07 yash

    TypeOrmModule.forFeature([MasterDataRepo])
    

    TypeOrmModule.forFeature accepts array of Entities and your MasterDataRepo is not an entity.

    But when you use EntityRepository decorator with the User entity, it will add your User entity to the available entities list(which will be applied to the database connection). Also extending the Repository class will make your MasterDataRepo as an extended version of an Entity.

    https://github.com/typeorm/typeorm/blob/906d97fc8dbf1dba8f4e579a4f5bfead83af36ab/src/decorator/EntityRepository.ts

    https://github.com/typeorm/typeorm/blob/906d97fc8dbf1dba8f4e579a4f5bfead83af36ab/src/repository/Repository.ts

    Note:

    There are two solutions:

    1. Nestjs already provides @nestjs/typeorm module and you can inject a repository for an entity easily in your service.

    For example:

    @Injectable()
    export class UsersService {
      constructor(
        @InjectRepository(User)
        private usersRepository: Repository<User>
      ) {}
      ...
    }
    

    You can check the documentation here - https://docs.nestjs.com/techniques/database

    1. You can just use Active Record pattern that doesn't require a repository. What you need to do is just to make your entities extend BaseEntity of typeorm.

    For example:

    @Entity()
    export class User extends BaseEntity {} // <- BaseEntity
    
    ...
    
    const user = await User.find({}) // You just use the User class for executing a query.
    user.name = 'something';
    await user.save();