NestJS/类型ORM : custom repository + multiple databases

标签 nestjs typeorm

我通过 nestjs 文档编写代码(custom repositorymultiple databases)

这是我的代码。

// app.module.ts
@Module({
    imports: [
        TypeOrmModule.forRoot({
            name: 'MYSQL_CONNECTION',
            type: 'mysql',
            port: 3306,
            entities: [AwsTest],
            logger: new MysqlLogger(),
            synchronize: false,
            extra: {
                connectionLimit: 10,
            },
        }),
        TypeOrmModule.forRoot({
            name: 'ORACLE_CONNECTION',
            type: 'oracle',
            entities: [],
            synchronize: false,
            logger: new OracleLogger(),
            extra: {
                connectionLimit: 10,
            },
        }),
        TestModule,
    ],
    providers: [Logger],
})
export class AppModule implements NestModule {}
// test.module.ts
@Module({
    imports: [TypeOrmModule.forFeature([TestRepository], 'MYSQL_CONNECTION')],
    providers: [TestService],
    controllers: [TestController],
})
export class TestModule {}
// test.service.ts
@Injectable()
export class TestService {
    constructor(private readonly testRepository: TestRepository) {}
    async getAwsTest() {
        return await this.testRepository.getAwsTest()
    }
}
// test.repository.ts
@EntityRepository(AwsTest)
export class TestRepository extends BaseRepository<AwsTest> {
    async getAwsTest() {
        return await this.find()
    }
}
// aws-test.entity.ts
@Entity('test', { database: 'report' })
export class AwsTest {
    constructor(name: string, address: string) {
        this._name = name
        this._address = address
    }

    @Column('varchar', { primary: true, name: 'name' })
    readonly _name: string

    @Column('varchar', { name: 'address' })
    readonly _address: string
}

在这种情况下,nest 应用程序没有运行。 这是错误控制台。

Nest can't resolve dependencies of the TestService (?). Please make sure that the argument TestRepository at index [0] is available in the TestModule context.

Potential solutions:

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

我需要你的帮助.....(在 app.module.ts 中,我隐藏了关键信息,如主机、用户名、密码、数据库、sid...)

最佳答案

尝试像这样注入(inject)您的存储库:

constructor(
  @InjectRepository(AwsTest, 'MYSQL_CONNECTION') private readonly testRepository: TestRepository,
) { }

关于NestJS/类型ORM : custom repository + multiple databases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70602450/

相关文章:

nestjs - 如何从 NestJS CacheManager 模块获取 redis io 客户端

typescript - 自定义存储库中带有 Typeorm 事务的 Nestjs

javascript - 使用 Nestjs 监听多个 RabbitMQ 队列

sql - TypeORM:所有权方面如何运作?

testing - 在内存数据库中的NestJs的e2e测试中创建了具有相同名称的多个连接

nestjs - Nest 无法解析 JwtStrategy 的依赖关系

使用 TypeOrm 进行 Postgresql 全文搜索

javascript - 如何在 Nest App 中使用 RabbitMQ 和 REST-API?

mysql - 使用nestjs框架编写mysql的自定义原始查询

具有 TypeORM : When using custom repository, 的 NestJS 是否需要服务?