Hi all,
I'm experimenting with NestJS, and currently having an issue with injecting TypeORM repository into my service.
The error is: Error: Nest can't resolve dependencies of the UserRepositoryPsql (LOGGER_SERVICE, ?). Please make sure that the argument UserEntityPsqlRepository at index [1] is available in the UserModule context.
Below is my main module where I import TypeOrmModule and set the configuration. The configuration works and I'm able to connect to the database as I can see query logs (TypeOrm's logging level set to query and errors)
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
return {
type: config.get('DB_TYPE'),
host: config.get('DB_HOST'),
port: config.get<number>('DB_PORT'),
username: config.get('DB_USER'),
password: config.get('DB_PASSWORD'),
database: config.get('DB_NAME'),
autoLoadEntities: config.get<boolean>('DB_LOAD_ENTITIES'),
synchronize: config.get<boolean>('DB_SYNCHRONIZE'),
ssl: config.get<boolean>('DB_SSL'),
entities: [UserEntityPsql],
logging: JSON.parse(config.get<string>('DB_LOG_LEVEL')),
} as TypeOrmModuleOptions
},
}),
Class that injects the repository:
import { Repository } from 'typeorm'
import { IUserRepository } from '../../domain/user.repository.interface'
import { UserEntityPsql } from '../entity/user.entity.psql'
import { Inject, Injectable, Logger } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { LOGGER_SERVICE } from '@app/common/constants'
@Injectable()
export class UserRepositoryPsql<User> implements IUserRepository<User> {
constructor(
@Inject(LOGGER_SERVICE) private readonly _logger = new Logger(UserRepositoryPsql.name),
@InjectRepository(UserEntityPsql) private readonly _repo: Repository<UserEntityPsql>,
) {}