kittgen / kittgen-nestjs

Set of libraries and code generators for Nestjs
MIT License
46 stars 0 forks source link

Nest can't resolve dependencies of the HistoryService (?, TYPEORM_HISTORY_OPTIONS). #60

Closed predragffwd closed 2 years ago

predragffwd commented 2 years ago

I'm getting issue when I try to create history for one of my entities. Issue that I'm getting is:

[Nest] 7980  - 11/08/2021, 4:46:27 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +98ms
[Nest] 7980  - 11/08/2021, 4:46:27 PM     LOG [InstanceLoader] PassportModule dependencies initialized +0ms
[Nest] 7980  - 11/08/2021, 4:46:27 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the HistoryService (?, TYPEORM_HISTORY_OPTIONS). Please make sure that the argument Connection at 
index [0] is available in the TypeOrmHistoryModule context.

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

Error: Nest can't resolve dependencies of the HistoryService (?, TYPEORM_HISTORY_OPTIONS). Please make sure that the argument Connection at index [0] is available in the TypeOrmHistoryModule context.

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

I registered module right under the TypeORM module,

TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (config: ConfigService) => {
    return {
      type: 'mysql',
      host: config.get('DATABASE_HOST'), // localhost
      port: config.get('DATABASE_PORT'), // 3306
      username: config.get('DATABASE_USERNAME'),
      password: config.get('DATABASE_PASSWORD'),
      database: config.get('DATABASE_NAME'),
      entities: [__dirname + '/../**/entities/*.entity{.ts,.js}'],
      synchronize: config.get('DATABASE_SYNC') || false
      timezone: 'Z',
    };
  },
}),
TypeOrmHistoryModule.register(),
....

And I added my history module like

import {
  HistoryFor,
  HistoryActionType,
  HistoryActionColumn,
  SnapshotColumn,
} from '@kittgen/nestjs-typeorm-history';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { UserData } from './user-data.entity';

@Entity()
@HistoryFor(UserData)
export class UserDataHistory {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @SnapshotColumn({ type: 'json' })
  payload: UserData;

  @HistoryActionColumn()
  action: HistoryActionType;

  // any other properties
  // ...
}
edgarmueller commented 2 years ago

@predragffwd The module is not global and it could be that the connection dependency is not yet fulfilled when your module is loaded. Could you try injecting on a module level where you also register your user entity (I suppose that would be UserModule in your case) and see if that works?

na3r commented 2 years ago

Hi @edgarmueller We have the same issue. This issue appeared on nestjs v8

 _   _             _      ___  _____  _____  _     _____
| \ | |           | |    |_  |/  ___|/  __ \| |   |_   _|
|  \| |  ___  ___ | |_     | |\ `--. | /  \/| |     | |
| . ` | / _ \/ __|| __|    | | `--. \| |    | |     | |
| |\  ||  __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/  \____/\_____/\___/

[System Information]
OS Version     : Linux 5.10
NodeJS Version : v14.15.3
YARN Version    : 1.22.5 

[Nest CLI]
Nest CLI Version : 7.6.0 

[Nest Platform Information]
platform-express version : 8.2.3
platform-fastify version : 8.2.3
event-emitter version    : 1.0.0
throttler version        : 2.0.0
passport version         : 8.0.1
swagger version          : 5.1.5
typeorm version          : 8.0.2
common version           : 8.2.3
config version           : 1.1.3
core version             : 8.2.3
jwt version              : 8.0.0
Done in 0.86s.

In addition, we register history module on module level

edgarmueller commented 2 years ago

@na3r I'm able to reproduce the issue with Nest 8. We'll push an update/fix soon

edgarmueller commented 2 years ago

We've updated the history module and released 2.0.0, see https://github.com/kittgen/kittgen-nestjs/releases/tag/nestjs-typeorm-history-v2.0.0 Could you give it a try?

na3r commented 2 years ago

Hi @edgarmueller Yes, it works now on nestjs v8 very well. Thank you

edgarmueller commented 2 years ago

@na3r Great, happy to hear that, closing the issue then.