如何使用 'Replication in typeorm'

how to use 'Replication in typeorm'

我想使用 TypeORM 设置 read/write 复制。我的复制连接设置示例:文件 -> ormconfig.js

import Config from './src/config'



module.exports = {
  type: 'mysql',
  logging: true,
  replication: {
    master: {
      host: "asdasd.amazonaws.com",
      port: 3306,
      username: "rwdev",
      password: "RWss4t3dev",
      database: "SysDev"
    },
    slaves: {
      host: "asdadasd.rds.amazonaws.com",
      port: 3306,
      username: "rodev",
      password: "ROss4r34ddev",
      database: "SysDevRead"
    }
  },
  entities: ['src/repo/entities/*.ts'],
  migrations: ['src/migrations/*.ts'],
  cli: {
    migrationsDir: "src/migrations"
  },
  subscribers: ['src/subscriber/**/*.ts'],
  extra: { connectionLimit: 10}
}

然后当我开始 运行 "yarn start" 我的代码时出现错误 "unhandledRejection TypeError: this.options.replication.slaves.forEach is not a function"。 我对使用 typeorm 执行此 "replication" 还是个新手,所以如果有任何帮助,我将不胜感激。谢谢

TypeORM 期望 slaves 是一个数组,因此你得到错误 "forEach is not a function"。所以你只需要包装它:

module.exports = {
  replication: {
    ...
    slaves: [{
      host: "asdadasd.rds.amazonaws.com",
      port: 3306,
      username: "rodev",
      password: "ROss4r34ddev",
      database: "SysDevRead"
    }],
    ...
}