我试图将我的 TypeORM 项目分离到多个数据库上,因为它的规模不断增长,并且它的组件非常离散(但相互关联,所以我需要能够跨数据库建立关系)。
我正在尝试使用database
设置在@Entity
装饰器,如下所述:https://typeorm.io/#multiple-connections/using-multiple-databases-in-a-single-connection https://typeorm.io/#multiple-connections/using-multiple-databases-in-a-single-connection
我为此制作了一个最小的可重现示例,其中两个实体理论上应该放入不同的数据库中:
@Entity({ database: 'test' })
export default class Entity1 {
@PrimaryGeneratedColumn()
id?: number
@Column()
name?: string
@Column()
address?: string
}
and
@Entity({ database: 'database2' })
export default class Entity2 {
@PrimaryGeneratedColumn()
id?: number
@Column()
name?: string
@Column()
address?: string
}
连接代码:
import {createConnections} from "typeorm";
async function doDbExample() {
const connections = await createConnections([{
name: "db1Connection",
type: "postgres",
host: "db",
port: 5432,
username: "test",
password: "testPassword",
database: "test",
entities: [__dirname + "/entity/*{.js,.ts}"],
synchronize: true
}]);
console.log("Created connections")
}
doDbExample()
但是,所发生的情况是两个实体的表都放入连接的数据库中。我做错了什么,或者这是 TypeORM 中的错误?在我看来,这似乎不尊重database
不再设置。
我正在使用运行代码ts-node-dev
我在 github 上制作了一个完整的最小可重现示例,完成了数据库环境的 Docker 化设置:https://github.com/petterroea/TypeOrmBug-MRE https://github.com/petterroea/TypeOrmBug-MRE