我通过 nestjs 文档编写代码(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 应用程序没有运行。 这是错误控制台。
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/