我正在使用@nestjs/cqrs
模块。如何使用两个独立的数据库,一个用于写入端,一个用于读取端。 (两个数据库通过eventbus同步)
谢谢
最佳答案
使用 typeorm,您可以通过给出连接名称来指定多个数据库(如果您不指定名称,则将使用默认连接):
@Module({
imports: [
TypeOrmModule.forRoot({
...defaultOptions,
name: 'writeConnection',
host: 'write_db_host',
entities: [WriteEntity],
}),
TypeOrmModule.forRoot({
...defaultOptions,
name: 'readConnection',
host: 'read_db_host',
entities: [ReadEntity],
}),
],
})
export class ApplicationModule {}
然后在功能模块中,通过指定连接名称导入实体:
@Module({
imports: [
TypeOrmModule.forFeature([ReadEntity], 'readConnection'),
TypeOrmModule.forFeature([WriteEntity], 'writeConnection'),
],
})
export class FeatureModule {}
有关详细信息,请参阅 docs .
关于javascript - 如何使用 2 个独立的数据库,一个用于写入端,一个用于读取端。 (两个数据库通过eventbus同步),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55465785/