Search code examples
node.jsnestjstypeorm

Nest can't resolve dependencies for Repository when having more than one database connection


I have two database connections in my project, so i had to name one of them as recommended by the documentation. The module that uses this named connection can't resolve the dependency for the Repository of my entity and I can't find out why.

I believe it has something to do with the "naming" of the connections because when I named the main connection, the other repositories had the same problem.

Appreciate any help!

The error:

Nest can't resolve dependencies of the DataFactService (?). Please make sure that the argument DataFactRepository at index [0] is available in the DataFactModule context.

Potential solutions:
- If DataFactRepository is a provider, is it part of the current DataFactModule?
- If DataFactRepository is exported from a separate @Module, is that module imported within DataFactModule?
  @Module({
    imports: [ /* the Module containing DataFactRepository */ ]
  })

The module:

@Module({
    imports: [TypeOrmModule.forFeature([DataFact], 'sumarized')],
    controllers: [DataFactController],
    providers: [DataFactService],
})
export class DataFactModule {}

The service:

@Injectable()
export class DataFactService {
    constructor(
        @InjectRepository(DataFact)
        private dataFactRepository: Repository<DataFact>,
    ) {}
}

The database connection

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            name: 'sumarized',
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => ({
                synchronize: false,
                type: 'mysql',
                host: configService.get('DB_HOST'),
                port: configService.get('DB_PORT'),
                username: configService.get('DB_USER'),
                password: configService.get('DB_PASSWORD'),
                database: configService.get('DB_SUMARIZED_DATABASE'),
                entities: [__dirname + '/../**/*.entity.{js,ts}'],
            }),
        }),
    ],
})
export class SumarizedDatabaseModule {}

The app module:

@Module({
    imports: [
        ConfigModule.forRoot({
            envFilePath: `.env.${process.env.NODE_ENV}`,
            isGlobal: true,
        }),
        WinstonModule.forRoot(winstonConfig),
        UsersModule,
        AuthModule,
        StoresEventsModule,
        StationsModule,
        DatabaseModule,
        MailerModule,
        SumarizedDatabaseModule,
        DataFactModule,
        SumarizedEventsModule,
    ],
    controllers: [],
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: LoggerInterceptor,
        },
    ],
})
export class AppModule {}

Solution

  • You need to use @InjectRepository(EntityClass, databaseName) if you are injecting an entity from a connection rather than default. This is mentioned in the docs

    With this setup, you have to tell the TypeOrmModule.forFeature() method and the @InjectRepository() decorator which connection should be used. If you do not pass any connection name, the default connection is used.