【问题标题】:NestJS/TypeORM : custom repository + multiple databasesNestJS/TypeORM:自定义存储库 + 多个数据库
【发布时间】:2022-01-09 16:44:01
【问题描述】:

我通过nestjs docs (custom repository, multiple 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 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...)

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: nestjs typeorm


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-07-02
    • 2021-01-07
    • 2021-06-07
    • 2020-04-19
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2022-07-07
    • 2020-04-25
    相关资源
    最近更新 更多