Nestjs - Typeorm 自定义连接名称

Nestjs - Typeorm custom connection name

我有一个 Nestjs 数据库模块,它工作得很好

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            useFactory: () => {
                return {
                    name: 'default', // <=== here
                    type: "mysql",
                    ...
                };
            },
        }),

        TypeOrmModule.forFeature(entities, 'default'), // <=== here
    ],
    exports: [TypeOrmModule],
})
export class DBModule {}

如果我将连接名称更改为其他名称而不是 'default' 说 'test' 我收到错误消息

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            useFactory: () => {
                return {
                    name: 'test', // <=== here
                    type: "mysql",
                    ...
                };
            },
        }),

        TypeOrmModule.forFeature(entities, 'test'), // <=== here
    ],
    exports: [TypeOrmModule],
})
export class DBModule {}
[Nest] 10746   - 05/15/2021, 5:55:34 PM   [ExceptionHandler] Nest can't resolve dependencies of the test_UserEntityRepository (?). Please make sure that the argument testConnection at index [0] is available in the TypeOrmModule context.

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

错误接缝只有在我使用 TypeOrmModule.forRootAsync 时才会出现 对于 TypeOrmModule.forRoot 如果有效!

是否有任何不同的方式来指示连接名称?我需要添加另一个连接,但由于此错误而无法添加。真的很想用'forRootAsync'

按如下方式传递连接名称。

@Module({
imports: [
    TypeOrmModule.forRootAsync({
        name: 'test', // <=== here
        useFactory: () => {
            return {
                type: "mysql",
                ...
            };
        },
    }),

    TypeOrmModule.forFeature(entities, 'test'), // <=== here
    ],
    exports: [TypeOrmModule],
})
export class DBModule {}