【问题标题】:Nestjs - Typeorm custom connection nameNestjs - Typeorm 自定义连接名称
【发布时间】:2021-08-05 09:44:28
【问题描述】:

我有一个 Nestjs 数据库模块,它运行良好

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

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

如果我将连接名称更改为其他名称,而不是“默认”,则说“测试”,我会收到错误

@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'

【问题讨论】:

    标签: connection nestjs typeorm


    【解决方案1】:

    如下传递连接名称。

    @Module({
    imports: [
        TypeOrmModule.forRootAsync({
            name: 'test', // <=== here
            useFactory: () => {
                return {
                    type: "mysql",
                    ...
                };
            },
        }),
    
        TypeOrmModule.forFeature(entities, 'test'), // <=== here
        ],
        exports: [TypeOrmModule],
    })
    export class DBModule {}
    

    【讨论】:

    • 谢谢你的解决方案!
    • @levansuper 很高兴它解决了这个问题。你总是可以进入函数定义文件来检查它接受什么参数。另外,您可以接受此解决方案。
    猜你喜欢
    • 2021-10-01
    • 2021-10-17
    • 1970-01-01
    • 2021-01-07
    • 2019-08-13
    • 2022-01-21
    • 1970-01-01
    • 2022-11-27
    • 2020-12-20
    相关资源
    最近更新 更多