#Unable to inject repository defined in TypeOrm entities list

15 messages · Page 1 of 1 (latest)

daring niche
#

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>,
  ) {}
#

One more thing is, I can add another TypeOrmModule import in the main app by appending:

TypeOrmModule.forFeature([UserEntityPsql]),

The injection works with no error, but there's nothing being logged by TypeOrm when I tried to e.g. create:

const entity = await this._repo.create(UserEntityPsql.fromModel(model))
static canopy
#

What does your UserModule look like?

daring niche
#

The UserModule is my main app. I started it with npm run start:dev user and imported TypeOrmModule as the above

static canopy
#

What is the value of DB_LOG_LEVEL?

daring niche
#

DB_LOG_LEVEL=["query", "error"]

#

I checked in the DB, there's no such record being created

static canopy
#

Weird, I'd expect some sort of logs or errors to be shown. You've fixed the injection problem so that's good

#

If you do logging: true what happens?

daring niche
#

I'm not sure whether I fixed the injection problem by having 2 imports of TypeOrmModule (one with forRootAsync and another forFeature)? I was actually concerned whether it creates 2 different connection / instances. But to clarify:
- when I have both TypeOrmModule forRootAsync and forFeature, there's no error, just that there's no logging (and no record is being created)
- when I have only TypeOrmModule forRootAsync even with entities defined, the error appears

I've tried with logging: true with no difference in the output

static canopy
#

when I have only TypeOrmModule forRootAsync even with entities defined, the error appears
This makes sense. forFeature() is required to create the injection token for @InjectRepository()
when I have both TypeOrmModule forRootAsync and forFeature, there's no error, just that there's no logging (and no record is being created)
This is interesting, and not what I would really expect

daring niche
#

I've just realised a stupid mistake.... it seems that in typeorm, I should use save() rather than create(). It works now

#

But why is the entities: [UserEntityPsql], in the TypeOrmModule.forRootAsync won't work and require TypeOrmModule.forFeature?

static canopy
daring niche
#

ahhh... noted. thanks a lot!!