【问题标题】:How to use 2 separate databases, one for the write side and one for the read side. (two databases synchronized via eventbus)如何使用 2 个独立的数据库,一个用于写入端,一个用于读取端。 (两个数据库通过事件总线同步)
【发布时间】:2019-04-02 01:42:34
【问题描述】:

我正在使用@nestjs/cqrs 模块。如何使用两个独立的数据库,一个用于写入端,一个用于读取端。 (两个数据库通过 eventbus 同步)

谢谢

【问题讨论】:

    标签: javascript node.js typescript cqrs nestjs


    【解决方案1】:

    使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多