Im getting "[ExceptionHandler] UnknownDependenciesException [Error]: Nest can't resolve dependencies of the MysqlAgendaRepository (?). Please make sure that the argument "AgendaRepository" at index [0] is available in the AgendaModule context." everytime i start my application. I use MysqlAgendaRepository as concrete implementation and AgendaRepository as a abstration implementation.
agenda.module.ts:
import { Module } from '@nestjs/common';
import { AgendaService } from './agenda.service';
import { AgendaController } from './agenda.controller';
import { Agenda } from './entities/agenda.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MysqlAgendaRepository } from './repositories/agenda.mysql.repository';
@Module({
imports:[TypeOrmModule.forFeature([Agenda])],
providers: [AgendaService, { provide: "AgendaRepository", useClass: MysqlAgendaRepository}
],
controllers: [AgendaController]
})
export class AgendaModule {}
agenda.service.ts:
import { Inject, Injectable } from "@nestjs/common";
import { Agenda } from "./entities/agenda.entity";
import { IAgendaRepository } from "./repositories/agenda.repository";
@Injectable()
export class AgendaService{
public constructor(
@Inject("AgendaRepository")
private readonly agendaRepository: IAgendaRepository,
){}
public findAll(): Promise<Agenda[]>{
return this.agendaRepository.findAll()
}
}
agenda.mysql.repository.ts:
import { Repository } from "typeorm";
import { Agenda } from "../entities/agenda.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { Injectable } from "@nestjs/common";
import { IAgendaRepository } from "./agenda.repository";
@Injectable()
export class MysqlAgendaRepository implements IAgendaRepository {
public constructor(
@InjectRepository(Agenda)
private agendaRepository: Repository<Agenda>,
){}
public findAll(): Promise<Agenda[]> {
return this.agendaRepository.find()
}
}
agenda.repository.ts:
import { Agenda } from "../entities/agenda.entity";
export interface IAgendaRepository {
findAll(): Promise<Agenda[]>
}
agenda.entity.ts:
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity()
export class Agenda {
@PrimaryGeneratedColumn()
public id: number;
@Column()
public nome: string;
}
app.modoule:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CoreModule } from './core/core.module';
import { Agenda } from './core/agenda/entities/agenda.entity';
@Module({
imports: [
CoreModule,
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'assembleia',
entities: [Agenda],
synchronize: true,
}),
],
})
export class AppModule {}
So the issue is due to a limitation of @nestjs/typeorm
When you do
TypeOrmModule.forFeature([Agenda])
that dynamic module will register a provider under the "AgendaRepository"
token. So as long as you don't use the same name within the same module, it should work.
Change yours provider's name from "AgendaRepository"
to something else -- or change Agenda's class name
.